ATM方法程序(可能的逻辑或括号错误)

时间:2016-11-09 00:36:41

标签: java methods

所以,我必须制作一个充当基本ATM的程序,并且我遇到了两个问题:

  • 当我使用相关帐户测试无效密码的程序时,该程序应该进行循环计数,在用户踢出用户之前为用户提供最多3次无效尝试,而是打印"无效密码" 3次并在第一个错误密码后结束程序。我认为这是一个包围错误,但我并不确定自己

  • 每当我执行像存入账户的操作时,程序将显示新的金额,但它实际上不会存储该程序的新值。例如,如果我将100美元存入账户1,它会说新余额为620.36美元,但当它回到主菜单并再次检查新余额时,它会将其恢复为之前的值,即“不应该(tjis是我认为逻辑错误可能在哪里)

我希望有人能够指导我找到可能的调试解决方案或我编码错误的一般解决方案。

这是代码:

import java.util.*;

public class ATM {
    public static Scanner kbd;

    private static final int MAXATTEMPTS = 3;

    public static void main(String[] args) {

        kbd = new Scanner(System.in);

        System.out.println("Hello, user. This is an ATM.");
        System.out.println("Please enter account number");
        String acctNum = kbd.next();
        System.out.println("Please enter password");
        String pwd = kbd.next();
        int attemptNumber = 0;
        String res = checkID(acctNum, pwd);
        do{




            if (res.equals("error")){
                attemptNumber++;
                System.out.println("Invalid password, please try again.");

                if (attemptNumber == MAXATTEMPTS){
                    System.out.print("Maximum Login Attempts Reached.");
                    System.exit(0);

                }
            }
            else {
                double balance = Double.parseDouble(res);
                while(true){


                    int option = menu();

                    switch (option) {
                    case 1:
                        displayBalance(balance);
                        break;
                    case 2:
                        deposit(balance, balance);
                        break;
                    case 3:
                        withdraw(balance, balance);
                        break;
                    case 4:
                        System.out.println("Thank you for banking with us, have a nice day!");
                        System.exit(0); 

                    }
                }
            }
        }
        while (res.equals("error"));



        kbd.close();
    }


    /**
     * Determines if acctNum is a valid account number and pwd is the
     * correct password for the account.
     * @param acctNum The account number to be tested
     * @param pwd The possible password for the account
     * @return If the account information is valid, returns the account balance
     * as a string. If the account information is invalid, returns the string "error".
     */
    public static String checkID(String acctNum, String pwd)
    {
        String result = "error";

        // Strings a, b, and c contain the valid account numbers and passwords.
        // For each string, the account number is listed first, followed by
        // a space, followed by the password for the account, followed by a space,
        // followed by the current balance.
        String a = "44567-5 mypassword 520.36";
        String b = "1234567-6 anotherpassword 48.20";
        String c = "4321-0 betterpassword 96.74";

        String acctNum1, acctNum2, acctNum3, pwd1, pwd2, pwd3, bal1, bal2, bal3;
        acctNum1 = a.substring(0, a.indexOf(" "));
        acctNum2 = b.substring(0, a.indexOf(" "));
        acctNum3 = c.substring(0, a.indexOf(" "));
        pwd1 = a.substring(a.indexOf(" ")+1, a.lastIndexOf(" "));
        pwd2 = b.substring(b.indexOf(" ")+1, b.lastIndexOf(" "));
        pwd3 = c.substring(c.indexOf(" ")+1, c.lastIndexOf(" "));
        bal1 = a.substring(a.lastIndexOf(" " )+1);
        bal2 = b.substring(a.lastIndexOf(" " )+1);
        bal3 = c.substring(a.lastIndexOf(" " )+1);
        if (acctNum.equals(acctNum1) && pwd.equals(pwd1)){
            result = bal1;
        }
        if (acctNum.equals(acctNum2) && pwd.equals(pwd2)){
            result = bal2;
        }   
        if (acctNum.equals(acctNum3) && pwd.equals(pwd3)){
            result = bal3;
        }

        return result;
    }
    /**
     */
    public static int menu(){
        boolean invalidInput = true;
        do {
            System.out.println("Please select from the following options:");
            System.out.printf("1. Display Balance \n");
            System.out.printf("2. Deposit \n");
            System.out.printf("3. Withdraw \n");
            System.out.printf("4. Log Out \n");

            int choice = kbd.nextInt();
            if (choice == 1){
                return 1;
            }
            else {
                if (choice == 2){
                    return 2;
                }
                else{
                    if (choice == 3){
                        return 3;
                    }
                    if (choice == 4) {
                        return 4;
                    }
                }
            }
        }



        while(invalidInput);


        return 0;

    }
    /**
     * 
     * @param accBalance given account balance for Account
     * @param depoAmnt amount going to be added to accBalance
     * @return double that will show updated balance
     */
    public static double deposit(double accBalance, double depoAmnt){
        System.out.println("How much would you like to deposit");
        depoAmnt = kbd.nextDouble();
        double newBalance = accBalance + depoAmnt;
        System.out.println("Your new balance is: " + newBalance);
        return newBalance;

    }
    /**
     * 
     * @param accBalance amount of money in account
     * @return displays amount of money in account
     */
    public static void displayBalance(double accBalance){

        System.out.printf("\nYour current balancre is $%.2f\n", accBalance);


    }
    /**
     * 
     * @param accBalance given account balance for account
     * @param withdraw amount to be taken out of account
     * @return if withdraw !> accBalance, then it will be subtracted from it and resultant amount will be displayed
     */
    public static double withdraw(double accBalance, double withdraw){
        System.out.println("How much would you like to withdraw?");
        withdraw = kbd.nextDouble();
        if (accBalance < withdraw){
            System.out.printf("Cannot withdraw more than balance" + "\nYour current balance is: " + accBalance + "\n");
            return accBalance;
        }
        else {

            double newBalance = accBalance - withdraw;
            System.out.println("Your new balance is: " + newBalance);
            return newBalance;
        }


    }

}

1 个答案:

答案 0 :(得分:0)

首先,只有在ID检查结果为&#34;错误&#34;时,才会执行while循环。您需要将循环的条件更改为“真实”&#39;所以它会运行,因为这是主要的事件循环:

C:/...Scripts> pip install requests