[英]calling methods from classes in the main method
我有點卡在這個上面。 我正在編寫一個帶有兩個類的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);
- 賬戶類代碼:
//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!");
}
注意:天平設定器正在工作,因為它在測試程序中早先被稱為...
任何幫助非常感謝!!!
完整的帳戶類代碼:
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);
}
}
您的數字可能看起來總是相同,因為在打印之前未更新本地變量。
確保在調用System.out.printf("%.2f%n",balance);
之前更新balance
值System.out.printf("%.2f%n",balance);
。
就像是:
balance = account.getBalance();
它不應該打印您創建的對象的余額而不僅僅是“平衡”:System.out.println(account.getBalance())?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.