I have been studying c ++ for a while. A friend asked me to help her develop a simulated ATM program two days ago. So I spent one night writing it. In fact, this program is also very simple, however, I think it is useful for people who have just learned c ++, because it can help you better understand the true meaning of object-oriented programming ------- programming based on the real world model. This is the true purpose of learning c ++. It truly understands object-oriented programming!
//************************************// ** // * Function. h *//**//******************************** * *** # include <iostream. h> class consumer; class ATM // ATM {public: ATM (consumer & cn): cnsm (cn) {} void welcome (); // logon interface bool check_passwd (char n [], char pwd []); // check the password void change_passwd (); // modify the password void fetchmoney (); // void information () for withdrawal; // query information void exitATM (); // exit the system void functionshow (); // function interface void lock (); // lock private: int times; // record Password Input times consumer & cnsm ;}; class consumer // user {public: friend class ATM; consumer (char Name [], char Num [], float Money, char Password []); protected: char * get_name (); // get the name char * get_num (); // get the card number char * get_passwd (); // obtain the password float get_money (); // obtain the balance void set_passwd (char pwd []); // set the password void set_money (float m); // private: char passwd [8]; // User Password char name [20]; // user name char num [20]; float money ;};
|
//************************************// ** // * Member functions of the consumer class *//**//*********************** * ************ # include "function. h "# include <string. h> consumer: consumer (char Name [], char Num [], float Money, char Password []) {strcpy (name, Name); strcpy (num, Num ); money = Money; strcpy (passwd, Password);} float consumer: get_money () {return money;} char * consumer: get_name () {return name ;} char * consumer: get_num () {return num;} char * consumer: get_passwd () {return passwd;} void consumer: set_money (float m) {money-= m;} void consumer: set_passwd (char pwd []) {strcpy (passwd, pwd );}
|
//************************************// ** // * Member functions of the ATM class *//**//*********************** * ************ # include "function. h "# include <string. h> # include <stdlib. h> void ATM: welcome () {times = 0; cout <"$ welcome to the snow bank ATM! ~! "<Endl; char pwd [8], num [20], ch; int I = 0; do {I = 0; cout <endl <" Enter the card number: "; do {cin. get (ch); num [I ++] = ch;} while (ch! =); Num [I-1] =; I = 0; cout <"enter the password:"; do {cin. get (ch); pwd [I ++] = ch;} while (ch! =); Pwd [I-1] =; if (! Check_passwd (num, pwd) {cout <"the card number or password you entered is incorrect. Please enter" <endl; times ++ ;} else {functionshow () ;}} while (times <3); lock () ;}bool ATM: check_passwd (char num [], char pwd []) {if (strcmp (num, cnsm. get_num () = 0 & strcmp (pwd, cnsm. get_passwd () = 0) return true; else return false;} void ATM: functionshow () {int n; do {cout <endl <"Enter the corresponding operation serial number for the operation:" <endl; cout <"1) change Password "<endl <" 2) Withdrawal "<endl <" 3) query balance "<e Ndl <"4) Quit System" <endl; cout <"$> \"; cin> n; while (n <1 | n> 4) {cout <"enter the correct operation number! "<Endl; cout <" $> \ "; cin> n ;}switch (n) {case 1: change_passwd (); break; case 2: fetchmoney (); break; case 3: information (); break; case 4: exitATM (); break ;}while (true);} void ATM: change_passwd () {char pwd [8], repwd [8]; times = 0; do {cout <endl <"Enter the old password:"; cin> pwd; if (! Check_passwd (cnsm. get_num (), pwd) times ++; else break;} while (times <3); if (times = 3) lock (); int t = 0; do {cout <"Enter the new password:"; cin> pwd; cout <"Enter the new password again:"; cin> repwd; if (t = strcmp (pwd, repwd ))! = 0) cout <"the two passwords you entered are different. Please enter them again! "<Endl ;}while (t! = 0); cnsm. set_passwd (pwd); cout <"the password is successfully modified. Please remember! "<Endl;} void ATM: fetchmoney () {float m; char ch; do {cout <endl <" How much do you need: "<" $ >\" <endl; cin> m; while (m <= 0) {cout <"enter a correct number! "<Endl; cout <" $ >\"; cin> m ;}if (cnsm. get_money ()-m <0) {cout <"sorry, your balance is insufficient! "<Endl ;}else {cout <endl <" operation successful. Please collect the money! "<Endl; cnsm. set_money (m);} cout <"whether to continue this operation: (Y/N)" <endl; cout <"$> \"; cin> ch; while (ch! = N & ch! = N & ch! = Y & ch! = Y) {cout <"$ >\"; cin> ch ;}}while (ch = y | ch = Y);} void ATM :: information () {cout <"**********************************" <endl; cout <"*" <endl; cout <"* User name:" <cnsm. get_name () <endl; cout <"* card number:" <cnsm. get_num () <endl; cout <"* Balance:" <cnsm. get_money () <endl; cout <"*********************************" <<endl ;} void ATM: lock () {cout <endl <"sorry, your card has been confiscated due to incorrect operations! "<Endl; exit (1) ;}void ATM: exitATM () {cout <endl <" thank you for your support of this bank. Welcome to the next visit! "<Endl; cout <" pick up the card ...... "<Endl; exit (0 );}
|