[设计] C++课程设计-ATM (纪念大一)

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
#include <cstdlib>
#include <cstdio>

using namespace std;

void shaohou() {
    for(int i = 0; i < 6; i++) {
        printf(".");
        for(int j = 0; j < 40000000; j++) {
            ;
        }
    }
}
class AccountItem {
private:
	string account;      // 账号
	string passwd;       // 密码
	string name;         // 姓名
    double pay;          // 存款余额
public:
    AccountItem() : account(""), passwd(""), name(""), pay(0) {}
	AccountItem(string m_account, string m_passwd, string m_name, const double m_pay) {
        account = m_account;
        passwd = m_passwd;
        name = m_name;
        pay = m_pay;
	}
	void display() {           // 显示对象
        cout << "账号 :\n" << account << endl;
        cout << "密码 :\n" << passwd << endl;
        cout << "姓名 :\n" << name << endl;
        cout << "余额 :\n" << pay << endl;
	}

	void write(ofstream& s);   // 修改账目
	void read(ifstream& s);    // 读入账目数据

	bool checkAccount(string m_account) {    // 检测信号
        if(account == m_account) return true;
        return false;
    }
	string getAccount() { return account; }   // 取账号
	string getPasswd() { return passwd; }     // 取密码
	string getName() { return name; }         // 取帐号姓名
	double getPay() { return pay; }           // 得到余额

	void subPay(double m_pay) { pay -= m_pay; }  // 取款后-修改余额
    void addPay(double m_pay) { pay += m_pay; }  // 存款后-修改存款余额
    void modifyPasswd(string m_passwd) { passwd = m_passwd; }   // 修改密码

    void saveNew();      // 开户用---存入新的数据成员
    void addPerson();    // 此函数用于增加新的数据成员

	bool isNull() {
	    if(account == "") return true;
	    return false;
    }
};
void AccountItem::write(ofstream& s) {
    s << account << endl;
    s << passwd << endl;
    s << name << endl;
    s << pay << endl;
}
void AccountItem::read(ifstream& s) {  //从输入流读入数据
    s >> account;
    s >> passwd;
    s >> name;
    s >> pay;
};
void AccountItem::saveNew() {             //--------往里面写用的函数,存入新的成员-----------
    ofstream outputStream("Account.in", ios::app);
    if (!outputStream) {
        cout << "\n对不起!!!打开文件失败!!!!\n" << endl;
        cout << "按 Enter 键继续" << endl;
        cin.get();
        return;
    }
    outputStream << account << "\n" << passwd << "\n" << name << "\n" << pay <<endl;
    outputStream.close();
}
void AccountItem::addPerson() {
    cout << "\n根据下面提示输入新开户人信息\n" << endl;
    cout << "账号 :";
    cin >> account; fflush(stdin); // stdin是默认的输入流文件,对应输入缓冲区,fflush(stdin)的作用就是清空输入缓冲区,避免缓冲区内残存读取函数无法取走的内容!
    cout << "密码 :";
    cin >> passwd; fflush(stdin);
    cout << "姓名 :";
    cin >> name; fflush(stdin);
    cout << "开户金额 : ";
    cin >> pay; fflush(stdin);
    saveNew();
    cout << "\n\n\t开户成功,您的账号是 : " << account;
	cout << "请牢记!" << endl;
    cout << "\n\n\t您的可用余额为 : " << pay << endl;
    cout << "\n\n新的账户信息已经保存好!!!!!\n" << endl;
    cout << "按 Enter 键继续" << endl;
    cin.get();   //直观的讲就是显示:”按任意键继续。。。“
}
///* 以下 Accountbook 为帐号表单类。*///
///* 新增加一个新的Accountbook类    *///
///* 把修改后的账目存储到文件        *///
class Accountbook {
private :
    vector<AccountItem> v_Account;   // Account 对象数组
    size_t accountCount;       // 数组中的对象个数
public:
	Accountbook();       //构造函数
	size_t loadAccountbook(ifstream& inputStream);     // 读入账目本

	void storeAccountbook(ofstream& outputStream);     // 为修改账目本写文件

	AccountItem findItem(string m_account);       // 在账目本中一个一个查询,看参数(No-形参)是否与(类似李雷这种)对象的账号匹配

	bool updateItem(AccountItem item);     // 按所给对象修改对象内容,但是账号不能改哦! 修改账目中的一个记录
};
Accountbook::Accountbook() {
	v_Account = vector<AccountItem> (100);
	accountCount = 0;
}
size_t Accountbook::loadAccountbook(ifstream& inputStream) {
	size_t i = 0;           /// 从文件中读入i个对象
	while(true) {
	    v_Account[i].read(inputStream);
	    ++i;
        if(inputStream.fail()) break;
	}
	inputStream.close();         // 关闭文件
	accountCount = i;
	return accountCount;         // 返回读入对象的个数
}
void Accountbook::storeAccountbook(ofstream& outputStream) {
	for(size_t i = 0; i < accountCount; ++i)   // 将对象组通过流输出到文件
        v_Account[i].write(outputStream);
	outputStream.close();
}
// 按给定账号在账目本中查询
AccountItem Accountbook::findItem(string m_account) {
	for(size_t i = 0; i < accountCount; ++i) {
		if(v_Account[i].getAccount() == m_account)
            return v_Account[i];
	}
	return AccountItem();   // 若未查询到,返回一个空对象
}
//按所给对象修改对象内容,但是账号不能改哦
bool Accountbook::updateItem(AccountItem item) {
	string number = item.getAccount();
	//按账号在账目本中查询账号与所给对象相同的对象
	for(size_t i = 0; i < accountCount; i++) {
		if(v_Account[i].getAccount() == number) {
			v_Account[i] = item;
			return true;
		}
	}
	return false;
}
void g_login() {
	ifstream inputStream("Account.in");

	string accNo;

	Accountbook myAccountbook;   // myAccountbook 为 Accountbook 的一个对象
	AccountItem myItem;          // myItem 为 AccountItem 的一个对象

    myAccountbook.loadAccountbook(inputStream);   // 读入账目本
	cout << "  请插卡(即输入账号):"; cin >> accNo;

	myItem = myAccountbook.findItem(accNo);    // 在账目本中查询
	if(myItem.isNull()) {

		cout << " ★★  账号不存在!\n\n\n\t☆★\t立宾提醒您重新操作" << endl;
		return;
	}
	//读入密码

	cout << "  请输入您的个人密码:";
    char *pw = new char[20];
	cin >> pw;
	//判断密码是否正确
	if(myItem.getPasswd() != pw) {

		cout<<"\t您输入的密码错误!\n\n\n\t☆★\t立宾提醒您重新操作"<<endl;
		return;
	}

	double oldPay = myItem.getPay(); //取原存款余额
	char w = '#';
    do {
        cout << "请选择交易密码 : " << endl;
        cout << "\n\tG (取钱)" << endl;
        cout << "\n\tE (存款)" << endl;
        cout << "\n\tC (查询余额)" << endl;
        cout << "\n\tX (修改密码)" << endl;
        cout << "\n\tZ (转账)" << endl;
        cout << "\n\tT (退卡)" << endl;
        string sel;
        cin >> sel; /// 判断用户需要做什么!
        if(sel == "C" || sel == "c") { //查询存款余额
            cout << "\n\n\t余额是 : " << myItem.getPay() << endl;
            shaohou();
        }
        else if(sel == "X" || sel == "x") { //修改密码
            cout<<"\t请输入原密码:";
            string pwd;
            cin >> pwd;
            if(myItem.getPasswd() != pwd) {
                cout << "您输入的密码错误!\n\n\n\t☆★\t立宾提醒您重新操作";
            }
            else {
                cout << "\n\n\t请输入新密码:";
                cin >> pwd;
                string rpwd;
                cout << "\n\n\t请再输入一次新密码:";
                cin >> rpwd;
                if(rpwd != pwd) {
                    cout << "\n\n\t你输入的两次密码不相同, 请重新操作!" << endl;
                }
                else {
                    myItem.modifyPasswd(rpwd);
                    myAccountbook.updateItem(myItem);
                    cout << "\n\n\t密码修改成功,请牢记!" << endl;
                }
                /// 修改后的密码写到文件中
                ofstream outputStream("Account.in");
                myAccountbook.storeAccountbook(outputStream);
            }
        }
        else if(sel == "T"|| sel == "t") {//退卡

            cout << " \n\n\n        ★-★-☆-☆-★-★-☆-☆-★-★-☆-☆-★-★" << endl;
            cout << "\n\n\n\t  感谢你对本银行的支持,欢迎下次光临!"<< endl;
            cout << "   \n\n       ★-★-☆-☆-★-★-☆-☆-★-★-☆-☆-★-★" << endl;
            cout << "\n\n\t☆★ 请取卡 ......" << endl;
            cout << "\n\n    ★-★-☆-☆-★-★-☆-☆-★-★-☆-☆-★-★\n";
            exit(0);
        }
        else if(sel == "E" || sel == "e") { /// 存款
            cout << "请选择存款的数量 : " << endl;
            cout << "\n\t\t1 (存100)" << endl;
            cout << "\n\t\t2 (存200)" << endl;
            cout << "\n\t\t5 (存500)" << endl;
            cout << "\n\t\tA (存1000)" << endl;
            cout << "\n\t\tB (存2000)" << endl;
            char count;
            double ap;
            cin >> count;
            if(count == '1') ap = 100;
            else if(count == '2') ap = 200;
            else if(count == '5') ap = 500;
            else if(count == 'A') ap = 1000;
            else if(count == 'B') ap = 2000;
            else { cout << "\t\t选择错误" << endl; return; }
            cout<<"\n\n     ★☆★☆请按您选择的存款数量,塞入人民币☆★☆★ ";
            for(size_t i = 0; i < 4; ++i) {
                shaohou();
                shaohou();
            }
            /// 修改对象的存款余额
            myItem.addPay(ap);
            myAccountbook.updateItem(myItem);
            ofstream outputStream("Account.in");
            myAccountbook.storeAccountbook(outputStream);
            cout << "\n\n\n    ★☆★☆存款成功。您存入了" << ap << "元. ☆★☆★ ";
            for(size_t i = 0; i < 5; ++i) {
                shaohou();
            }
        }
        else if(sel == "Z" || sel == "z") { // 转账
            cout << "\t请输入要转到账户的账号:";
            string otherAccount;
            cin >> otherAccount;
            AccountItem otherItem = myAccountbook.findItem(otherAccount);
            if(otherItem.isNull()) {
                cout << " ★★  账号不存在!\n\n\n\t☆★\t立宾提醒您重新操作" << endl;
                return;
            }
            else {
                cout << "\t请输入转账金额 : ";
                double zhuane;
                cin >> zhuane;
                oldPay = myItem.getPay();
                if(oldPay < zhuane) {
                    cout << "\n\t存款余额不够!\n\n\n\t☆★\t立宾提醒您" << endl;
                }
                else { // 修改对象的存款余额
                    myItem.subPay(zhuane);
                    myAccountbook.updateItem(myItem); //将修改后的账目本写到文件中
                    ofstream outputStream1("Account.in");
                    myAccountbook.storeAccountbook(outputStream1);
                    outputStream1.close();
                    otherItem.addPay(zhuane);         /// 修改账目本
                    myAccountbook.updateItem(otherItem); /// 将修改后的账目本写到文件中
                    ofstream outputStream2("Account.in");
                    myAccountbook.storeAccountbook(outputStream2);
                    cout << "转账成功!\n\n\n\t☆★\t立宾提醒您" << endl;
                    outputStream2.close();
                    shaohou();
                    shaohou();
                    shaohou();

                }
            }
        }
        else if(sel == "G" || sel == "g") {// 取款
            cout << "\t请选择取钱的数量:" << endl;
            cout << "\n\t\t1 (取100)" << endl;
            cout << "\n\t\t2 (取200)" << endl;
            cout << "\n\t\t5 (取500)" << endl;
            cout << "\n\t\tA (取1000)" << endl;
            cout << "\n\t\tB (取2000)" << endl;
            char count;
            double ap;
            cin >> count;
            if(count == '1') ap = 100.;
            else if(count == '2') ap = 200.;
            else if(count == '5') ap = 500.;
            else if(count == 'A') ap = 1000.;
            else if(count == 'B') ap = 2000.;
            else {
                cout << "选择错误" << endl; return;
            }
            //判断存款余额是否够
            if(oldPay < ap) {
                cout << "\n\t存款余额不够!" << endl;
            }
            else { //修改对象的存款余额
                myItem.subPay(ap);
                //修改账目本
                myAccountbook.updateItem(myItem);

                cout << "\t请取钱!" << endl;
                //将修改后的账目本写到文件中
                ofstream outputStream("Account.in");
                myAccountbook.storeAccountbook(outputStream);
            }
        }
        shaohou();
        shaohou();

        cout << "\n\t你想继续进行其他操作吗?(y/n)" << endl;
        cin >> w;

    } while (w == 'y');
}
int main() {
    AccountItem tempAccount;
    while(true) {
        char xz;
        cout << "\n\n\t欢迎使用立宾国际ATM系统\n";
        cout << "\n\n\t请选择您要执行的操作:\n";
        cout << "\n\n\tA.开户服务\n";
        cout << "\n\n\tB.登录服务\n";
        cout << "\n\n\tC.退出。\n";
        cout << "\n\n\t请选择:";
        scanf("%c", &xz);
        switch(xz) {
            case 'a' :
            case 'A' : {
                cout << "\n\n\t【开户服务】"; ///
                tempAccount.addPerson();   /// 添加新人
                break;
            }
            case 'b' :
            case 'B' : {
                g_login();
                break;
            }
            case 'c' :
            case 'C' : {
                cout << "   \n\n\n     ★-★-☆-☆-★-★-☆-☆-★-★-☆-☆-★-★" << endl;
                cout << "\n\t★★☆ 感谢您对本本银行的支持,欢迎下次光临! ☆★★\n";
                exit(0);
            }
            default : {
                cout << "\n\t您的选择错误!\n\n\n\t☆★\t立宾提醒您重新选择!" << endl;
            }
        }
        cout << "\n\n\t请选择您要执行的操作:\n";
    }
    return 0;
}

--------- Account.in ----------

001
lilei
李雷
1000
002
hanmei
韩梅梅
1000
003
wangwenhao
王文豪
3900
004
maoxu
王冒旭
4000
005
hongwu
于洪武
2000
006
xiaofei
王小飞
900
007
zhangli
张丽丽
1000

// ---------------其他美观--------------

void AccountItem::welcome() {
    for(int i = 0; i < 2; i++) {             //可以改变i的值,以美观
        cout << "\n\n\n\n\n\n\n\n";
        cout << "\t\t\t ●☆★◇◆○●☆★◇◆○●☆★◇◆○\n";
        cout << "\t\t\t◎        欢迎使用立宾国际ATM系统     ●\n";
        cout << "\t\t\t ●☆★◇◆○●☆★◇◆○●☆★◇◆○\n";
        cout << "\n\n\t\t      ★希望立宾国际ATM系统可以时刻给您带来方便!★";
        cout << "\n\n\n\n\t\t\t\t  陈立宾将竭诚为您提供优质的服务";
        shaohou();
        system("cls");
        cout << "\n\n\n\n\n\n\n\n";
        cout << "\t\t\t ○★☆◆◇●○★☆◆◇●○★☆◆◇●\n";
        cout << "\t\t\t●       欢迎使用立宾国际ATM系统      ◎\n";
        cout << "\t\t\t ○★☆◆◇●○★☆◆◇●○★☆◆◇● \n";
        cout << "\n\n\t\t      ☆希望立宾国际ATM系统可以时刻给您带来方便!☆";
        cout << "\n\n\n\n\t\t\t\t 陈立宾将竭诚为您提供优质的服务";
        shaohou();
        system("cls");
    }
};
posted @ 2013-03-10 16:23  小尼人00  阅读(208)  评论(0编辑  收藏  举报