【问题标题】:How can I write a better and cleaner version of my bank account code? [closed]如何编写更好、更简洁的银行账户代码版本? [关闭]
【发布时间】:2023-04-04 06:26:01
【问题描述】:

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

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

【问题讨论】:

标签: c++ visual-studio c++11


【解决方案1】:

尽管正如 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 对象。假设这是银行经理执行此操作以服务不同的银行客户。

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

干杯, 迪帕克

【讨论】:

    猜你喜欢
    • 2018-06-30
    • 2010-09-13
    • 2012-05-08
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    相关资源
    最近更新 更多