forked from abdulhakkeempa/cpp-programming-lab-cusat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshopping_list.cpp
171 lines (169 loc) · 3.89 KB
/
shopping_list.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
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
#include <iostream>
using namespace std;
class list{
string itemName;
int itemCode;
float itemPrice;
int itemQuantity;
float totalSum;
public:
list(){
itemCode=0;
itemPrice=0;
itemQuantity=0;
totalSum = 0;
}
void addItem(){
cout<<"Item Name : ";
cin>>itemName;
cout<<"Item Code : ";
cin>>itemCode;
cout<<"Item Quantity : ";
cin>>itemQuantity;
cout<<"Item Price : ";
cin>>itemPrice;
}
void displayItem(){
cout<<itemCode<<"\t\t"<<itemName<<"\t\t"<<itemPrice<<"\t\t"<<itemQuantity<<endl;
}
void alterQuantity(int decrement){
itemQuantity = itemQuantity - decrement;
}
int checkCode(){
return itemCode;
}
int checkQuantity(){
return itemQuantity;
}
void updateQuantity(int newQuantity){
itemQuantity = itemQuantity + newQuantity;
}
float generateSum(){
return itemQuantity*itemPrice;
}
void newQuantity(int newQuantity){
itemQuantity = newQuantity;
}
};
int main() {
list *stock = new list[10];
list *shoppingList = new list[10];
int stockCount = 0;
int shoppingCount = 0;
int option,choice;
int keyPosition;
bool found = false;
do {
cout<<"1.Add Item\n2.Delete Item\n3.Update Item\n4.Display Items\n5.Purchase Item"<<endl;
cin>>option;
switch (option) {
case 1:
stock[stockCount].addItem();
cout<<"Item Added Successfully"<<endl;
stockCount++;
break;
case 2:
{
int searchKey;
cout<<"Item Code : ";
cin>>searchKey;
for (int i = 0; i < stockCount; ++i) {
if (searchKey==stock[i].checkCode()) {
found = true;
keyPosition = i;
cout<<"Successfully Deleted"<<endl;
break;
}
}
if (!found) {
cout<<"Invalid Code"<<endl;
}
else{
for (int i = keyPosition; i < stockCount; ++i) {
stock[i]=stock[i+1];
}
stockCount--;
}
}
break;
case 3:
{
bool found = false;
cout<<"Item Code\tItem Name\tItem Price\tItem Quantity"<<endl;
for (int i = 0; i < stockCount; ++i) {
stock[i].displayItem();
}
int searchKey;
cout<<"Enter the Item Code : ";
cin>>searchKey;
for (int i = 0; i < stockCount; ++i) {
if (searchKey==stock[i].checkCode()) {
int x;
cout<<"Enter the Amount of Quantity : ";
cin>>x;
stock[i].updateQuantity(x);
found = true;
cout<<"Successfully Updated"<<endl;
break;
}
}
if(!found){
cout<<"Invalid Code"<<endl;
}
}
break;
case 4:
cout<<"Item Code\tItem Name\tItem Price\tItem Quantity"<<endl;
for (int i = 0; i < stockCount; ++i) {
stock[i].displayItem();
}
break;
case 5:
int choice;
do{
bool purchased = false;
int tempItemCode,tempItemQuantity;
cout<<"Enter the Item Code"<<endl;
cin>>tempItemCode;
cout<<"Quantity you want to purchase"<<endl;
cin>>tempItemQuantity;
for (int i = 0; i < stockCount; ++i) {
if (tempItemCode==stock[i].checkCode()) {
if(tempItemQuantity>stock[i].checkQuantity()){
cout<<"Insufficient Stock"<<endl;
break;
}
cout<<"Came"<<endl;
purchased = true;
shoppingList[shoppingCount]=stock[i];
shoppingList[shoppingCount].newQuantity(tempItemQuantity);
stock[i].alterQuantity(tempItemQuantity);
shoppingList[shoppingCount].displayItem();
shoppingCount++;
break;
}
}
cout<<"Do you want to continue purchase"<<endl;
cout<<"1.Continue\n2.Generate Bill"<<endl;
cin>>choice;
float totalSum;
if(choice==2 and purchased==true){
cout<<"Your Purchase Bill"<<endl;
cout<<"Item Code\tItem Name\tItem Price\tItem Quantity"<<endl;
for (int i = 0; i < shoppingCount; ++i) {
shoppingList[i].displayItem();
totalSum = totalSum + shoppingList[i].generateSum();
}
cout<<"Total Sum = "<<totalSum<<endl;
}
}
while(choice==1);
break;
default:
break;
}
cout<<"1.Continue\n2.Quit"<<endl;
cin>>choice;
}while(choice==1);
return 0;
}