Java帐户类程序

时间:2014-07-01 21:30:58

标签: java

我一遍又一遍地写了这个程序,有人可以告诉我我错过了什么,看起来它是一个小错误,我没有抓到。

这是我想要完成的事情:

设计一个名为Account的类,其中包含:

  • 帐户名为id的私有int数据字段(默认为0)
  • 名为帐户余额的私有双重数据字段(默认为0)
  • 一个名为annualInterestRate的私有双数据字段,用于存储 当前利率(默认为0)。假设所有帐户都相同 利率。
  • 名为dateCreated的私有日期数据字段,用于存储创建帐户的日期。一个创建默认帐户的无参数构造函数。
  • 创建帐户的构造函数 具体的id和初始余额。
  • id,balance和annualInterestRate的访问器和mutator方法。
  • dateCreated的访问方法
  • 一个名为getMonthlyInterestRate()的方法,它返回每月利率。
  • 一种名为withdraw的方法,该方法从帐户中提取指定金额。该方法名为deposit,将指定金额存入该帐户。
  • 写一个测试程序 创建一个帐户ID为1122的Account对象,余额为 20,000美元,年利率4.5%。使用withdraw方法 提取2,​​500美元,使用存款方式存入3,000美元和 打印balanace,每月利息和此日期 帐户已创建。

以下是我目前的情况:

import java.util.Date;

public class Assign22 {

public static void main(String[] args) {
    // TODO Auto-generated method stub

Account account1 = new Account(1122, 20000, .045);
account1.withdraw(2500);
account1.deposit(3000);
System.out.println("Date Created:" + java.util.Date dateCreated = new java.util.Date());
System.out.println("Account ID:" + account1.id);
System.out.println("Balance:" + account1.getBalance());
System.out.println("Interest Rate:" + account1.getAnnualInterestRate());
System.out.println("Balance after withdraw of 2500:" +       account1.getAnnualInterestRate());
System.out.println("Balance after deposit of 3000:" + account1.getAnnualInterestRate());
System.out.println("Monthly Interest:" + account1.id);

System.out.println("Process completed.");
}

class Account {
//define variables
private int id;
private double balance; // balance for account
private double annualInterestRate; //stores the current interest rate
private Date dateCreated; //stores the date account created

//no arg construtor
Account () {
    id = 0;
    balance = 0.0;
    annualInterestRate = 0.0;
}
//constructor with specific 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/mutator methods for id, balance, and annualInterestRate
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;
}
//accessor method for dateCreated
public void setDateCreated(Date newDateCreated) {
    dateCreated = newDateCreated;
}
//define method getMonthlyInterestRate
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;   
}
}

}

这是我收到的错误:不确定我错过了什么:

“令牌”Date“上的语法错误,在此令牌dateCreated之后无法解析为Assign22.main(Assign22.java:12)上的变量”

1 个答案:

答案 0 :(得分:3)

您不能将变量声明作为String连接表达式的一部分

System.out.println("Date Created:" + java.util.Date dateCreated = new java.util.Date());

将其拆分

java.util.Date dateCreated = new java.util.Date()
System.out.println("Date Created:" + dateCreated);