为我的银行项目设置支票账户和储蓄账户的UI

UI for Setting up a Checking account and Savings account for my bank project

本文关键字:UI 我的 项目 设置      更新时间:2023-10-16

我想显示一个界面,询问用户是否要使用支票或储蓄账户,然后当他们选择时,将其带到我的通用显示菜单。但我不太确定如何设置。我被告知要创建Account对象并使用指针,但正如你所看到的,我被卡住了。如果有人能告诉我如何做到这一点,我将不胜感激

这是我的代码:

#ifndef ACCOUNT_H 
#define ACCOUNT_H 
class Account 
{ 
private: 
  double balance; // Account balance 
  double interestRate; // Interest rate for the period 
  double interest; // Interest earned for the period 
  int transactions; // Number of transactions 
public: 
  Account(double iRate = 0.045, double bal = 0) 
  { balance = bal; 
    interestRate = iRate; 
    interest = 0; 
    transactions = 0; } 
  void setInterestRate(double iRate) 
  { interestRate = iRate; } 
  void makeDeposit(double amount) 
  { balance += amount; transactions++; } 
  bool withdraw(double amount); // Defined in Account.cpp 
  void calcInterest() 
  { interest = balance * interestRate; balance += interest; } 
  double getInterestRate() const 
  { return interestRate; }
  double getBalance() const 
  { return balance; } 
  double getInterest() const 
  { return interest; }     
  int getTransactions() const 
  { return transactions; } 
}; 
#endif

int main() 
{ 
  Account Savings;
  Account Checkings;
  Account *ptr;
  Account savings; // Savings account object 
  char choice; // Menu selection 
  // Set numeric output formatting. 
  cout << fixed << showpoint << setprecision(2); 
  do 
  { 
    // Display the menu and get a valid selection. 
    displayMenu(); 
    cin >> choice; 
    while (toupper(choice) < 'A' || toupper(choice) > 'G') 
    { 
      cout << "Please make a choice in the range " << "of A through G:"; 
      cin >> choice; 
    } 
    // Process the user's menu selection. 
    switch(choice) 
    { 
      case 'a': 
      case 'A': cout << "The current balance is $"; 
        cout << savings.getBalance() << endl; 
        break; 
      case 'b': 
      case 'B': cout << "There have been "; 
        cout << savings.getTransactions() << " transactions.n";
        break; 
      case 'c': 
      case 'C': cout << "Interest earned for this period: $"; 
        cout << savings.getInterest() << endl; 
        break; 
      case 'd': 
      case 'D': makeDeposit(savings); 
        break; 
      case 'e': 
      case 'E': withdraw(savings); 
        break; 
      case 'f': 
      case 'F': savings.calcInterest(); 
        cout << "Interest added.n"; 
    }
  } while (toupper(choice) != 'G'); 
return 0; 
} 
void displayMenu() 
{ 
  cout << "n Welcome to The Bank n"; 
  cout << "-----------------------------------------n"; 
  cout << "A) Display the account balancen"; 
  cout << "B) Display the number of transactionsn"; 
  cout << "C) Display interest earned for this periodn"; 
  cout << "D) Make a depositn"; 
  cout << "E) Make a withdrawaln"; 
  cout << "F) Add interest for this periodn"; 
  cout << "G) Exit the programnn"; 
  cout << "Enter your choice: "; 
} 
void makeDeposit(Account *acct)
{
  double dollars; 
  cout << "Enter the amount of the deposit: "; 
  cin >> dollars;
  cin.ignore(); 
  acct.makeDeposit(dollars);
}
void withdraw(Account *acct)
{
  double dollars;
  cout << "Enter the amount of the withdrawal: "; 
  cin >> dollars; 
  cin.ignore(); 
  if (!acct.withdraw(dollars)) 
    cout << "ERROR: Withdrawal amount too large.nn";
}

试试这样的东西:

char displayAccountSelectionMenu();
char displayAccountActionMenu();
void makeDeposit(Account *acct);
void withdraw(Account *acct);
int main() 
{ 
    Account Savings;
    Account Checkings;
    Account *acct = NULL;
    char choice; // Menu selection 
    do
    {
        // Display the menu and get a valid selection. 
        choice = displayAccountSelectionMenu();
        // Process the user's menu selection. 
        switch (choice)
        {
            case 'A':
                acct = &Savings;
                break;
             case 'B':
                acct = &Checkings;
                break;
             case 'C':
                return 0;
        }
        // Set numeric output formatting. 
        cout << fixed << showpoint << setprecision(2); 
        do 
        { 
            // Display the menu and get a valid selection. 
            choice = displayAccountActionMenu(); 
            // Process the user's menu selection. 
            switch (choice) 
            { 
                case 'A':
                    cout << "The current balance is $"
                         << acct->getBalance() << endl; 
                    break; 
                case 'B':
                    cout << "There have been "
                         << acct->getTransactions() 
                         << " transactions." << endl;
                    break; 
                case 'C':
                    cout << "Interest earned for this period: $"
                         << acct->getInterest() << endl; 
                    break; 
                case 'D':
                    makeDeposit(acct); 
                    break; 
                case 'E':
                    withdraw(acct); 
                    break; 
                case 'F':
                    acct->calcInterest(); 
                    cout << "Interest added." << endl; 
                    break;
                case 'G':
                    break;
                case 'H':
                    return 0;
            } 
        }
        while (choice != 'G'); 
    }
    while (true);
    return 0; 
} 
char displayAccountSelectionMenu()
{
    char choice;
    cout << "n Welcome to The Bank n"; 
    cout << "-----------------------------------------n"; 
    cout << "Select an account:n";
    cout << "A) Savingsn"; 
    cout << "B) Checkingn"; 
    cout << "C) Exit the programnn"; 
    cout << "Enter your choice: "; 
    cin >> choice; 
    choice = toupper(choice);
    while ((choice < 'A') || (choice > 'C'))
    { 
        cout << "Please make a choice in the range of A through C:"; 
        cin >> choice; 
        choice = toupper(choice);
    } 
    return choice;
}
char displayAccountActionMenu() 
{ 
    char choice;
    cout << "n Welcome to The Bank n"; 
    cout << "-----------------------------------------n"; 
    cout << "Select an action:n";
    cout << "A) Display the account balancen"; 
    cout << "B) Display the number of transactionsn"; 
    cout << "C) Display interest earned for this periodn"; 
    cout << "D) Make a depositn"; 
    cout << "E) Make a withdrawaln"; 
    cout << "F) Add interest for this periodn"; 
    cout << "G) Select a different accountn"; 
    cout << "H) Exit the programnn"; 
    cout << "Enter your choice: "; 
    cin >> choice; 
    choice = toupper(choice);
    while ((choice < 'A') || (choice > 'H'))
    { 
        cout << "Please make a choice in the range of A through H:"; 
        cin >> choice; 
        choice = toupper(choice);
    } 
    return choice;
} 
void makeDeposit(Account *acct)
{
    //...
}
void withdraw(Account *acct)
{
    // ...
}