-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookStore.cpp
More file actions
243 lines (216 loc) · 6.07 KB
/
BookStore.cpp
File metadata and controls
243 lines (216 loc) · 6.07 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
* TODO: Implement all the BookStore methods below.
* Note you'll have to remain inside the CPSC131::BookStore namespace.
* name: Anderson Pham
* CWID: 884815002
* Email: ahpham123@csu.fullerton.edu
*/
//
#include "BookStore.hpp"
//
#include <iostream>
#include <sstream>
#include <string>
//
using std::cout, std::cin, std::endl;
using std::string, std::to_string;
using std::stringstream;
//
namespace CPSC131::BookStore
{
/// Your welcome
BookStore::BookStore() {}
/**
* Adjust the store's account balance
* Should accept positive or negative adjustments
*/
void BookStore::adjustAccountBalance(int adjustment)
{
account_balance_ += adjustment;
}
/**
* Return the store's current account balance
*/
int BookStore::getAccountBalance() const
{
return account_balance_;
}
/**
* Find a book by its ISBN
*
* Return this->bookList.end() if the book isn't found.
*
* Return an interator pointing to the Book if it is found.
*/
DoublyLinkedList::DoublyLinkedList<Book>::Iterator BookStore::findBook(std::string isbn) const
{
for (auto it = bookList.begin() ; !(it.isAtEnd()) ; it++)
{
if (it.getCursor()->getElement().getIsbn() == isbn)
{
return it;
}
}
return this->bookList.end();
//return DoublyLinkedList::DoublyLinkedList<Book>::Iterator();
}
/**
* Check whether a book exists, by its ISBN
*
* Return true if it exists, or false otherwise
*/
bool BookStore::bookExists(std::string isbn) const
{
for (auto it = bookList.begin() ; !(it.isAtEnd()) ; it++)
{
if (it.getCursor()->getElement().getIsbn() == isbn)
{
return true;
}
}
return false;
//return false;
}
/**
* Check the quantity of stock we have for a particular book, by ISBN
*
* If the book doesn't exist, just return 0
*/
size_t BookStore::getBookStockAvailable(std::string isbn) const
{
for (auto it = bookList.begin() ; !(it.isAtEnd()) ; it++)
{
if (it.getCursor()->getElement().getIsbn() == isbn)
{
return it.getCursor()->getElement().getStockAvailable();
}
}
return 0;
}
/**
* Locate a book by ISBN and return a reference to the Book
*
* If the book doesn't exist, throw an exception
*/
Book& BookStore::getBook(std::string isbn) const
{
if (bookExists(isbn))
{
return findBook(isbn).getCursor()->getElement();
}
else
{
throw std::range_error ("Called getBook on nonexistent book");
}
//return *(new Book());
}
/**
* Take a Book instance and add it to inventory
*
* If the book's ISBN already exists in our store,
* simply adjust account balance by the book's price and quantity,
* but ignore other details like title and author.
*
* If the book's ISBN doesn't already exist in our store,
* adjust our account balance and push the book into our list
*/
void BookStore::purchaseInventory(const Book& book)
{
if (bookExists(book.getIsbn()))
{
adjustAccountBalance(-1 * book.getPriceCents() * book.getStockAvailable());
findBook(book.getIsbn()).getCursor()->getElement().adjustStockAvailable(book.getStockAvailable());
}
else
{
adjustAccountBalance(-1 * book.getPriceCents() * book.getStockAvailable());
bookList.push_back(book);
}
}
/**
* Take some book details and add the book to our inventory.
*
* Use the same rules as the other overload for this function.
*
* You might want to avoid repeating code by simply building a Book
* object from the details, then calling the other overload
* with the new Book object.
*/
void BookStore::purchaseInventory(
std::string title, std::string author, std::string isbn,
size_t price_cents,
size_t unit_count
)
{
Book* book = new Book(title, author, isbn, price_cents, unit_count);
if (bookExists(book->getIsbn()))
{
adjustAccountBalance(-1 * book->getPriceCents() * unit_count);
findBook(book->getIsbn()).getCursor()->getElement().adjustStockAvailable(book->getStockAvailable());
}
else
{
adjustAccountBalance(-1 * book->getPriceCents() * unit_count);
bookList.push_back(*book);
}
}
/**
* Print out inventory.
* Should be in a particular way to earn unit test points.
* Example (ignore the asterisks at the left of this comment block):
*
* *** Book Store Inventory ***
* "Book1", by Author1 [123] (5 in stock)
* "Book2", by Author2 [456] (19 in stock)
*
* Should print a trailing std::endl after the last book line, for the unit tests
*/
void BookStore::printInventory() const
{
std::cout << "*** Book Store Inventory ***" << std::endl;
for (auto it = bookList.begin() ; !(it.isAtEnd()) ; it++)
{
std::cout << '"' << it.getCursor()->getElement().getTitle() << '"' << ", by " << it.getCursor()->getElement().getAuthor() << " [" << it.getCursor()->getElement().getIsbn() << "] (" << std::to_string(it.getCursor()->getElement().getStockAvailable()) << " in stock)" << std::endl;
}
//std::cout << std::endl;
}
/**
* Sell a book to a customer!
*
* Takes the ISBN of the book, the selling price of the book, and the quantity of books sold
*
* Uses the same rules as the other overload.
*
* You may wish to just grab a reference to the book and call the other overload,
* to avoid repeating code
*/
void BookStore::sellToCustomer(std::string isbn, size_t price_cents, size_t quantity)
{
auto book = getBook(isbn);
if (book.getStockAvailable() < quantity)
{
throw std::range_error ("Called sellToCustomer(isbn), not enough books");
}
book.adjustStockAvailable(quantity);
this->adjustAccountBalance(price_cents * quantity);
}
/**
* Sell a book to a customer!
*
* Takes a Book reference, the selling price of the book, and the quantity of books sold
*
* If we don't have enough of this book in stock for the quantity the customer wants to purchase,
* throw an std::range_error
*
* Otherwise, adjust the stock available in our store, and update our account balance.
*/
void BookStore::sellToCustomer(Book& book, size_t price_cents, size_t quantity)
{
// TODO: Your code here
}
/*
private:
::CPSC131::DoublyLinkedList::DoublyLinkedList<Book> bookList;
int account_balance_ = 0;
*/
}