如何编写更好,更清晰的银行帐户代码版本? [关闭]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何编写更好,更清晰的银行帐户代码版本? [关闭]相关的知识,希望对你有一定的参考价值。

我有这个小银行帐户代码来创建帐户并访问已经创建的帐户。这是非常程序化的,但我需要帮助知道如何做才能让它变得更好。就像我想要一个选项,在存款完成后退回并退出,并使用单独的键退出控制台应用程序。

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;
}
答案

虽然正如Paul所建议的那样,有关改进工作代码的问题属于codereview.stackexchange.com,但仍然是对你的问题的一个简短的架构答案1)创建一个BankAccount类以及类似这样的BankCustomer类:

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")
}

干杯,迪帕克

以上是关于如何编写更好,更清晰的银行帐户代码版本? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Playwright 关闭弹出消息框?

银行0925

如何读取扫描支票的支票号码和银行路由号码[关闭]

IPython 8.0大版本更新:Debug报错提示更清晰,加入自动代码补全

IPython 8.0大版本更新:Debug报错提示更清晰,加入自动代码补全

银行类题目