2016-11-09 29 views
0

所以,我必須作出充當基本ATM的程序,並且我遇到兩個問題: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

歡迎使用StackOverflow。請花一些時間訪問[幫助]並閱讀[問]。形式問題「這是我的代碼,請調試」被認爲是無關緊要的。 StackOverflow不是討論,教程或調試網站。這樣做的方式是,你需要嘗試解決問題,然後在遇到困難時尋求幫助,清楚地解釋你所嘗試的以及你不瞭解的內容。至少您應該已經在IDE調試器中逐步瞭解代碼,並能夠識別出與您的期望不符的結果。 –

+1

您可以將問題分爲兩部分。但對於第一個:如果只讀一次,則不能引入3個密碼:'String pwd = kbd.next();'。你需要在循環中添加它。 –

+0

考慮使用'String acctNum = kbd.nextLine();' –

回答

0

那麼首先你所有while循環將只執行如果ID檢查的結果是「錯誤」。您需要將循環條件更改爲'true',因此它將運行,因爲這是主要事件循環:

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 (true);