2013-04-06 68 views
0

我創建了一個主要的Java程序,像一個銀行給用戶的平衡,取款和轉賬的工作。我不能得到Java文件給我的錯誤

import java.util.Scanner; 

public class Lab12 { 
    public static void main(String[] args) 
    { 
     //Creating Two BankAccounts 
     BankAccount B1 = new BankAccount(1000, "ASU_ACCOUNT_110"); 
     BankAccount B2 = new BankAccount(500, "ASU_ACCOUNT_100"); 
     double amount; 
     Scanner scan = new Scanner(System.in); 

     //Account Deposit 
     System.out.println("Please Enter amount to deposit into ASU_ACCOUNT_110 Account"); 
     amount = scan.nextDouble(); 
     if(!B1.deposit(amount)) 
      System.out.println("Error depositing amount in account. Please enter positive value."); 
     else 
      System.out.println("Successfully deposited $"+amount+". The current balance is "+B1.getBalance()); 

     //Account Withdrawal 
     System.out.println("Please Enter the amount you would like to withdraw from ASU_ACCOUNT_110"); 
     amount = scan.nextDouble(); 
     if(!B1.withdraw(amount)) 
      System.out.println("Error withdrawing amount. You are either overdrawing or you have entered a negative value"); 
     else 
      System.out.println("Successfully withdrew $"+amount+". The current balance is "+B1.getBalance()); 

     //Account Transfer 
     System.out.println("Please Enter the amount you would like to transfer from ASU_ACCOUNT_110 to ASU_ACCOUNT_100"); 
     amount = scan.nextDouble(); 
     if(!B1.transfer(amount, B2)) 
     { 
      System.out.println("Error transfering amount. You are either overdrawing or you have entered a negative value"); 
     } 
     else 
     { 
      System.out.println("Successfully transferred $"+amount+".\nThe current balance in ASU_ACCOUNT_110 is "+B1.getBalance()); 
      System.out.println("The current balance in ASU_ACCOUNT_100 is "+B2.getBalance()); 
     } 

     //Account Display 
     System.out.println("\nThe details of the two accounts are:"); 
     System.out.println("------------------------------------------"); 
     display(B1); 
     System.out.println("------------------------------------------"); 
     display(B2); 
    } 

    public static void display(BankAccount B) 
    { 
     System.out.println("The Account Number is "+B.getAccountNumber()); 
     System.out.println("The balance is "+B.getBalance()); 
    } 
} 

我創造出了方法和獲取並調用餘額

第二類
public class BankAccount { 

    private String AccountNumber; 
    private double balance; 

    public BankAccount(double balance, String accountNumber) 
    { 
     this.balance = balance; 
     this.AccountNumber = accountNumber; 
    } 

    public boolean deposit(double amount) 
    { 
     return true; 
    } 

    public double getBalance() 
    { 
     return balance; 
    } 

    public boolean withdraw(double amount) 
    { 
     return true; 
    } 

    public boolean transfer(double amount, BankAccount b2) 
    { 
     return true; 
    } 

    public String getAccountNumber() 
    { 
     return AccountNumber; 
    } 
} 

的問題是,當我進入一個負金額存款,取款,轉賬,我沒有得到我把這個錯誤放在主類

有什麼想法?

+0

你似乎是堆棧溢出的新手。因此,答對的人得到信用,如果他們的問題有幫助,請點擊向上箭頭。如果與問題無關,請單擊向下箭頭。如果他們的答案解決了您的問題,請點擊勾號。謝謝! – Cin316 2013-04-06 02:37:05

+2

以下有關您的BankAccount中的方法需要完成的一些很好的答案。但是,當您開始編寫真正的銀行軟件爲生活時,請不要使用浮點數來表示錢! :) – Todd 2013-04-06 02:37:07

回答

0

存款,取款和轉賬方式始終返回true。因此你永遠不會看到錯誤信息。

2

您不檢查BankAccount.deposit(double)中的數字是否爲負數。您需要檢查代碼是否如下所示:

public boolean deposit(double amount) 
{ 
    if(amount<0) 
    { 
     return false; 
    }else{ 
     return true; 
    } 
} 
2

您在BankAccount中的方法始終返回true。除非您提供這樣做的實現,否則您不能指望它們有時表示失敗。

例如:

public boolean withdraw(double amount) 
{ 
    return true; 
} 

永遠不會信號故障,因爲它總是回報真實。爲了使您的方法按照您希望的方式行事,您必須添加更復雜的行爲。例如:

public boolean withdraw(double amount) 
{ 
    //if the withdrawal is positive and does not exceed the balance 
    if (amount >= 0 && amount <= balance) 
    { 
     //remove money and signal success 
     balance -= amount; 
     return true; 
    } 
    //if either condition failed, signal failure 
    return false; 
} 

它看起來像你只是忘了實現這些方法。無論是你對編程有一個根本性的誤解。

相關問題