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

问题描述 投票:-1回答:1

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

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个回答
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对象。想象一下,这是银行经理执行此操作以服务不同的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")
}

干杯,迪帕克

© www.soinside.com 2019 - 2024. All rights reserved.