2013-12-18 56 views
0

我有點卡在這一個。我正在編寫一個帶有兩個類的java程序,然後用一個測試程序來測試這個類中的方法。我堅持要在main方法中調用下面的兩個方法。所有的類文件(測試程序類和另外兩個類)正在編譯,並且IDE不會給我任何錯誤消息,計算僅僅不會發生...從主要方法中的類調用方法

- 主要方法代碼:

//Call debit method 
System.out.println("Please enter amount to be debited"); 
double amount=input.nextDouble(); 
account.debit(amount); 
System.out.printf("%.2f%n",balance); 

//Call credit method 
System.out.println("Please enter amount to be credited"); 
amount=input.nextDouble(); 
account.credit(amount); 
System.out.printf("%.2f%n",balance);  

--account類代碼:

//Method for crediting account balance 
public void credit (double amount) { 
    double newBalance=this.balance+amount; 
    this.setBalance(newBalance); 
} 

//Method for debiting account balance 
public void debit (double amount) { 
    if (amount<balance) { 
    double newBalance=this.balance-amount; 
    this.setBalance(newBalance); 
    } else { 
    System.out.println("Insufficient Funds!"); 
} 

注意:天平二傳手工作,因爲它是在前面的測試程序中調用...

任何幫助非常感激d!

的賬戶類完整代碼:

public class Account { 
private int accountId; 
private String accountName; 
private String accountAddress; 
private double balance; 
private Bank bank; 

//Default Constructor 
public Account() { 
} 

//Getters 
public int getAccountId() { 
    return accountId; 
} 

public String getAccountName() { 
    return accountName; 
} 

public String getAccountAddress() { 
    return accountAddress; 
} 

public double getBalance() { 
    return balance; 
} 

public Bank getBank() { 
    return bank; 
} 

//Setters 
public void setAccountId (int accountId) { 
    if (accountId <=10000000 || accountId >=99999999) { 
    System.out.println("Invalid Account Id"); 
    } else { 
    this.accountId=accountId; 
    } 
} 

public void setAccountName (String accountName) { 
    if (accountName.length()>=10) { 
    System.out.println("Too Long"); 
    } else { 
    this.accountName=accountName; 
    } 
} 

public void setAccountAddress (String accountAddress) { 
    this.accountAddress=accountAddress; 
} 

public void setBalance (double balance) { 
    if (balance<0.0) { 
    System.out.println("Invalid Balance"); 
    } else { 
    this.balance=balance; 
    } 
} 

public void setBank (Bank bank) { 
    this.bank=bank; 
} 

//Constructor to initialize accountId, accountName, accountAddress and Bank 
public Account (int accountId, String accountName, String accountAddress, Bank bank) { 
    this.setAccountId(accountId); 
    this.setAccountName(accountName); 
    this.setAccountAddress(accountAddress); 
    this.setBank(bank); 
} 

//Method to print out account category based on balance 
public void printAccountCategory() { 
    if (balance<100.0) { 
    System.out.println("Challenged Account"); 
    } else if (balance>=100.0 && balance<999.9) { 
    System.out.println("Standard Account"); 
    } else if (balance>=1000.0 && balance<9999.9) { 
    System.out.println("Promising Account"); 
    } else if (balance>=10000.0 && balance<99999.9) { 
    System.out.println("Gold Star Account"); 
    } else { 
    System.out.println("Super Duper Account"); 
    } 
} 

//Method to project balance based on compound interest and the number of years required 
//Note: I took the formula using n (number of times the interest is compounded per year) as 1 
public double projectNewBalance (int numberYears) { 
    if (numberYears>0) { 
    double interest=1; 
    for (int i=1; i<=numberYears; i++) { 
     interest*=(1.0+bank.getInterestRate()); 
    } 
     double newBalance=balance*interest; 
     return newBalance; 
    } else if (numberYears<0) { 
     System.out.println("Invalid Value"); 
    } else { 
     return balance; 
    } 
    return balance; 
    } 

    //Method for crediting account balance 
    public void credit (double amount) { 
    double newBalance=this.balance+amount; 
    this.setBalance(newBalance); 
    } 

    //Method for debiting account balance 
    public void debit (double amount) { 
    if (amount<balance) { 
     double newBalance=this.balance-amount; 
     this.setBalance(newBalance); 
    } else { 
     System.out.println("Insufficient Funds!"); 
    } 
    } 

    //toString method 
    public String toString() { 
    return "Account Id: "+accountId+", Account Name: " + accountName + ", Account Address: "+accountAddress+", Balance: "+balance+", Bank Details: "+bank.toString()+"."; 
    } 
} 

主要方法全碼:

import java.util.Scanner; 

public class BankAccountTest { 
    public static void main (String [ ] args) { 
    //Create an instance of the Bank class 
    Bank bank = new Bank ("WIT Bank", "Paddy Snow", 0.045); 

    //Create instance of Scanner class 
    Scanner input=new Scanner(System.in); 

    //Prompt user to input data to create an account 
    System.out.println("Please enter an Account ID"); 
    int accountId=input.nextInt(); 

    System.out.println("Please enter an Account Name"); 
    String accountName=input.next(); 

    System.out.println("Please enter an Account Address"); 
    String accountAddress=input.next(); 

    //Create instance of the Account class 
    Account account = new Account (accountId, accountName, accountAddress, bank); 

    //Print out details of account class 
    System.out.println(account); 

    //Prompt user to enter balance for the account 
    System.out.println("Please enter account balance"); 
    double balance=input.nextDouble(); 
    account.setBalance(balance); 

    //Use printAccountCategory method 
    account.printAccountCategory(); 

    //Call projectNewBalance method 
    // Note: Method tested with value of 10 years as mentioned in spec, 
    // but user input also included I think it is more appropriate for the functionality of the program 
// int numberYears=10; 
// double newBalance1=account.projectNewBalance(numberYears); 
// System.out.println(""+newBalance1); 
    System.out.println("Please enter number of years"); 
    int numberYears=input.nextInt(); 
    double newBalance=account.projectNewBalance(numberYears); 
    System.out.printf("%.2f%n",newBalance); 

    //Call debit method 
    System.out.println("Please enter amount to be debited"); 
    double amount=input.nextDouble(); 
    account.debit(amount); 
    System.out.printf("%.2f%n",balance); 

    //Call credit method 
    System.out.println("Please enter amount to be credited"); 
    amount=input.nextDouble(); 
    account.credit(amount); 
    System.out.printf("%.2f%n",balance);  
    } 
} 
+1

你在哪裏實例化'Account'類的main?當你打印出「balance」時,你應該首先更新來自'account'對象的值。您應該顯示您的課程的完整源代碼。 –

+0

請顯示一個完整的例子。此外,你是編程OO還是程序?如果第二,那麼你必須聲明一切爲靜態。我建議開始在單個(類)文件中將所有文件全部複製並運行快速測試。 –

回答

1

你的數字可能永遠看起來是一樣的,因爲局部變量沒有被打印出來之前更新。

確保您在致電System.out.printf("%.2f%n",balance);之前更新balance的值。

喜歡的東西:

balance = account.getBalance();

+0

現在就試試,謝謝 – user3068335

+0

完美工作,謝謝!知道這將是愚蠢的東西! – user3068335

1

它不應該被打印你所創建的對象的平衡,而不只是 「平衡」: 的System.out.println(account.getBalance()) ?

+0

會嘗試,現在,謝謝 – user3068335

+0

完美的工作,謝謝!知道這將是愚蠢的東西! – user3068335