-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
228 lines (179 loc) · 3.72 KB
/
main.cpp
File metadata and controls
228 lines (179 loc) · 3.72 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* You don't need to edit this file, although you can if you wish.
* This source file will not be graded.
*
* This is a live TUI for the BookStore you're working on. You can
* use it to manually play around in your store.
*/
//
#include "BookStore.hpp"
//
#include <iostream>
#include <string>
//
using std::cout, std::cin, std::endl;
using std::string;
//
using CPSC131::BookStore::BookStore, CPSC131::BookStore::Book;
// PROTO
void testRun();
void menuLoop();
void purchaseInventory();
void viewInventory();
void sellToCustomer();
string getLine();
// Globals: Not a great practice, but very good for a lazy professor making starter code :)
BookStore store;
//
int main()
{
//
testRun();
//menuLoop();
//
cout << "Program exiting" << endl;
return 0;
}
void testRun()
{
CPSC131::DoublyLinkedList::DoublyLinkedList<int> list;
list.push_front(19);
list.Print();
list.push_front(12);
list.Print();
list.push_front(-129878);
list.Print();
list.push_front(228764364);
list.Print();
list.push_front(119991111);
list.Print();
list.push_front(0);
list.Print();
list.push_front(1900000000);
list.Print();
//for (Iterator it (list.begin()) ; !it.isAtEnd() ; it++)
{
list.Print();
}
CPSC131::DoublyLinkedList::DoublyLinkedList<int> list2;
list2 = list;
std::cout << "PRINTING LIST 2" << std::endl;
list2.Print();
auto list3 = list;
std::cout << "PRINTING LIST 3" << std::endl;
list3.Print();
}
//
void menuLoop()
{
//
while (true)
{
//
auto balance = store.getAccountBalance();
//
cout
<< endl << endl << endl
<< "*** Book Store - Main Menu ***" << endl
<< endl
<< "Account balance: " << balance << " cents" << (balance >= 0 ? "" : " (Oh nyo)") << endl
<< endl
<< "1. Purchase inventory (add Book to store)" << endl
<< "2. View inventory" << endl
<< "3. Sell to customer" << endl
<< endl
<< "Q. Quit" << endl
<< endl
<< "Enter your selection ==> "
;
//
string choice = getLine();
cout << endl;
//
if ( choice == "1" ) {
purchaseInventory();
}
else if ( choice == "2" ) {
viewInventory();
}
else if ( choice == "3" ) {
sellToCustomer();
}
else if ( choice == "Q" || choice == "q" ) {
break;
}
else {
cout << "Invalid choice: " << choice << endl;
}
}
}
//
void purchaseInventory()
{
string title, author, isbn;
size_t price_cents, quantity;
//
cout
<< "Purchasing inventory ..." << endl
<< endl
<< "Enter book ISBN: "
;
isbn = getLine();
//
if ( !store.bookExists(isbn) ) {
cout << "Enter title: ";
title = getLine();
cout << "Enter author: ";
author = getLine();
}
//
cout << "Enter wholesale price (in cents): ";
price_cents = stoi(getLine());
cout << "Enter quantity: ";
quantity = stoi(getLine());
//
store.purchaseInventory(
title, author, isbn,
price_cents,
quantity
);
}
//
void viewInventory()
{
store.printInventory();
}
//
void sellToCustomer()
{
cout << "Selling to customer ..." << endl;
string isbn;
cout << "Enter the ISBN of the book to sell: ";
isbn = getLine();
size_t price;
cout << "Enter the price per copy, in cents: ";
price = stoi(getLine());
size_t quantity;
cout << "Enter the quantity to sell: ";
quantity = stoi(getLine());
try
{
store.sellToCustomer(isbn, price, quantity);
cout << "Sale was successful" << endl;
cout << "Sold " << quantity << " copies of ISBN:" << isbn << " at " << price << " cents each" << endl;
}
catch( const std::exception& e )
{
cout << "Failed to sell to customer: " << e.what() << endl;
}
}
//
string getLine()
{
const size_t BUFFER_SIZE = 8192;
char buffer[BUFFER_SIZE];
cin.clear();
cin.getline(buffer, BUFFER_SIZE);
string s = buffer;
return s;
}