task
task
#include <vector>
#include <string>
#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) {}
class Inventory {
private:
std::vector<Item*> items;
float total_money;
public:
Inventory() : total_money{0} {}
~Inventory() {
for (Item* item : items) {
delete item;
}
}
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;
void sell_item() {
std::string item_to_check;
std::cin.ignore();
std::cout << "\nEnter item name: ";
std::cin >> item_to_check;
if (item->get_quantity() == 0) {
delete item; // Deallocate memory for the item
items.erase(items.begin() + item_index); // Remove item from vector
std::cout << "\nItem completely removed from inventory.";
}
} else {
std::cout << "\nCannot sell more items than you have.";
}
}
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;
}
}
}