为什么我的撤销方法允许我取出更多然后存入?

时间:2016-11-06 05:46:19

标签: java

我必须为我的计算机科学课写一个ATM程序。除了程序本身的一些逻辑问题之外,它在很大程度上起作用。到目前为止,据我所知,我的ATM将存入我告诉它的正确金额但是,当我取款时,如果我故意犯错误(例如试图拿走),它并不总是取出正确的金额出一个不是20的倍数的金额。我也遇到了一个问题,如果我试图拿出更多的钱然后实际上是在帐户中,它将成为负值,我不想被允许。我希望它不要减去导致它变为负数的值,并且它要提示用户,直到能够取出小于或等于余额的值。我对编码很新,所以请原谅我相当混乱的代码。我的错误可能是在else if if语句where = = 3.这是我的代码:

import java.io.*;
import java.util.*;
public class aTMLauncher
{
    public static int options=0;
    public static void main(String [] args)
    {
        Scanner input = new Scanner(System.in);
        aTMTester atm = new aTMTester();

        System.out.println("Login: \n(case sensitive)");
        System.out.print("Username > ");

        String usernameInput=input.nextLine();
        int count=4;
        while (!usernameInput.equals(aTMTester.getUsername()))
        {
            System.out.println("\nIncorrect input. Please try again. You have " + (count-1) + " trys remaining attempts.");
            System.out.println("Login: \n(case sensitive)");
            System.out.print("Username > ");
            count--;
            if(count==0)
            {
                System.out.println("No remain attempts, system will now exit");
                System.exit(0);
            }
            usernameInput = input.nextLine();
        }

        System.out.print("Pin > ");
        int PINInput=input.nextInt();
        int count1=4;
        while (PINInput<aTMTester.getPIN() || PINInput>aTMTester.getPIN())
        {
            System.out.println("\nIncorrect input. Please try again. You have " + (count1-1) + " trys remaining");
            System.out.print("Pin > ");
            count1--;
            PINInput=input.nextInt();
            if(count1==0)
            {
                System.out.println("No remain attempts, system will now exit");
                System.exit(0);
            }
        }

        System.out.println("\nWelcome, " + aTMTester.getUsername() +"!");

        while(PINInput==2468)
        {
            atm.main();
            options = input.nextInt();
            if (options==4)
            {
                atm.logout();
            }
            else if(options==2)
            {
                System.out.println("How much money would you like to deposit?");
                System.out.print("$ ");
                atm.deposit = input.nextInt();
                atm.balance+=atm.deposit;
                System.out.println("Your new balance is $" + atm.balance +"0");
            }
            else if(options==3)
            {
                System.out.println("How much money will you be withdrawing? \n(Only amounts divisible by 20 are accepted) \n$");
                atm.withdraw= input.nextInt();
                atm.balance-=atm.withdraw;
                if(atm.withdraw%20==0.0)
                {
                    System.out.println("You took out " +atm.withdraw);
                    System.out.println("Your new balance is $" + atm.balance);
                }
                while(atm.withdraw%20>0)
                {
                    System.out.println("Invalid withdraw amount. Please retry.");
                    System.out.println("\nHow much money will you be withdrawing? \n(Only amounts divisible by 20 are accepted)");
                    atm.withdraw= input.nextInt();
                    System.out.println("You took out " +atm.withdraw);
                    System.out.println("Your new balance is $" + atm.balance);
                }
                if(!(atm.balance>0.0))
                {
                    System.out.println("You are not allowed to take out more then you have in your account \n Please retry");
                    atm.withdraw= input.nextInt();
                }
            }
            else if(options==1)
            {
                System.out.println("Your account balance is $"+atm.balance+"0");
            }
        }
    }
}


public class aTMTester
{
    private static final String username = "David";
    private static final int PIN = 2468;
    public static int deposit, withdraw;
    public static double balance=0.00;
    private String menu;

    /*
     * Default constructor
     */

    public aTMTester()
    {
    }

    public static String getUsername()
    {
        return username;
    }

    public static int getPIN()
    {
        return PIN;
    }

    public static void main()
    {
        System.out.println("\n+++++++++++Account: "+ username +"+++++++++++");
        System.out.println("1. Check Account Balance");
        System.out.println("2. Deposit Checks");
        System.out.println("3. Withdraw Money");
        System.out.println("4. Logout");

        System.out.print("\nWhat would you like to do next?\n");
    }

    public static void logout()
    {
        System.out.println("Thanks for usinging the David Vachlon Inc. ATM. We hope you expierence was fast and simple! Have a great day.");
        System.exit(0);
    }

    public static double getBalance()
    {
        return balance;
    }

    public static void deposit()
    {
        balance+=deposit;
    }

    public static void withdraw()
    {
        balance-=withdraw;
    }
}

1 个答案:

答案 0 :(得分:0)

在成功通过所有检查后,您应该atm.balance -= atm.withdraw; 。例如,您可以按如下方式重构代码:

boolean ok = false;
while (!ok) {
    System.out.println("\nHow much money will you be withdrawing? \n(Only amounts divisible by 20 are accepted)");
    atm.withdraw = input.nextInt();
    if (atm.withdraw % 20 != 0) {
        System.out.println("Invalid withdraw amount. Please retry.");
    } else if (atm.balance < atm.withdraw) {
        System.out.println("You are not allowed to take out more then you have in your account \n Please retry");
    } else if (atm.withdraw < 0) {
        // Probably you should not allow to withdraw a negative amount of money
    } else {
        ok = true;
    }
}
atm.balance -= atm.withdraw;
System.out.println("You took out " + atm.withdraw);
System.out.println("Your new balance is $" + atm.balance);