Java OOP练习

时间:2019-02-28 18:38:42

标签: java

ATM练习

它要求用户输入一个ID,然后检查ID是否正确,并显示具有四个选项的主菜单。

问题

问题在于,当用户选择一个选项时,程序结束了..但是我需要的是,一旦系统启动,它就永远不会停止,直到用户选择4(这是退出选项),​​然后重新开始

书中的问题

(游戏:ATM机)使用在编程练习9.7中创建的Account类模拟ATM机。在ID为0、1 、、的数组中创建十个帐户。 。 。 ,9和初始余额$ 100。系统提示用户输入编号。如果输入的ID错误,请要求用户输入正确的ID。接受ID后,将显示主菜单,如示例运行中所示。您可以输入选项1查看当前余额,选择2提取资金,选择3存入资金,选择4退出主菜单。退出后,系统将再次提示输入ID。因此,一旦系统启动,它就不会停止。

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


    }

3 个答案:

答案 0 :(得分:3)

程序中缺少该问题的两个基本部分:

1)退出后,系统将再次提示输入ID。因此,一旦系统启动,它就不会停止。

这意味着一旦您的main方法中的实际工作开始(第一个“输入id”),就不应在内部停止程序;如果它在终端中运行,则只能通过Ctrl-C来访问;如果它在IDE中运行,则只能通过“停止”按钮来实现。

要实现此目的,您需要一个外部while循环:

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

主要方法中整个“工作”的内容。

2)接受ID后,将显示主菜单,如示例运行中所示。您可以输入选项1来查看当前余额,选择2来取款,选择3来存钱,选择4退出主菜单。

我假设这意味着在输入选项4之前,菜单应该在用户完成任务1、2或3之后一直重新出现,这意味着该部分代码需要使用ANOTHER循环,即:

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

希望这有助于指导您正确的方向。

答案 1 :(得分:0)

尝试以下代码:

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

我建议使用switch语句代替if-else梯形图

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

答案 2 :(得分:0)

导入java.util.Scanner;

公共类帐户{

$request->validate([
        'ObservationNotes'=>'required',
    'ImageUpload.*'=>'image|mimes:jpeg, png, jpg, gif, svg |max:2048'
]);

}