c++ 一个简单的map,struct小应用

题目要求:

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;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值