jin-s
public class Account {
    private int id;                //账号
    private double balance;        //余额
    private double rate;        //存款的年利率
    private int lastDate;        //上次变更余额的时期
    private double accumulation;    //余额按日累加之和
    private static double total = 0;    //所有账户的总金额
    
    //记录一笔帐,date为日期,amount为金额,desc为说明
    private void record(int date, double amount)
    {
        accumulation = accumulate(date);
        lastDate = date;
        amount = Math.floor((amount * 100 + 0.5)) / 100;    //保留小数点后两位
        balance += amount;
        total += amount;
        System.out.println(date + "\t#" + id + "\t" + amount + "\t" + balance);
    }
    
    //获得到指定日期为止的存款金额按日累积值
    private double accumulate(int date)
    {
            return accumulation + balance * (date - lastDate);
    }
    
    //构造函数
    public Account(int date, int id, double rate)
    {
        this.lastDate = date;
        this.id = id;
        this.rate = rate;
        this.accumulation = 0;
        this.balance = 0;
        System.out.println(date + "\t#" + id + " is created");
    }
    public int getId() { return id; }
    public double getBalance() { return balance; }
    public double getRate() { return rate; }
    public static double getTotal() { return total; }

    //存入现金
    public void deposit(int date, double amount)
    {
        record(date, amount);
    }
    
    //取出现金
    public void withdraw(int date, double amount)
    {
        if (amount > getBalance())
            System.out.println("Error: not enough money");
        else
            record(date, -amount);
    }
    
    //结算利息,每年1月1日调用一次该函数
    public void settle(int date)
    {
        double interest = accumulate(date) * rate / 365;    //计算年息
        if (interest != 0)
            record(date, interest);
        accumulation = 0;
    }
    
    //显示账户信息
    public void show()
    {
        System.out.println("#" + id + "\tBalance: " + balance);
    }
}
public class main {
    public static void main(String[] args)
    {
    //建立几个账户
    Account sa0 = new Account(1,21325302,0.015);
    Account sa1 = new Account(1,58320212,0.015);
    //几笔账目
    sa0.deposit(5, 5000);
    sa1.deposit(25, 10000);
    sa0.deposit(45, 5500);
    sa1.withdraw(60, 4000);
    //开户后第90天到了银行的计息日,结算所有账户的年息
    sa0.settle(90);
    sa1.settle(90);
    //输出各个账户信息
    sa0.show();
    sa1.show();
    System.out.println("Total: " + Account.getTotal());
    }
}

1. java没有const关键字,c++中的const类似于java的final;

2.c++调用类的静态成员函数是类名::函数名,而Java是类名.函数名。

分类:

技术点:

相关文章:

  • 2021-11-01
  • 2021-10-10
  • 2019-10-21
  • 2021-11-05
  • 2022-01-10
  • 2018-04-01
  • 2022-01-24
  • 2021-07-20
猜你喜欢
  • 2022-01-01
  • 2022-01-01
  • 2022-01-01
  • 2021-12-08
  • 2021-11-01
  • 2021-09-27
相关资源
相似解决方案