首页 > 解决方案 > Java OOP 练习

问题描述

自动柜员机练习

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

问题

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

书中的问题

(游戏:ATM 机)使用在编程练习 9.7 中创建的 Account 类来模拟 ATM 机。在 id 为 0, 1, 的数组中创建十个帐户。. . , 9, 初始余额 100 美元。系统提示用户输入一个id。如果 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();
            }
        }


    }

标签: java

解决方案


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

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 后不断重新出现,这意味着代码的该部分需要使用另一个循环,即:

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

希望这有助于指导您朝着正确的方向前进。


推荐阅读