Repository navigation
Expand file tree
/
Copy pathEReader.cpp
More file actions
162 lines (150 loc) · 5.96 KB
/
Copy pathEReader.cpp
File metadata and controls
162 lines (150 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include "Book.h"
#include "EReader.h"
using namespace std;
//Name: EReader - Default Constructor
//Precondition: None
//Postcondition: Creates a new EReader
EReader::EReader(){}
//Name: EReader - Overloaded Constructor
//Precondition: None
//Postcondition: Creates a new EReader and passes the book filename
EReader::EReader(string filename){
LoadCatalog(filename);
}
//Name: ~EReader - Destructor
//Precondition: None
//Postcondition: Creates a new EReader
EReader::~EReader(){
for(int i = 0; i < int(m_bookCatalog.size()); i++){
delete m_bookCatalog[i];
m_bookCatalog[i] = nullptr;
}
}
//Name: LoadCatalog(string filename)
//Precondition: Requires filename
//Postcondition: Loads the file into the m_bookCatalog (as books)
void EReader::LoadCatalog(string filename){
string title;
string author;
string rank;
string price;
ifstream F(filename);
F.is_open();
while(getline(F, title, ',')){
getline(F, author, ',');
getline(F, rank, ',');
getline(F, price);
Book *book = new Book(title, author, stoi(rank), stod(price));
m_bookCatalog.push_back(book);
}
F.close();
}
//Name: MainMenu
//Precondition: None
//Postcondition: Manages the application and the menu
void EReader::MainMenu(){
int choice = 0;
while(choice != 5){
cout << "What would you like to do?" << endl;
cout << "1. Display All Books" << endl;
cout << "2. Add Book to Readlist" << endl;
cout << "3. Display Readlist" << endl;
cout << "4. Sort Readlist by Price" << endl;
cout << "5. Exit" << endl;
cin >> choice;
while (choice < 1 or choice > 5){
cout << "What would you like to do?" << endl;
cout << "1. Display All Books" << endl;
cout << "2. Add Book to Readlist" << endl;
cout << "3. Display Readlist" << endl;
cout << "4. Sort Readlist by Price" << endl;
cout << "5. Exit" << endl;
cin >> choice;
}
switch(choice){
case 1:
DisplayBooks();
break;
case 2:
AddBook();
break;
case 3:
DisplayReadlist();
break;
case 4:
SortReadlist();
break;
case 5:
cout << "Thanks for using the EReader!" << endl;
}
}
}
//Name: DisplayBooks
//Precondition: Displays all 150 books
//Postcondition: Lists all books (numbered)
void EReader::DisplayBooks(){
for(int i = 0; i < int(m_bookCatalog.size()); i++){
cout << i+1 << "."<< *m_bookCatalog[i] << endl;
}
}
//Name: AddBook
//Precondition: m_bookCatalog is populated
//Postcondition: Displays the current list and adds a book
// to the m_readList
void EReader::AddBook(){
cout << "Choose the book you would like to add to your reading list." << endl;
DisplayBooks();
int num;
cout << "Enter the number of the book you would like to add." << endl;
cin >> num;
while(num < 1 or num >int(m_bookCatalog.size())){
cout << "Enter the number of the book you would like to add." << endl;
cin >> num;
}
num--;
m_readList.Push(*m_bookCatalog[num], true);
}
//Name: DisplayReadlist
//Precondition: None (can be empty)
//Postcondition: Displays the readlist
void EReader::DisplayReadlist(){
if(int(m_readList.GetSize()) == 0)
cout << "Your reading list is empty." << endl;
else{
for(int i = 0; i < int(m_readList.GetSize()); i++){
cout << i+1 << "." << m_readList[i] << endl;
}
}
}
//Name: SortReadlist()
//Precondition: Requires Readlist
//Postcondition: Uses the overloaded < operator in Book to sort
// the prices from high to low in the m_readList
void EReader::SortReadlist(){
if(m_readList.GetSize() == 0)
cout << "Your reading list is empty." << endl;
else{
bool inOrder = false;
do{
//running through the list and swapping
for(int i = 0; i < m_readList.GetSize() - 1; i++){
if(m_readList[i] < m_readList[i+1]){
m_readList.Swap(i+1);
}
}
//increments counter if need more sorts
int numToSort = 0;
for(int j = 0; j < m_readList.GetSize() - 1; j++){
if(m_readList[j] < m_readList[j+1])
numToSort++;
}
//if it's in order, updates the bool
if(numToSort == 0)
inOrder = true;
} while(inOrder == false);
}
}