银行账户实验-1.2

实验目的与要求

  • 模仿个人银行账户管理系统的C++版本(第4章-第8章),使用Java语言重新实现该系统,比较C++与Java在实现上的异同,熟练掌握Java基础及语法。
  • 根据系统需求的演化,逐步完善个人银行账户管理系统的功能,改进代码,体会面向对象思想的封装、继承、多态特性在实际系统中的应用,初步掌握使用Java编写可复用、可扩展、可维护代码的基本技能。

实验进展记录

  • 个人银行管理系统版本1.2(chapter 6)

实现:

  • 一个人拥有多个活期账户,账户包括账号(id)、余额(balance)、年利率(rate)等信息
  • 可以实现查询当前账户信息、存款、取款、结算利息的功能,增加 报告错位 的功能。
  • 为每笔账户增加说明性文字的功能。
  • 增加一个日期功能,其中的子功能有:存储一个日期,返回年月日,判断是否为闰年,获取两日只差的天数,显示日期的功能。
    源码:
  • 日期类
package experimentWork.bankAccount.bank6;

public class data {

    private int year;   // 年
    private int month;  //月
    private int day;    //日
    private int totalDays;  //该日期是从公元元年1月1日开始的第几天
    //存储平年中某个月1日前有多少天
    final int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };

    public data(int _year, int _month, int _day){
        year = _year;
        month = _month;
        day = _day;
        if (day <= 0 || day > getMaxDay()) {
            System.out.println("Invalid date: ");
            show();
            System.out.println();
            System.exit(0);
        }
        int years = year - 1;
        totalDays = years * 365 + years / 4 - years / 100 + years / 400
                + DAYS_BEFORE_MONTH[month - 1] + day;
        if (isLeapYear() && month > 2) totalDays++;
    }

    public final int getYear() {
        return year;
    }

    public final int getMonth() {
        return month;
    }

    public final int getDay() {
        return day;
    }

    public final int getMaxDay() {
        if(isLeapYear() && month == 2)
            return 29;
        else
            return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];
    }

//    判断是否是闰年
    public final boolean isLeapYear(){
        return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
    }

//    输出当前日期
    public final void show(){
        System.out.print(getYear() + "-" + getMonth() + "-" + getDay());
    }

    public final int distance(final data data){
        return totalDays - data.totalDays;
    }

}
  • 实现类
package experimentWork.bankAccount.bank6;

class SavingAccount {
    private String id;     //账号
    private double balance;     //余额
    private double rate;        //存款的利率
    private data lastDate;       //上次变更的时期
    private double accumulation;        //余额按日累加之和
    static double total = 0;        //所有账户的总金额

    //构造函数
    public SavingAccount(data _data, String _id, double _rate) {
        lastDate = _data;
        id = _id;
        rate = _rate;
        System.out.println("			#" + id + " is created");
    }
    public final data getLastDate() {
        return lastDate;
    }

    public final String getId() {
        return id;
    }

    public final double getAccumulation() {
        return accumulation;
    }

    public final double getRate() {
        return rate;
    }

    public final double getBalance() {
        return balance;
    }

    public static final double getTotal() {
        return total;
    }


    //存入现金
    public void deposit(final data _data, double _amount, final String salary){
        record(_data, _amount, salary);
    }
    //取出现金
    public void withdraw(data _data, double _amount, String desc){
        if(_amount > getBalance()){
            System.out.println("Error: not enough money");
        } else{
            record(_data, -_amount, desc);
        }
    }
    //结算利息,每年1月1日调用一次该函数
    public void settle(data _data){
        double interest = accumulata(_data) * rate /365;    //计算年息
        if(interest != 0){
            record(_data, interest, "interest");
        }
        accumulation = 0;
    }
    public void show(){
        System.out.println("			#" + id + "	Balance:" + balance);
    }
    //记录一笔账,data为日期,amount为金额,desc为说明
    private void record(final data _data, double amount, String desc) {
        accumulation = accumulata(_data);
        lastDate = _data;
        amount = Math.floor(amount * 100 + 0.5) / 100;    //未完成 保留小数点后两位
        balance += amount;
        total += amount;
        _data.show();
        System.out.println("	#" + id + "	" + amount + "	" + balance + "	" + desc);
    }
    
    //获得到指定日期为止的存款金额按日累积值
    public double accumulata(final data _data) {//const
        return accumulation + balance * _data.distance(lastDate);
    }

}
public class part_6 {

    public static void main(String args[]){
        data Date = new data(2008, 11, 1);
        SavingAccount account[] = new SavingAccount[2];

        account[0] = new SavingAccount(Date, "S3755217", 0.015);
        account[1] = new SavingAccount(Date, "02342342", 0.015);

        final int n= account.length;
        account[0].deposit(new data(2008, 11, 5), 5000, "salary");
        account[1].deposit(new data(2008, 11, 25), 10000, "sell stock 0323");

        account[0].deposit(new data(2008, 12, 5), 5500, "salary");
        account[1].withdraw(new data(2008, 12, 20), 4000, "buy a laptop");

        System.out.println();

        for(int i = 0; i <= n - 1; ++i) {
            account[i].settle(new data(2009, 1, 1));
            account[i].show();
            System.out.println();
        }
        System.out.println("Total: " + SavingAccount.getTotal());
    }
}

总结

  1. 类的组合可以使一些功能实现更加的容易以及管理,此版本中利用日期类使得有关日期的操作可以和实现类相互独立,无需关心日期类的具体实现,通过直接调用方法来实现整体功能;
  2. 使用对象数组来实现一些操作可以通过循环来实现,减少代码量;
  3. c++与java在功能实现上大同小异,两个语言之间的差别体现在一些细节处理上面。
成本最低的事情是学习,性价比最高的事情也是学习!
原文地址:https://www.cnblogs.com/qiaofutu/p/13956056.html