-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShoppingCart.cpp
executable file
·117 lines (104 loc) · 3.03 KB
/
ShoppingCart.cpp
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
#include "ShoppingCart.h"
#include "ItemToPurchase.h"
#include <iostream>
#include <iomanip>
using namespace std;
ShoppingCart::ShoppingCart() {
customerName = "none";
dateCartCreated = "January 1, 2016";
}
ShoppingCart::ShoppingCart(string userName, string theDate) {
customerName = userName;
dateCartCreated = theDate;
}
string ShoppingCart::GetCustomerName(string userName) const {
return customerName;
}
string ShoppingCart::GetDate(string theDate) const {
return dateCartCreated;
}
void ShoppingCart::ShowOptions(){
cout << "MENU" << endl;
cout << "add - Add item to cart" << endl;
cout << "remove - Remove item from cart" << endl;
cout << "change - Change item quantity" << endl;
cout << "descriptions - Output items' descriptions" << endl;
cout << "cart - Output shopping cart" << endl;
cout << "options - Print the options menu" << endl;
cout << "quit - Quit" << endl << endl;
}
void ShoppingCart::FindItem(string theItem) {
itemPresent = false;
for (unsigned int i = 0; i < itemsShoppingCart.size(); ++i) {
if (itemsShoppingCart.at(i).GetName() == theItem) {
itemPresent = true;
modLocation = i;
break;
}
}
}
void ShoppingCart::AddItem(ItemToPurchase newItem) {
FindItem(newItem.GetName());
if (itemPresent == true) {
cout << "Item is already in cart. Nothing added." << endl;
}
else {
itemsShoppingCart.push_back(newItem);
}
cout << endl;
}
void ShoppingCart::RemoveItem(string removeItem) {
FindItem(removeItem);
if (itemPresent == false) {
cout << "Item not found in cart. Nothing removed." << endl;
}
else {
itemsShoppingCart.erase(itemsShoppingCart.begin() + modLocation);
}
cout << endl;
}
void ShoppingCart::ChangeQuantity(string theName, int newQuantity) {
FindItem(theName);
if (itemPresent == false) {
cout << "Item not found in cart. Nothing modified." << endl;
}
else {
itemsShoppingCart.at(modLocation).SetQuantity(newQuantity);
}
cout << endl;
}
void ShoppingCart::ShowDescriptions() {
cout << customerName << "\'s Shopping Cart - " << dateCartCreated << endl;
if (itemsShoppingCart.empty() == true) {
cout << "Shopping cart is empty." << endl;
}
else {
cout << "Item Descriptions" << endl;
for (unsigned int i = 0; i < itemsShoppingCart.size(); ++i) {
itemsShoppingCart.at(i).PrintDescription();
}
}
cout << endl;
}
void ShoppingCart::ShowCart() {
cout << customerName << "\'s Shopping Cart - " << dateCartCreated << endl;
if (itemsShoppingCart.empty() == true) {
cout << "Shopping cart is empty." << endl;
}
else {
cout << "Number of items: ";
int totalItems = 0;
double totalCost = 0.00;
for (unsigned int i = 0; i < itemsShoppingCart.size(); ++i) {
totalItems += itemsShoppingCart.at(i).GetQuantity();
totalCost += (itemsShoppingCart.at(i).GetPrice() * itemsShoppingCart.at(i).GetQuantity());
}
cout << totalItems << endl << endl;
cout << fixed << showpoint << setprecision(2);
for (unsigned int i = 0; i < itemsShoppingCart.size(); ++i) {
itemsShoppingCart.at(i).PrintCost();
}
cout << endl << "Total: $" << totalCost << endl;
}
cout << endl;
}