2017-04-02 51 views
0

每当使用存款或提款方法时,它将增加或减少原始余额,但如果您尝试再次增加或减少,则它不会识别以前的交易,并且将从原始余额中增加或减少在交易完成后,再次取代新的余额。发生双值的问题

例如:如果我将$ 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; 
    } 

} 
+1

欢迎使用堆栈溢出!它看起来像你需要学习使用调试器。请帮助一些[互补调试技术](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。如果您之后仍然遇到问题,请随时返回一个[最小,完整且可验证的示例](http://stackoverflow.com/help/mcve),以说明您的问题。 –

回答

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(...)

0

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

使用double来存储帐户的值时,您会遇到很多问题。您应该使用long int来保存美分数。

例如,如果您增加0.01美元一百次,您可能会惊讶当您取消$ 1.00时会发生什么