我有这个小的银行帐户代码,可以用来建立帐户并访问已经建立的帐户。这是相当程序化的程序,但是我需要知道如何做才能使其变得更好。就像我想要一个在存入和提取完后循环返回的选项以及退出控制台应用程序的单独键一样。
int main(){
//MIKE BANK LTD
string name;
string defacctNum = "123456";
string acctNum;
int defacctPin = 1357;
int acctPin;
double acctBal;
double defacctBal = 100.59;
int withdraw;
int deposit;
int check;
int yo;
cout << "|----------------------------------------------------------------------------------|" << endl;
cout << "|Hello customer, welcome to Obadan Bank. Do you already have an account with us?|" << endl;
cout << "|----------------------------------------------------------------------------------|" << endl;
cout << "|----------------------------------------------------------------------------------|" << endl;
cout << "|Enter 1 if you have an account or 2 if you want to create a new one.|" << endl;
cin >> check;
if (check == 1) {
cout << "Enter account number: ";
cin >> acctNum;
while (acctNum != defacctNum) {
cout << "Wrong account number not recognized try again: ";
cin >> acctNum;
}
if (acctNum == defacctNum) {
cout << "Enter your pin: ";
cin >> acctPin;
while (acctPin != defacctPin) {
cout << "Wrong pin please enter it again: ";
cin >> acctPin;
}
if (acctPin == defacctPin) {
cout << "You have $" << defacctBal << " in you account." << endl;
int check2;
cout << "Would you like to deposit or withdraw? Press 1 to deposit, 2 to withdraw or any other key to exit." << endl;
cin >> check2;
if (check2 == 1) {
cout << "Enter the amount you want to deposit.: " << endl;
cin >> deposit;
cout << "You deposited $" << deposit << ".";
defacctBal += deposit;
cout << "Your account balance is now $" << defacctBal << "." << endl;
}
else if (check2 == 2) {
cout << "Enter amount you want to withdraw." << endl;
cin >> withdraw;
while (withdraw > defacctBal) {
cout << "You can't withdraw more than you have!" << endl;
cin >> withdraw;
}
if (withdraw < defacctBal) {
defacctBal -= withdraw;
cout << "You withdrew $" << withdraw << ", now you have $" << defacctBal << endl;
}
}
}
}
}
else if (check == 2) {
int acctNums;
cout << "Enter your name: ";
cin >> name;
cout << "Welcome to Obadan Bank, " << name << ", we would be generating an account number for you.";
acctNums = rand() % 999999 + 100000;
cout << "..l o a d i n g..." << endl;
cout << "You new account number is: " << acctNums << ". Please enter your new pin: " << endl;
cin >> acctPin;
cout << "Confirm pin again." << endl;
int pinConf;
cin >> pinConf;
while (acctPin != pinConf) {
cout << "Please make sure both pins match!" << endl;
cin >> pinConf;
}
if (pinConf == acctPin) {
cout << "Welcome to your new account, " << name << ". Would you like to start off with a deposit? Hit 1 to deposit or any other key to exit." << endl;
int conf;
cin >> conf;
if (conf == 1) {
cout << "Enter your deposit amount." << endl;
cin >> deposit;
cout << "Great! You deposited $" << deposit << "." << endl;
}
}
}
cin >> yo;
return 0;
}
答案 0 :(得分:1)
尽管正如Paul所建议的那样,有关改进工作代码的问题属于codereview.stackexchange.com,但这里仍然是您的问题的简短体系结构答案 1)与BankCustomer类一起创建一个BankAccount类,如下所示:
class BankCustomer
{
//Member variables representing state of the object
BankAccount bankAcct;
std::string customerName;
.... //All other customer specific details in form of member variables
//Member functions for performing operations on this object
}
class BankAccount
{
//Member variables representing "state"
//Member functions to perform operations like:
"CreateAccount()"
"DepositToExistingAccount(int accountNumber)"
"WithdrawFromExistingAccount(int accountNumber)"
};
在客户应用程序(例如main.cpp)中,在do-while循环中创建BankCustomer对象。想象一下,这是银行经理执行此操作以服务于不同的BankCustomers。
int main()
{
std::string option;
cin>>option;
do
{
//Here ask the different choices like
1. New User Creation
2. Operations on Existing User:
a) Deposit
b) Withdraw
3. Exit
}while(option != "Exit")
}
干杯, 迪帕克