-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart_main.cpp
94 lines (78 loc) · 1.75 KB
/
cart_main.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
#include <iostream>
#include "datamodel.h"
#include <vector>
using namespace std;
vector<Product> allProducts = {
Product(1,"apple",26),
Product(2,"mango",16),
Product(3,"gauva ",36),
Product(4,"banana",56),
Product(5,"strawberry",29),
Product(6,"pineapple",20),
};
Product* chooseProduct(){
//Display the list of products
string productlist;
cout << "Avialable Products " << endl;
for(auto product : allProducts){
productlist.append(product.getDisplayName());
}
cout << productlist << endl;
cout <<"---------------------" << endl;
string choice;
cin>> choice;
for(int i=0; i< allProducts.size(); i++){
if(allProducts[i].getShortName() == choice){
return &allProducts[i];
}
}
cout<<"Product not found!" << endl;
return NULL;
}
bool checkout(Cart &cart){
if(cart.isEmpty()){
return false;
}
int total = cart.getTotal();
cout << "Pay in Cash";
int paid;
cin >> paid;
if(paid>=total){
cout <<"Change " << paid - total << endl;
cout << "Thank you for shopping!";
return true;
}else{
cout<<"Not enough cash!";
return false;
}
}
int main(){
char action;
Cart cart;
while(true){
cout << "Select an action - (a)dd item, (v)iew cart, (c)heckout" << endl;
cin >> action;
if(action == 'a'){
//Add to cart
//View all the Products + choose a product + Add to cart
Product * product = chooseProduct();
if(product!=NULL){
cout <<"Added to the Cart " << product -> getDisplayName() << endl;
cart.addProduct(*product);
}
}else if(action == 'v'){
//View the cart
cout<< "-------------------------" << endl;
cout<< cart.viewCart();
cout<<"--------------------------"<< endl;
}
else{
//Checkout
cart.viewCart();
if(checkout(cart)){
break;
}
}
}
return 0;
}