才学c++,昨晚突发奇想,做了一了Simple_ATM_drawmoney,后续功能持续开发中
,代码如下
//Bank.h
Bank的声明
#include<string> using namespace std; #ifndef Bank_h #define Bank_h class Bank { protected: string Bankid;//银行卡号 int Bankpassword;//密码 int drawmoney;//要取的钱 int savemoney;//要存入的钱 int totalmoney;//总共的钱 public: Bank(); ~Bank(); void setBankid(string);//输入账号 string getBankid()const;//获取账号 void setBankpassword(int);//输入密码 int getBankpassword()const;//获取密码 int getdrawmoney()const;//取钱 void setdrawmoney(int);//输入取钱 int getpaymoney()const;//存钱 void setpaymoney(int);//输入存钱 int gettotalmoney()const;//总钱 void print()const;//输出 }; #endif
//Bank.cpp
//Bank的实现
#include<string>
#include<iostream> //#include<stdexcept> #include"Bank.h" using namespace std; int total=1000; Bank::Bank() ://构造函数初始化 Bankid(""), Bankpassword(-1), drawmoney(0), savemoney(0), totalmoney(1000) {} Bank::~Bank()//析构函数 {} void Bank::setBankid(string id)//设置账号 { Bankid = id; } string Bank::getBankid()const//返回账号 { return Bankid; } void Bank::setBankpassword(int password)//设置密码 { Bankpassword = password; } int Bank::getBankpassword()const//返回密码 { return Bankpassword; } void Bank::setpaymoney(int money)//要存的钱 { savemoney = money; } int Bank::getpaymoney()const//存钱 { total = total + savemoney; return total; } void Bank::setdrawmoney(int money)//要取的钱 { drawmoney = money; } int Bank::getdrawmoney()const//取钱 { if (total < drawmoney) { cout << "余额不足" << endl; return 0; } else total = total - drawmoney; return total; //throw exception(); } int Bank::gettotalmoney()const { return total ; } void Bank::print()const { cout << "余额剩余" << gettotalmoney() << endl; }
//主函数 #include<iostream> #include<Windows.h> #include"Bank.h" #include<string> using namespace std; int Print();//打印主界面 void printpaymoney(Bank *b);//存钱界面 void printdrawmoney(Bank *b);//取钱界面 int main() { Bank *p = new Bank; cout << "请输入银行卡号" << endl; string id; cin >> id; p->setBankid(id); //cout << p->getBankid()<< endl; cout << "请输入密码" << endl; int password; cin >> password; p->setBankpassword(password); cout << "输入正确,正在进入主界面。。。。" << endl; Sleep(500); bool done = true; while (done) { int section = Print(); switch (section) { case 1: p->print(); break; case 2: printpaymoney(p); cout << "存钱成功!您当前余额为"; cout << p->getpaymoney() << endl; break; case 3: printdrawmoney(p); cout << "您当前余额"; cout << p->getdrawmoney()<< endl; break; case 0: done = false; break; default: cout << "unkown command!" << endl; } } return 0; } int Print() { int section; cout << "1)查询当前余额 " << endl; cout << "2)存钱" << endl; cout << "3)取钱" << endl; cout << "0)退出" << endl; cin >> section; return section; } void printdrawmoney(Bank *b)//取钱界面 { cout << "请输入您要取钱的金额" << endl; int money; cin >> money; b->setdrawmoney(money); } void printpaymoney(Bank *b)//存钱界面 { cout << "请输入您要存入的金额" << endl; int money; cin >> money; b->setpaymoney(money); }