-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefactor-starter.cpp
More file actions
166 lines (138 loc) · 3.79 KB
/
refactor-starter.cpp
File metadata and controls
166 lines (138 loc) · 3.79 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
#include <string>
#include<iostream>
#include <utility>
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];
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;
} 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;
}
}
}