写一个C++饭卡管理系统,要求面向对象,数据成员私有,文件输入输出,循环显示菜单
时间: 2024-05-10 20:18:01 浏览: 119
C++写的餐厅管理系统(含源代码和运行程序)
3星 · 编辑精心推荐
以下是一个简单的饭卡管理系统,使用 C++ 实现,要求面向对象,数据成员私有,文件输入输出,循环显示菜单。
```cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
class Card {
private:
int id;
string name;
double balance;
public:
Card(int id, string name, double balance) : id(id), name(name), balance(balance) {}
int getId() const { return id; }
string getName() const { return name; }
double getBalance() const { return balance; }
void setBalance(double new_balance) { balance = new_balance; }
};
class CardManager {
private:
vector<Card> cards;
public:
void loadFromFile(string filename) {
ifstream infile(filename);
int id;
string name;
double balance;
while (infile >> id >> name >> balance) {
cards.push_back(Card(id, name, balance));
}
infile.close();
}
void saveToFile(string filename) {
ofstream outfile(filename);
for (auto& card : cards) {
outfile << card.getId() << " " << card.getName() << " " << card.getBalance() << endl;
}
outfile.close();
}
void addCard(Card card) {
cards.push_back(card);
}
void removeCard(int id) {
for (auto it = cards.begin(); it != cards.end(); ++it) {
if (it->getId() == id) {
cards.erase(it);
break;
}
}
}
Card* findCard(int id) {
for (auto& card : cards) {
if (card.getId() == id) {
return &card;
}
}
return nullptr;
}
void showMenu() {
int choice;
do {
cout << "1. Add card" << endl;
cout << "2. Remove card" << endl;
cout << "3. Check balance" << endl;
cout << "4. Add balance" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addCard(getNewCard());
break;
case 2:
removeCard(getCardId());
break;
case 3:
checkBalance(getCardId());
break;
case 4:
addBalance(getCardId());
break;
case 5:
break;
default:
cout << "Invalid choice" << endl;
}
} while (choice != 5);
}
private:
Card getNewCard() {
int id;
string name;
double balance;
cout << "Enter card id: ";
cin >> id;
cout << "Enter card name: ";
cin >> name;
cout << "Enter initial balance: ";
cin >> balance;
return Card(id, name, balance);
}
int getCardId() {
int id;
cout << "Enter card id: ";
cin >> id;
return id;
}
void checkBalance(int id) {
Card* card = findCard(id);
if (card == nullptr) {
cout << "Card not found" << endl;
} else {
cout << "Balance: " << card->getBalance() << endl;
}
}
void addBalance(int id) {
Card* card = findCard(id);
if (card == nullptr) {
cout << "Card not found" << endl;
} else {
double amount;
cout << "Enter amount to add: ";
cin >> amount;
card->setBalance(card->getBalance() + amount);
cout << "New balance: " << card->getBalance() << endl;
}
}
};
int main() {
CardManager manager;
manager.loadFromFile("cards.txt");
manager.showMenu();
manager.saveToFile("cards.txt");
return 0;
}
```
在这个程序中,`Card` 类表示一张饭卡,包含 id、name 和 balance 三个数据成员,分别表示卡号、姓名和余额。`CardManager` 类是饭卡管理器,包含了添加、删除、查看余额和充值等操作。它内部维护了一个 vector,存储所有的饭卡。`loadFromFile` 和 `saveToFile` 方法分别用于从文件中读取饭卡信息和将饭卡信息写入文件。`showMenu` 方法是程序入口,循环显示菜单,根据用户的选择调用不同的方法。
你可以根据自己的需求修改这个程序,例如添加更多的操作,修改文件格式等。
阅读全文