设计一个名为Account的类

设计一个名为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());
     }

}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值