简述是创建一个ID为1122,余额为£20000的年利率为4.5%,使用取款方法为£2500的存款方法和存款为£3000的帐户对象,以及打印余额,蒙蒂利息和日期。该帐户已创建。
我已经写了下面的代码,但是在主要方法中,初始余额是错误的,应该是20000英镑,而是20500英镑,取款和存款也是错误的。该金额应该是:提款= 17,500英镑,存款= 20,500英镑。关于如何重新爱这个有什么建议吗?
package Account;
import java.util.Date;
class Account {
private int id;
private double balance; //balance for account
private double annualInterestRate; //store interest rate
private Date dateCreated; //store date account created
// no arg constructor for default account
Account() {
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}
//constructor with specfic id and initial balance
Account (int newId, double newBalance) {
id = newId;
balance = newBalance;
}
//
Account (int newId, double newBalance, double newAnnualInterestRate) {
id = newId;
balance = newBalance;
annualInterestRate = newAnnualInterestRate;
}
//accessor and mutator methods
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setId (int newId) {
id = newId;
}
public void setBalance (double newBalance) {
balance = newBalance;
}
public void setAnnualInterestRate (double newAnnualInterestRate) {
annualInterestRate = newAnnualInterestRate;
}
public void seDateCreated (Date newDateCreated) {
dateCreated = newDateCreated;
}
//Method for monthly interest
double getMonthlyInterestRate() {
return annualInterestRate/12;
}
//Define method withdraw
double withdraw (double amount) {
return balance -= amount;
}
//Define method deposit
double deposit(double amount) {
return balance += amount;
}
public static void main(String[] args) {
java.util.Date dateCreated = new java.util.Date();
Account account1 = new Account (1122, 20000, 0.045); //
//account1.withdraw(2500);
//account1.deposit(3000);
System.out.println("----------------------------------------");
System.out.println(" Welcome to your online account!");
System.out.println("Please find your account details below");
System.out.println("----------------------------------------");
System.out.println("Account ID:" + " " + account1.id);
System.out.println("Initial Balance:" + account1.getBalance());
System.out.println("Balance after Withdraw:" + " " + account1.withdraw(2500));
System.out.println("Balance after deposit" + " " + account1.deposit(3000));
System.out.println("Interest Rate:" + " " + account1.getAnnualInterestRate());
System.out.println("Monthly Interest:" + " " + "£" + account1.getMonthlyInterestRate());
System.out.println("Date Account was Created:" + " " + dateCreated);
System.out.println("------------------------");
System.out.println("The Process is complete");
System.out.println("------------------------");
}
}
答案 0 :(得分:2)
您需要注释掉/删除以下几行:
public static void main(String[] args) {
java.util.Date dateCreated = new java.util.Date();
Account account1 = new Account (1122, 20000, 0.045); //
//account1.withdraw(2500);
//account1.deposit(3000);
您两次调用提款/存款(稍后在打印语句中再次进行。)