简体   繁体   中英

Java OOP exercise

ATM Exercise

It asks the user to enter an id then checks the id if it's correct and displays the main menu which has four options.

The problem

The problem is that when the user chooses an option the program is over .. but what I need is that once the system starts it never stop until the user chooses 4 (which is the exit option ) and then starts all over again.

the question in the book

(Game: ATM machine) Use the Account class created in Programming Exercise 9.7 to simulate an ATM machine. Create ten accounts in an array with id 0, 1, . . . , 9, and initial balance $100. The system prompts the user to enter an id. If the id is entered incorrectly, ask the user to enter a correct id. Once an id is accepted, the main menu is displayed as shown in the sample run. You can enter a choice 1 for viewing the current balance, 2 for withdrawing money, 3 for depositing money, and 4 for exiting the main menu. Once you exit, the system will prompt for an id again. Thus, once the system starts, it will not stop.

import java.util.Scanner;

public class Account {

    private int id;
    private double balance;
    private static double annualIntrestRate;
    private java.util.Date dateCreated;

    public Account() {

    }

    public Account(int id) {
        this.id = id;
        balance = 100;
        dateCreated = new java.util.Date();

    }

    public void setAnnualIntrest(double intrest) {
        annualIntrestRate = intrest;

    }

    public void setBalance(double newBalance) {
        balance = newBalance;
    }

    public void setID(int newID) {
        id = newID;
    }

    public double getBalance() {
        return balance;
    }

    public int getID() {
        return id;
    }

    public java.util.Date getDate() {
        return dateCreated;
    }

    public static double getMonthlyIntrestRate() {

        return ((annualIntrestRate / 12) / 100);
    }

    public double getMonthlyIntrest() {
        return (balance * getMonthlyIntrestRate());
    }

    public double withDraw(double withDrawAmount) {
        return balance = balance - withDrawAmount;
    }

    public double deposit(double depositeAmount) {
        return balance = balance + depositeAmount;
    }


    public static void main(String[] args) {

        Account[] accounts = new Account[10];
        for (int i = 0; i < accounts.length; i++) {
            accounts[i] = new Account(i);
        }

        Scanner input = new Scanner(System.in);

        System.out.print("Enter an ID: ");
        int enteredID = input.nextInt();


        while (enteredID != accounts[enteredID].getID()) {

            System.out.print("enter correct id!");
            enteredID = input.nextInt();


        }
        if (enteredID == accounts[enteredID].getID()) {


            System.out.println("Main Menu: ");
            System.out.println("1: check balance");
            System.out.println("2: withdraw");
            System.out.println("3: deposit");
            System.out.println("4: exit");

            System.out.print("Enter a choice: ");
            int choice = input.nextInt();

            if (choice == 1) {
                System.out.println("The balance is: " + accounts[enteredID].getBalance());
            } else if (choice == 2) {
                System.out.print("Enter withdraw amount: ");
                int withdrawAmount = input.nextInt();
                accounts[enteredID].withDraw(withdrawAmount);
            } else if (choice == 3) {
                System.out.print("Enter deposit amount: ");
                int depositAmount = input.nextInt();
                accounts[enteredID].deposit(depositAmount);
            } else if (choice == 4) {
                System.out.print("Enter an ID: ");
                enteredID = input.nextInt();
            }
        }


    }

There are 2 fundamental parts of the question that you are missing in your program:

1) Once you exit, the system will prompt for an id again. Thus, once the system starts, it will not stop.

This means that once the real work in your main method starts (the first "enter an id"), there should be no way the program gets stopped internally; only through Ctrl-C if it is running in a terminal, or a "Stop" button if it is running in an IDE.

To implement this, you need an outer while loop:

while(true) {
    // the rest of the code goes in here
}

around the whole body of "work" in your main method.

2) Once an id is accepted, the main menu is displayed as shown in the sample run. You can enter a choice 1 for viewing the current balance, 2 for withdrawing money, 3 for depositing money, and 4 for exiting the main menu.

I am assuming this means that until option 4 is entered, the menu should keep reappearing after the user completes task 1, 2, or 3, meaning ANOTHER loop needs to be used for that part of the code, ie:

boolean shouldExit = false;
while (!shouldExit) {
    // print menu and do your logic checking when a value is entered.
    if (choice == 4) {
        shouldExit = true; // This will break out of this loop, and go back to the first "enter an id".
    }
}

Hopefully this helps guide you in the right direction.

Try below code:

 import java.util.Scanner;
 public class Account {
   private int id;
   private double balance;
   private static double annualInterestRate;
   private java.util.Date dateCreated;
   public Account() { }
   public Account(int id) {
       this.id = id;
       this.balance = 100;
       this.dateCreated = new java.util.Date();
   }

   public void setAnnualInterest(double interest) {
       annualInterestRate = interest;
   }

   public void setBalance(double newBalance) {
       balance = newBalance;
   }

   public void setID(int newID) {
       id = newID;
   }

   public double getBalance() {
       return balance;
   }

   public int getID() {
       return id;
   }

   public java.util.Date getDate() {
       return dateCreated;
   }

   public static double getMonthlyInterestRate() {
       return ((annualInterestRate / 12) / 100);
   }

   public double withDraw(double withDrawAmount) {
       return balance = balance - withDrawAmount;
   }

   public double deposit(double depositAmount) {
       return balance = balance + depositAmount;
   }


   public static void main(String[] args) {

      Account[] accounts = new Account[10];
      for (int i = 0; i < accounts.length; i++) {
         accounts[i] = new Account(i);
      }
      Scanner input = new Scanner(System.in);
      System.out.print("Enter an ID: ");
      int enteredID;
      do {
         enteredID = input.nextInt();
         if (enteredID <= 9 && enteredID >=0 && enteredID == accounts[enteredID].getID()) {
            System.out.println("Main Menu: ");
            System.out.println("1: check balance");
            System.out.println("2: withdraw");
            System.out.println("3: deposit");
            System.out.println("4: exit");
             do {
                System.out.print("Enter a choice: ");
                int choice = input.nextInt();
                input.nextLine();
                 if (choice == 1) {
                    System.out.println("The balance is: " + accounts[enteredID].getBalance());
                 } else if (choice == 2) {
                     System.out.print("Enter withdraw amount: ");
                     int withdrawAmount = input.nextInt();
                     accounts[enteredID].withDraw(withdrawAmount);
                 } else if (choice == 3) {
                     System.out.print("Enter deposit amount: ");
                     int depositAmount = input.nextInt();
                     accounts[enteredID].deposit(depositAmount);
                 } else if (choice == 4) {
                     System.out.println("Exit");
                     System.out.println("Enter an ID");
                     break;
                 }
              } while (true);
           }
           else{
               System.out.print("enter correct id!");
           }

       }while(true);
   }
}

I recommend to use switch statement instead of if-else ladder

                  // same as above code
                   System.out.print("Enter a choice: ");
                   int choice = input.nextInt();
                   input.nextLine();
                   switch(choice) {
                    case 1:
                        System.out.println("The balance is: " + accounts[enteredID].getBalance());
                        break;
                    case 2:
                        System.out.print("Enter withdraw amount: ");
                        int withdrawAmount = input.nextInt();
                        accounts[enteredID].withDraw(withdrawAmount);
                        break;
                    case 3:
                        System.out.print("Enter deposit amount: ");
                        int depositAmount = input.nextInt();
                        accounts[enteredID].deposit(depositAmount);
                        break;
                    case 4:
                        System.out.println("Exit");
                        System.out.println("Enter an ID");
                        break;
                }
            } while (true);
        }

import java.util.Scanner;

public class Account {

private int id;
private double balance;
private static double annualIntrestRate;
private java.util.Date dateCreated;

public Account() {

}

public Account(int id) {
    this.id = id;
    balance = 100;
    dateCreated = new java.util.Date();

}

public void setAnnualIntrest(double intrest) {
    annualIntrestRate = intrest;

}

public void setBalance(double newBalance) {
    balance = newBalance;
}

public void setID(int newID) {
    id = newID;
}

public double getBalance() {
    return balance;
}

public int getID() {
    return id;
}

public java.util.Date getDate() {
    return dateCreated;
}

public static double getMonthlyIntrestRate() {

    return ((annualIntrestRate / 12) / 100);
}

public double getMonthlyIntrest() {
    return (balance * getMonthlyIntrestRate());
}

public double withDraw(double withDrawAmount) {
    return balance = balance - withDrawAmount;
}

public double deposit(double depositeAmount) {
    return balance = balance + depositeAmount;
}


public static void main(String[] args) {

    Account[] accounts = new Account[10];
    for (int i = 0; i < accounts.length; i++) {
        accounts[i] = new Account(i);
    }

    Scanner input = new Scanner(System.in);

    System.out.print("Enter an ID: ");
    int enteredID = input.nextInt();
    boolean shouldExit = false;

    while (true) {

        if (enteredID >9) {

            System.out.print("enter correct id: ");
            enteredID = input.nextInt();



        }


        if (enteredID == accounts[enteredID].getID()) {


            System.out.println("Main Menu: ");
            System.out.println("1: check balance");
            System.out.println("2: withdraw");
            System.out.println("3: deposit");
            System.out.println("4: exit");

            System.out.print("Enter a choice: ");
            int choice = input.nextInt();

            if (choice == 1) {
                System.out.println("The balance is: " + accounts[enteredID].getBalance());
                continue;
            } else if (choice == 2) {
                System.out.print("Enter withdraw amount: ");
                int withdrawAmount = input.nextInt();
                accounts[enteredID].withDraw(withdrawAmount);
                continue;
            } else if (choice == 3) {
                System.out.print("Enter deposit amount: ");
                int depositAmount = input.nextInt();
                accounts[enteredID].deposit(depositAmount);
                continue;
            }

            shouldExit = false;
            while (!shouldExit) {
                if (choice == 4) {
                    System.out.print("Enter an ID: ");
                    enteredID = input.nextInt();

                    shouldExit = true;


                }

            }
        }


    }


}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM