双重值问题

时间:2017-04-02 20:18:00

标签: java

无论何时使用存款或取款方式,它都会在原始余额中加或减,但如果您尝试再次加或减,则无法识别以前的交易,并会再次从原始余额中加或减,而不是新的交易完成后的余额。

例如:如果我将10美元存入20美元的账户,那么它将等于30美元。如果我再尝试存入5美元,它将等于25美元而不是35美元。它基于原始余额的所有交易,这就是问题所在。我不知道如何解决它。

//This program will create a functioning mock ATM with the usual features of a real ATM.

import java.util.*;

public class ATM {

    public static Scanner kbd;

    /**
     * This is the main method, everything that is returned from other methods is sent here to be processed and initialized into the atm.
     * @param args
     */
    public static void main(String[] args) {
        String acctNum, acctPword;
        String balance;
        boolean menu = false;
        double depAmnt = 0.0, withAmnt = 0.0;
        int x = 1;
        kbd = new Scanner(System.in);
        //getting the user account username
        System.out.print("Enter your account number: ");
        acctNum = kbd.nextLine();
        //getting the users password
        System.out.print("Enter your account password: ");
        acctPword = kbd.nextLine();
        //incase the user enters invalid information
        balance = checkID(acctNum, acctPword);
        while (balance.equals("error") && x <4){
            System.out.println("Wrong password try again.");
            System.out.print("Enter your account password: ");
            acctPword = kbd.nextLine();
            x++;
        }
        //once they gained access to the atm machine, the balance will print for informative purposes.
        double balance1 = Double.parseDouble(balance);
        System.out.println("You currently have $" + String.format("%.2f",balance1));
        //they only get 3 attempts to enter the correct password.
        if (x == 4)
            System.out.println("Maximum number of attempts reached, Please try again later.");
        //This switch and while statement controls the main menu of the atm machine and is capable of calling all methods.
        while (menu == false){
        switch (menu()) {
        case 1: 
            System.out.println("Your balance is $" + String.format("%.2f",balance1));
            break;
        case 2: 
            deposit(balance1, depAmnt);
            break;
        case 3:
            withdraw(balance1, withAmnt);
            break;
        case 4: 
            System.out.println("Have a nice day.");
            menu = true;
            break;
        }
        }
        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 checked
     * @param pwd The password to be checked
     * @return If the account information is valid, returns the current 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";
        //these 3 if statements and everything declared right before the if statements, first checks the users name and pword for gained access and allows the user to enter the code and change the password and username if they so please.
        int pos1, pos2;
        pos1 = a.indexOf(" ");
        pos2 = a.lastIndexOf(" ");
        String account, password;
        account = a.substring(0, pos1);
        password = a.substring(pos1+1,pos2);
        if (acctNum.equals(account) && pwd.equals(password)){
            result = a.substring(pos2+1);
            return result;
        }

        int pos1b, pos2b;
        pos1b = b.indexOf(" ");
        pos2b = b.lastIndexOf(" ");
        String accountb, passwordb;
        accountb = b.substring(0, pos1b);
        passwordb = b.substring(pos1b+1,pos2b);
        if (acctNum.equals(accountb) && pwd.equals(passwordb)){
            result = b.substring(pos2b+1);
            return result;
        }

        int pos1c, pos2c;
        pos1c = c.indexOf(" ");
        pos2c = c.lastIndexOf(" ");
        String accountc, passwordc;
        accountc = c.substring(0, pos1c);
        passwordc = c.substring(pos1c+1,pos2c);
        if (acctNum.equals(accountc) && pwd.equals(passwordc)){
            result = c.substring(pos2c+1);
            return result;
        }

        return result;
    }

    /**
     * This menu method will get the users input and allow them to choose 4 options otherwise they will receive an error message.
     * @return x is returned as the users input and will dictate which option they choose.
     */
    public static int menu(){
        int x = 1;

            // the physical aspect of the menu
            System.out.println("1. Display Balance \n2. Deposit\n3. Withdraw\n4. Log Out");
            x = kbd.nextInt();

        //incase they dont enter valid info
        if(x > 4){
            System.out.println("That is not an option.");
            x = 0;
            x = kbd.nextInt();
        }
        return x;
    }
    /**
     * This method will allow the user to use a deposit feature found on every atm if they wish to deposit money into their account.
     * @param acctBal this is the account balance and will dictate how much they have before and after the deposit
     * @param depAmnt this is how much they so choose to deposit to their accounts
     * @return the account balance is returned and revised to what ever amount they chose to add.
     */
    public static double deposit(double acctBal, double depAmnt){

        System.out.print("How much money would you like to deposit? $");
        depAmnt = kbd.nextDouble();
        acctBal = acctBal + depAmnt;
        System.out.println("Your balance is now at $" + String.format("%.2f", acctBal));
        return acctBal;
    }

    /**
     * This allows the user to take money from their account if they have money in the first place.
     * @param acctBal the account balance will be returned as a new lesser value once the withdraw is made
     * @param withAmnt this dictates how much is taken from the acct.
     * @return the now lesser acct balance is returned
     */
    public static double withdraw(double acctBal, double withAmnt){

        System.out.print("How much money would you like to withdraw? $");
        withAmnt = kbd.nextDouble();

        if (acctBal <= 0){
            System.out.println("You do not have any money.");
            return acctBal;
        }

        if (acctBal < withAmnt){
            System.out.print("You do not have enough money.\nHow much money would you like to withdraw? $");
            withAmnt = kbd.nextDouble();
        }
        else{
            acctBal = acctBal - withAmnt;
        }
        System.out.println("You now have $" + String.format("%.2f",acctBal));
        return acctBal;
    }

    /**
     * all this does is simply display the current balance of the user
     * @param balance the balance as it stands during the program.
     * @return 
     */
    public static double displayBalance(double balance){
        System.out.println("Your balance is $" + String.format("%.2f", balance));
        return balance;
    }

}

2 个答案:

答案 0 :(得分:0)

问题出在您的switch声明中:

switch (menu()) {
    case 1: 
        System.out.println("Your balance is $" + String.format("%.2f",balance1));
        break;
    case 2: 
        deposit(balance1, depAmnt);
        break;
    case 3:
        withdraw(balance1, withAmnt);
        break;
    case 4: 
        System.out.println("Have a nice day.");
        menu = true;
        break;
}

您的depositwithdraw功能都会返回新余额,但不会更新。因此,您必须将新值存储在balance1中。只需balance1 = deposit(...)balance1 = withdraw(...)

答案 1 :(得分:0)

当您致电存款(balance1,depAmnt)时,您不会将返回值保存在任何地方。请注意,该方法会增加deposit()方法的本地变量balance1的值,而不是main()中的值。

使用双倍存储帐户的值时,您会遇到很多问题。您应该使用long int来节省分数。

例如,如果你加100美元一百次,你可能会惊讶于当你提取1.00美元时会发生什么