//Chan Tak Sang //50929220 #include#include #include #include using namespace std; class bankaccount { friend int loginchecking(string, string y, bankaccount z[10]); //friend function prototype(account number, password, and list of account number) friend void balancechecking(int, bankaccount z[10]); //friend function prototype(integer for call by reference where the balance should be checked, list of account number) friend void withdraw(int, bankaccount z[10]); //friend function prototype(integer for call by reference where the withdraw is done, list of account number) friend void passwordreset(int, bankaccount z[10]); //friend function prototype(integer for call by reference where the pw should be reset, list of account number) friend void deposit(int, bankaccount z[10]); //friend function prototype(integer for call by reference where the deposit should be done, list of account number) friend void transfer(int, bankaccount z[10]); //friend function prototype(integer for call by reference , list of account number) private: string accountnumber; string password; double balance; public: bankaccount(); //default constructor }; void accountnogen(string &str); //function prototype, same as AccNumGen function but the function has been modified bankaccount::bankaccount() //default constructor { string x; balance = 0; password = "000000"; //by default every account number should have pw "000000" and $0 of balance accountnogen(x); accountnumber = x; //number of account depends on how the main function gives arugment } int loginchecking(string x, string y, bankaccount z[10]) { for (int i=0; i<10; i++) { if ((z[i].accountnumber == x) && (z[i].password == y)) return i; //doing a for loop scanning, x and y are string variable for call by reference what account number the user has input //return a value same as the use of bool(true) } return 999; //same as bool (false), but only use int instead } void accountnogen(string& str) { static int seed = 1200; char buffer[10]; string str1 = "300-475-"; string str2(itoa(seed++, buffer, 10)); str = str1 + str2; } //given function AccNumGen function definition, but function name has been modified void balancechecking(int record, bankaccount z[10]) { cout << "Balance:$" << z[record].balance; //z[record]is to find out what account number balance is checking } //function definition of balancechecking void withdraw(int record, bankaccount z[10]) { double wd; cout << "Withdraw:"; cin >> wd; if (wd > z[record].balance) { cout << "Insufficient fund"; //if user type an amount(wd) > his own account balance, result in this } else { z[record].balance -= wd; cout << "$" << wd << " has been withdrawn from your account"; //balance has been modified because amount of withdraw(wd) is less or equal to the user's balance } } void passwordreset(int record, bankaccount z[10]) { string oldpass, newpass; cout << "Old password:"; cin >> oldpass; cout << "New password:"; cin >> newpass; if (z[record].password == oldpass) { // checks input oldpass against original password set for account z[record].password = newpass; cout << "Your password has been reset"; } else { cout << "Invalid account number or password"; //old password is not user's default password } } void deposit(int record, bankaccount z[10]) { double dp; cout <<"Deposit:"; cin >> dp; z[record].balance += dp; //user's account balance will be modified cout << "$" << z[record].balance << " has been deposited into your account"; } void transfer(int record, bankaccount z[10]) { string transfertarget; double transferamount = 0; int transfer; bool check; cout << "Transfer to:"; cin >> transfertarget; cout << "Amount:"; cin >> transferamount; if (z[record].balance < transferamount) { cout << "Insufficient fund"; void close(); } // checks for sufficient fund else if (transfertarget == z[record].accountnumber) { cout << "You cannot transfer to your own account"; void close(); } //checks if transfers to customer's own account else { for (int i=0; i<10; i++) { if (z[i].accountnumber == transfertarget) { transfer = i; z[record].balance -= transferamount; //customer's balance will be changed z[transfer].balance += transferamount; //target's balance will be changed check = 1; } // checks for target account location in constructor } if (check) { cout << "$" << transferamount << " has been transferred to account " << transfertarget; } else { cout << "Invalid account number"; void close(); //if user types an invalid account number, result in this } } } int main() { bankaccount customer[10]; //a bankaccount data type array with 10 account number inside string accno; string pw; int record; //use for check the return value of function "loginchecking" is 999 or not, same as bool char optiontype; //what option the user will type while (true) //this while loop is dummy until the user type "q' in option //and "-" in account number, program will terminate { cout << "Enter account number:"; cin >> accno; if (accno == "-") { break; } cout << "Enter password:"; cin >> pw; record = loginchecking(accno, pw, customer); if (record != 999) //record should be equal to the return value of function "loginchecking" { cout << "Welcome " << accno; cout << "\nOption:"; cin >> optiontype; while (optiontype != 'q') { switch (optiontype) { case 'c' : balancechecking(record, customer); break; case 'r' : passwordreset(record, customer); break; case 'w' : withdraw(record, customer); break; case 'd' : deposit(record, customer); break; case 't' : transfer(record, customer); break; } //using switch to determine what function the program should use after user types in a option //repeating the looping until user type in q, go back to the first while looping cout << "\nOption:"; cin >> optiontype; } } else { cout << "Invalid account number or password" << endl; } } return 0; }
Text file Source (historic): geocities.com/hk/crazysang1223/cs2331
geocities.com/hk/crazysang1223geocities.com/hk
(to report bad content: archivehelp @ gmail)
|
|
|
|
|