写一个c++ 面向对象的酒水管理系统
时间: 2023-12-24 09:05:29 浏览: 27
好的,我可以为你提供一个简单的面向对象的酒水管理系统的代码示例。该系统包括酒水类、库存类和管理类。
```
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Drink {
public:
Drink(string name, double price, int quantity)
: name_(name), price_(price), quantity_(quantity) {}
string GetName() const { return name_; }
double GetPrice() const { return price_; }
int GetQuantity() const { return quantity_; }
void SetName(string name) { name_ = name; }
void SetPrice(double price) { price_ = price; }
void SetQuantity(int quantity) { quantity_ = quantity; }
private:
string name_;
double price_;
int quantity_;
};
class Inventory {
public:
void AddDrink(Drink drink) { drinks_.push_back(drink); }
void RemoveDrink(int index) {
if (index >= 0 && index < drinks_.size()) {
drinks_.erase(drinks_.begin() + index);
}
}
Drink& GetDrink(int index) { return drinks_[index]; }
int GetSize() const { return drinks_.size(); }
private:
vector<Drink> drinks_;
};
class Manager {
public:
void PrintInventory() {
cout << "Inventory:" << endl;
for (int i = 0; i < inventory_.GetSize(); i++) {
Drink drink = inventory_.GetDrink(i);
cout << i + 1 << ". " << drink.GetName() << " - $" << drink.GetPrice() << " - " << drink.GetQuantity() << " in stock" << endl;
}
cout << endl;
}
void AddDrink() {
string name;
double price;
int quantity;
cout << "Enter the name of the drink: ";
cin >> name;
cout << "Enter the price of the drink: ";
cin >> price;
cout << "Enter the quantity of the drink: ";
cin >> quantity;
Drink drink(name, price, quantity);
inventory_.AddDrink(drink);
cout << "Drink added to inventory." << endl << endl;
}
void RemoveDrink() {
int index;
cout << "Enter the index of the drink to remove: ";
cin >> index;
inventory_.RemoveDrink(index - 1);
cout << "Drink removed from inventory." << endl << endl;
}
private:
Inventory inventory_;
};
int main() {
Manager manager;
while (true) {
cout << "1. Print inventory" << endl;
cout << "2. Add a drink" << endl;
cout << "3. Remove a drink" << endl;
cout << "4. Exit" << endl;
int choice;
cout << "Enter your choice: ";
cin >> choice;
cout << endl;
if (choice == 1) {
manager.PrintInventory();
} else if (choice == 2) {
manager.AddDrink();
} else if (choice == 3) {
manager.RemoveDrink();
} else if (choice == 4) {
break;
} else {
cout << "Invalid choice. Please try again." << endl << endl;
}
}
return 0;
}
```
该程序使用了三个类,Drink类表示酒水,Inventory类表示库存,Manager类表示管理。程序提供了打印库存、添加酒水和删除酒水的选项,可以通过简单的菜单进行操作。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)