-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask4_starter_updated.cpp
More file actions
185 lines (155 loc) · 4.58 KB
/
Copy pathtask4_starter_updated.cpp
File metadata and controls
185 lines (155 loc) · 4.58 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
#include <string>
#include<iostream>
#include <utility>
#include <climits> // for INT_MAX
//Modifed the remove section only.
//I did not change array to vectors as I wasn't sure I was allowed to do it
//I may also resubmit the vector version as well.
class Item {
private:
std::string name;
int quantity;
float price;
public:
Item(
std::string name,
int quantity,
float price
) :
name{ std::move(name) },
quantity{ quantity },
price{ price } {
}
std::string get_name() const {
return name;
}
int get_quantity() const {
return quantity;
}
void set_quantity(int new_quantity) {
quantity = new_quantity;
}
float get_price() const {
return price;
}
bool is_match(const std::string& other) {
return name == other;
}
};
class Inventory {
private:
Item* items[20]; // Kept it as array as I wasn't sure I was allowed to modify this
float total_money;
int item_count;
static void display_data(Item& item) {
std::cout << "\nItem name: " << item.get_name();
std::cout << "\nQuantity: " << item.get_quantity();
std::cout << "\nPrice: " << item.get_price();
}
public:
Inventory() :
items{},
total_money{ 0 },
item_count{ 0 } {
}
void add_item() {
std::string name;
int quantity;
float price;
std::cin.ignore();
std::cout << "\nEnter item name: ";
std::cin >> name;
std::cout << "Enter quantity: ";
std::cin >> quantity;
std::cout << "Enter price: ";
std::cin >> price;
items[item_count] = new Item(name, quantity, price);
item_count++;
}
void sell_item() {
std::string item_to_check;
std::cin.ignore();
std::cout << "\nEnter item name: ";
std::cin >> item_to_check;
for (int i = 0; i < item_count; i++) {
if (items[i]->is_match(item_to_check)) {
remove_item(i);
return;
}
}
std::cout << "\nThis item is not in your Inventory";
}
void remove_item(int item_index) {
int input_quantity;
Item* item = items[item_index];
std::cout << "\nEnter number of items to sell: ";
std::cin >> input_quantity;
int quantity = item->get_quantity();
if (input_quantity <= quantity) {
float price = item->get_price();
float money_earned = price * input_quantity;
item->set_quantity(quantity - input_quantity);
std::cout << "\nItems sold";
std::cout << "\nMoney received: " << money_earned;
total_money += money_earned;
//Modifed and Updated code here:
// if the quantity reaches zero, delete the item and shift others
if (item->get_quantity() == 0) {
delete item; // free the memory of item
// Shift items in array to fill the empty spaces (shifted spaces)
for (int i = item_index; i < item_count - 1; i++) {
items[i] = items[i + 1];
}
items[item_count - 1] = nullptr; // set last item to nullptr
item_count--; // decrease item count
std::cout << "\nItem removed from inventory as quantity is zero.";
}
}
else {
std::cout << "\nCannot sell more items than you have.";
}
}
void list_items() {
if (item_count == 0) {
std::cout << "\nInventory empty.";
return;
}
for (int i = 0; i < item_count; i++) {
display_data(*items[i]);
std::cout << "\n";
}
}
};
// no need to modify anything here
int main() {
int choice;
Inventory inventory_system;
std::cout << "Welcome to the inventory!";
while (1) {
std::cout << "\n\nMENU\n"
<< "1. Add new item\n"
<< "2. Sell item\n"
<< "3. List items\n"
<< "4. Exit\n\n"
<< "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1:
inventory_system.add_item();
break;
case 2:
inventory_system.sell_item();
break;
case 3:
inventory_system.list_items();
break;
case 4:
exit(0);
default:
std::cout << "\nInvalid choice entered";
std::cin.clear();
std::cin.ignore(INT_MAX, '\n');
break;
}
}
}