c++ 一个简单的map,struct小应用
题目要求:Test Program(24-2-2012)BANKING SYSTEM PROJECTDescription: This C++ programs on BANKINGSYSTEM has account class with data members like account number. name, deposit,withdraw amount and typ
题目要求:
Test Program
(24-2-2012)
BANKING SYSTEM PROJECT
Description: This C++ programs on BANKINGSYSTEM has account class with data members like account number. name, deposit,withdraw amount and type of account. Customer data is read from file (Createyour own file format) and add them to customer class attributes. A customer candeposit and withdraw amount in his account. All the fields must be accessedthrough setters and getters.
Output should look like:
Main Menu
1. New Account
2. Deposit amount
3. WithDraw amount
4. Exit
Select your option: 1
%%%%%%%%%New Entry Account Form%%%%%%%%%
Enter the account number: 92837
Enter Account holder Name: pi*og2
Enter type of account: savings
Enter Initial amount: 500
Your account created successfully...
我的文件:
bankaccountInfo.txt
bank.cc
bank.hh
main.cc
bankaccountInfo.txt:
2012
John
savings
1000
3264
Lauri
savigns
3000
92837
prog2
savings
0
bank.hh:
-bash-4.1$ more bank.hh
#ifndef BANK_HH
#define BANK_HH
#include<iostream>
#include<fstream>
#include<string>
#include<map>
using namespace std;
const string filename = "bankaccountInfo.txt";
struct accountinfo {
accountinfo(): name(), type_account(), amount(0){}
accountinfo(string newname, string newtype, double newamount): name(newname), ty
pe_account(newtype), amount(newamount) {}
string name;
string type_account;
double amount;
};
class Bank {
public:
void newAccount();
void depositAmount();
void withdrawAmount();
Bank();
private:
int account_number;
void sendtoFile();
map <int,accountinfo> customers;
};
#endif
bank.cc:
#include "bank.hh"
Bank::Bank(void)
{
ifstream inFile(filename.c_str());
int key;
string temp_name, temp_type_account;
double temp_amount;
while(!inFile.eof()) {
inFile >> key >> temp_name >> temp_type_account >> temp_amount;
customers[key] = accountinfo(temp_name, temp_type_account, temp_amount);
}
inFile.close();
}
void Bank::sendtoFile(void)
{
ofstream outFile;
outFile.open(filename.c_str());
map <int,accountinfo>::iterator map_it;
for (map_it = customers.begin(); map_it != customers.end(); map_it++) {
outFile << map_it->first << endl;
outFile << map_it -> second.name << endl;
outFile << map_it -> second.type_account << endl;
outFile << map_it -> second.amount << endl;
}
outFile.close();
}
void Bank::newAccount(void)
{
int someone_account_number;
cout << "%%%%%%%%%New Entry Account Form%%%%%%%%%\n";
cout << "Enter the account number: ";
cin >> someone_account_number;
map <int,accountinfo>::iterator map_it;
map_it = customers.find(someone_account_number);
if(map_it != customers.end())
cout << "the account number already existed\nfail to create account\n";
else {
int key = someone_account_number;
string temp_name, temp_type_account;
double temp_amount;
cout << "\nEnter Account holder Name: ";
cin >> temp_name;
cout << "\nEnter type of account: ";
cin >> temp_type_account;
cout << "\nEnter Initial amount: ";
cin >> temp_amount;
cout << endl;
customers[key] = accountinfo(temp_name, temp_type_account, temp_amount);
sendtoFile();
cout << "Your account created successfully...\n";
}
}
void Bank::depositAmount(){
int key;
double deposit_amount;
cout << "%%%%%%%%%Deposit Amount%%%%%%%%%\n";
cout << "Enter the account number: ";
cin >> key;
map <int,accountinfo>::iterator map_it;
map_it = customers.find(key);
if(map_it == customers.end())
cout << "No such account\n";
else {
cout << "\nEnter deposit amount: ";
cin >> deposit_amount;
map_it->second.amount += deposit_amount;
sendtoFile();
cout << "deposit successfully now you have " << map_it->second.amount << " in the bank\n";
}
}
void Bank::withdrawAmount()
{
int key;
int loop_index = 1;
double withdraw_amount;
cout << "%%%%%%%%%Deposit Amount%%%%%%%%%\n";
cout << "Enter the account number: ";
cin >> key;
map <int,accountinfo>::iterator map_it;
map_it = customers.find(key);
if(map_it == customers.end())
cout << "No such account\n";
else {
do {
cout << "you have " << map_it->second.amount << " in the bank\nEnter withdraw amount: ";
cin >> withdraw_amount;
if (withdraw_amount <= map_it->second.amount) {
map_it->second.amount -= withdraw_amount;
sendtoFile();
cout << "\nwithdraw successfully now you have " << map_it->second.amount << " in the bank\n";
loop_index = 0;
}
else {
cout << "you don't have enough balance\n";
}
} while(loop_index);
}
}
main.cc:
-bash-4.1$ more main.cc
#include "bank.hh"
#include <cstdlib>
using namespace std;
int main(void)
{
int choice;
Bank accountOperation;
cout << "Main Menu\n1. New Account\n2. Deposit amount\n3. WithDraw amount\n4. Exit\n";
cout << "Select your opetion: ";
cin >> choice;
cout << endl;
switch(choice) {
case 1:
accountOperation.newAccount();
break;
case 2:
accountOperation.depositAmount();
break;
case 3:
accountOperation.withdrawAmount();
break;
case 4:
exit(EXIT_SUCCESS);
}
return 0;
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)