java(account类)

设计Account类

设计一个Account类,它包括:
一个名为id的int类型的私有数据域(默认值为0)
一个名为balance的double类型私有数据域(默认值为0)
一个名为AnnualInterestRate的double类型私有数据域存储当前利率(默认值为0)。假设所有账户都有相同的利率。
一个名为DateCreated的date类型的私有数据域 存储账号的开户日期。
一个用于创建默认账户的无参构造方法一个用于创建带特定id和初始余额的账户的构造方法
id、balance、AnnualInterestRate的访问器和修改器
DateCreated的访问器一个名为withDraw的方法,从账号提取特定数额。一个名为deposit的方法向账号存储特定数额。
编写一个程序,创建一个账号ID为1122、余额为20000美元、年利率为4.5%的Account对象。使用withdraw方法取款2500美元,使用deposit方法存款3000美元,然后打印余额、月利息以及这个账号的开户信息。

public class Account{  
    private int id=0;//建立id 私有域 
    private double balance=0;//建立 balance私有域  
    private double AnnualInterestRate=0;//建立当前利率私有域
    private date DateCreated;//建立日期私有域 
    public Account()
    {
     }//建立无参构造函数
     public Account(int id,double balance)
     {
       this.id=id;  
       this.balance=balance; 
       this.datecreated=new Date;
     }//建立可以带id和balance的构造函数 
     public int getID()
     {
       return id;
     } //私有成员id的访问器
     public void setID(int newid)
     {
       this.id=newid;
     } //私有成员id的修改
     public double getbalance()
     {
       return balance;
     } //私有成员balance的访问器方法
     public double AnnualInterestRate()
     {
        return AnnualInterestRate;
     } //私有成员AnnualInterestRate的访问器方法
     public date DateCreated()
     {
        return DateCreated;
     } //私有成员日期的访问器方法
     public void setbalance(double newbalance)
     {
        this.Balance=newblance;
     }
     //私有成员balance的修改器方法
     public void setAnnualInterestRate(double newAnnualInterestRate)
     {
         this.AnnualInterestRate=newAnnualInterestRate;
     } //私有成员AnnualInterestRate的修改器方法
     public double getMonthlyInterstRate()
     {
         return AnnualInterestRate/12;
     }//月利率访问器方法
     public void withdraw(double Moneynum)
     {
         if(Moneynum>this.balance)
            {
               return error;
            }
            else
            {
               this.balance=this.balance-Moneynum;
             }
         }//取钱
      public void deposit(double Moneynum)
      {
          this.balance=this.balance+Moneynum;
      }//存钱
 }
 public class Account
 {
     public static void main(String []args)
     {   
         Account myAccount=new Account(1122,200000);    
         myAccount.setAnnualInterestRate(4.5/100);  
         myAccount.withDraw(2500); 
         myAccount.deposit(3000);  
         System.out.println(“余额:”+myAccount.getbalance()+”月利息:”+myAccount.getMonthlyInterstRate()*myAccount.getbalance()+”开户日期:”+myAccount.getDateCreated()); 
     }
 } 
  • 12
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值