设计一个名为Account的类:
* 一个名为id的int类型私有数据域(默认值为0)
* 一个名为balance的double类型私有数据域(默认值为0)
* 一个名为annualInterestRate的double类型私有数据域存储当前利率(默认值为0)。假设所有的账户都有相同的利 率。
* 一个名为dateCreated的Date类型的私有数据域,存储账户的开户日期。
* 一个用于创建默认账户的无参构造方法。
* 一个用于创建带特定的id和初始余额的账户的构造方法。
* id,balance和annualInterestRate的访问器和修改器。
* dateCreated的访问器。
* 一个名为getMonthlyInterestRate()的方法,返回月利率。
* 一个名为withDraw的方法,从账户提取特定数额。
* 一个名为deposit的方法向账户存储特定金额。
import java.util.Date;
public class Account {
private int id; // 余额
private double balance=0; // 利息
private double annualInterestRate=0; // 账户开户日期
private Date dataCreated;
public Account()
{
super();
this.dataCreated=new Date();
}
public Account(int id, double balance)
{
super();
this.id = id;
this.balance = balance;
this.dataCreated=new Date();
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public double getBalance()
{
return balance;
}
public void setBalance(double balance)
{
this.balance = balance;
}
public double getAnnualInterestRate()
{
return annualInterestRate;
}
public Date getDataCreated()
{
return dataCreated;
}
public void setAnnualInterestRate(double annualInterestRate)
{
this.annualInterestRate = annualInterestRate;
}
/**月利息--假设利息为年利息的话*/
public double getMinthlyInterestRate() throws Exception
{
if(this.annualInterestRate==0)
{
throw new Exception("还没设置利息呢");
}else {
return annualInterestRate/12;
}
}
/** 取钱 */
public void withDraw(double MoneyNum) throws Exception
{
if(MoneyNum>this.balance)
{
throw new Exception("没那么多钱");
}else
{
this.balance-=MoneyNum;
}
}
/**存钱*/
public void deposit(double MoneyNum)
{
this.balance+=MoneyNum;
}
/** 测试*/
public static void main(String[] args) throws Exception
{
Account myAccount=new Account(1122, 20000);
myAccount.setAnnualInterestRate(4.5/100);
myAccount.withDraw(2500);
myAccount.deposit(3000);
System.out.println("余额:"+myAccount.getBalance());
System.out.println("月利息:"+myAccount.getMinthlyInterestRate());
System.out.println("开户日期:"+myAccount.getDataCreated());
}
}