2016-11-06 9 views
-4

コンピューターサイエンスクラスのATMプログラムを書く必要があります。これは、プログラム自体の中のいくつかの論理的な問題を除いて、ほとんどの場合に機能します。これまでのところ、私が言うことから、私のATMは正しい金額を預金しますが、私がお金を引き出すとき、私が意図的に間違いを犯すと必ず正しい金額を払い戻すわけではありません。 20の倍数ではない量を外に出す)。私はまた、私が実際に口座に入っているより多くのお金を取り出そうとすると、負の値になり、許可されたくないという問題にぶつかります。私はそれが負になるような値を減算しないようにして、残高よりも少ないまたは同等の価値が取り出せるようになるまでユーザに促すことを望みます。私はむしろコーディングに新しいので、私のかなり面倒なコードを許してください。私の引き出し方法は、なぜ私が引き取ることができるのですか?

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

「ここには私のコードがありますが、何が間違っているのかわかりません」という形式の質問はここでは解説していません。このサイトを効果的に使用する方法については、[help]にアクセスして[ask]をお読みください。 –

+0

金額が大きすぎる場合は引き出し金額を引いてもらえないようにしたい場合は、引き落とす前に引き出し金額が大きすぎるかどうかを確認するために「if」を使用することです。 – ajb

+0

この質問は良いですか?そのようなより具体的には、ヘルプセンターに記載されています。 –

答えて

0

あなたはすべてのチェックが正常に渡された後atm.balance -= atm.withdraw;を行う必要があります:オプション== 3.ここに私のコードです文があれば私のエラーは、他の周りに、おそらくです。たとえば、次のようにコードを再構成することができます。

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); 
+0

本当に助けてくれてありがとう! –

関連する問題