个人银行账户管理C++转Java

版本一,4_9
(1)这是最基本的类
(2)private,public等在Java里面需要单独写在每一个成员前面。
(3)Java里不允许普通函数(除抽象类,接口等)的声明与函数体分开,要写一起。
(4)Java里的输出是语句是System.out.println(变量名 + “字符串”);相当于“<<”改成了“+”
(5)accumulate函数在原C++文件里是double accumulate(int date) const,所以这里要给它加上final修饰。
(6)c++ 里面建立类的对象可以直接:类名 对象名(参数),而Java里面需要:类名 对象名 = new 类名(参数)

public class SavingsAccount {
    private int id;                //账号
    private double balance;       //余额
    private double rate;          //存款的年利率
    private int lastDate;         //上次变更余额的时期
    private double accumulation;  //余额按日累加之和

    //记录一笔账,date为日期,amount为金额
    private void record(int date, double amount) {
        accumulation = accumulate(date);
        lastDate = date;
        amount = Math.floor((amount * 100 + 0.5)) / 100;    //保留小数点后两位
        balance += amount;
        System.out.println(date + "\t#" + id + "\t" + amount + "\t" + balance);
    }

    //获得到指定日期为止的存款金额按日累计值
    private double accumulate(int date) {
        return accumulation + balance * (date - lastDate);
    }

    //构造函数
    public SavingsAccount(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 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);
    }

    //结算利息
    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 static void main(String[] args) {
        //建立几个账户
        SavingsAccount sa0 = new SavingsAccount(1,21325302,0.015);
        SavingsAccount sa1 = new SavingsAccount(1, 58320212, 0.015);

        //几笔账目
        sa0.deposit(5,5000);
        sa1.deposit(25, 3000);
        sa0.deposit(45,5500);
        sa1.withdraw(60, 4000);

        //开户后第90天到了银行的计息日,结算所有账户的年息
        sa0.settle(90);
        sa1.settle(90);

        //输出各个账户信息
        sa0.show();
        sa1.show();
    }
}

版本二,5_11
这个版本是增添静态属性与方法
1)Java里面没有const修饰符,有类似的final,但两者功能有所差异。
2)Java里的构造函数没有id(id)这种写法,大多是this.id=id。
3)c++调用类的静态成员函数是 类名::函数名,而Java是 类名.函数名。

public class SavingsAccount2 {
    //private:
    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;
        System.out.println(date + "\t#"+id + "\t" + amount + "\t" + balance);
    }
    //获得指定日期为止的存款金额按日累积值
    private final double accumulate(int date) {
        return accumulation + balance * (date - lastDate);
    }
    //public:
    //SavingsAccount类相关成员函数的实现
    public SavingsAccount2(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 final int getId(){ return  id;}
    public final double getBalance(){return balance;}
    public final 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 final void show()
    {
        System.out.println("#" + id + "\tBalance:" + balance);
    }
    //main:
    public static void main(String[] args){
        //建立几个账户
        SavingsAccount sa0 = new SavingsAccount(1,21325302,0.015);
        SavingsAccount sa1 = new SavingsAccount(1,58320212,0.015);

        //几笔账目
        sa0.deposit(5,5000);
        sa1.deposit(25,3000);
        sa0.deposit(45,5500);
        sa1.withdraw(60,4000);

        //开户后第90天到了银行的计息日,结算所有账户的年息
        sa0.settle(90);
        sa1.settle(90);

        //输出各个账户信息
        sa0.show();
        sa1.show();
    }
}

版本三,6_25
这个版本是增添字符串、对象数组。
Date类:

package SavingAccount3;

public class Date {
    //存储平年中某个月1日之前有多少天,为便于getMaxDay函数的实现,该数组多出一项
    final int DAYS_BEFORE_MONTH[] = {0,31,59,90,120,151,181,212,243,273,304,334,365};
    private int year;
    private int month;
    private int day;
    private int totalDays;    //该日期是从公元元年1月1日开始的第几天

    boolean isLeapYear(){       //判断当年是否为闰年
        return  year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }
    public Date(int year,int month,int day)
    {
        this.day = day;
        this.month = month;
        this.year = year;
        if (day <= 0 || day > getMaxDay()){
            System.out.println("Invalid date: ");
            show();
            System.out.println();
            System.exit(1);
        }
        int years = year - 1;
        totalDays = years * 365 + year / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day;
        if (isLeapYear() && month > 2)  totalDays++;
    }
    public int getYear(){return  year;}
    public int getMonth(){return month;}
    public int getDay(){return day;}
    public int getMaxDay()  //获得当月有多少天
    {
        if (isLeapYear() && month == 2)
            return 29;
        else
            return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];

    }
    public void show()             //输出当前日期
    {
        System.out.print(getYear() + "-" + getMonth() + "-" + getDay());
    }
                 //计算两个日期之间差多少天
    int distance(Date date)
    {
        return totalDays - date.totalDays;
    }
}

SavingsAccount类:

package SavingAccount3;

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

           //记录一笔账,date为日期,amount为金额,desc为说明
    private void  record(Date date,double amount,String desc)
    {
        accumulation = accumulate(date);
        lastDate = date;
        amount = Math.floor(amount * 100 + 0.5) / 100;   //保留小数点后两位
        balance += amount;
        total += amount;
        date.show();
        System.out.println("\t#"+ id + "\t" + amount + "\t" + balance + "\t" + desc);
    }
              //报告错误信息
    private void error(String msg)
    {
        System.out.println("Error(#" + id + "):" + msg);
    }
    //获得到指定日期为止的存款金额按日累积值
    private double accumulate (Date date)
    {
        return accumulation + balance * date.distance(lastDate);
    }

             //构造函数
    public SavingsAccount(Date date,String id,double rate)
    {
        this.id = id;
        this.balance = 0;
        this.rate = rate;
        this.lastDate = date;
        this.accumulation = 0;
             date.show();
             System.out.println( "\t#" + id + " created");
    }
    public String getId() {return id;}
    public double getBalance(){return balance;}
    public double getRate()  {return rate;}
    public static double getTotal(){return total;}

                   //存入现金
    public void deposit(Date date,double amount,String desc)
    {
        record(date,amount,desc);
    }
            //取出现金
    public void withdraw(Date date,double amount,String desc)
    {
        if(amount > getBalance())
            error("not enough money");
        else
            record(date,-amount,desc);
    }
             //结算利息,每年1月1日调用一次该函数
    public void settle(Date date)  //计算年息
    {
        double interest = accumulate(date) * rate / date .distance(new Date(date.getYear() - 1, 1,1));
        if(interest != 0)
            record(date,interest,"interst");
        accumulation = 0;
    }
           //显示账户信息
    public void show()
    {
        System.out.println(id + "\tBalance:" + balance);
    }
}

版本四,7_10
这个版本加入抽象类,来方便实例化两个有些相似的子类,大体方法是和c++一样的,只是一些关键字不太一样。继承与派生,抽象出父类,增添子类。
Date类:

package SavingsAccount4;

public class Date {//日期类
    //存储平年中某个月1日之前有多少天,为便于getMaxDay函数的实现,该数组多出一项
    final int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
    private int year;     //年
    private int month;    //月
    private int day;      //日
    private int totalDays; //该日期是从公元元年1月1日开始的第几天

    public boolean isLeapYear() {  //判断当年是否为闰年
        return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }
    public Date(int year, int month, int day)  //用年、月、日构造日期
    {
        this.day = day;
        this.month = month;
        this.year = year;
        if (day <= 0 || day > getMaxDay()) {
            System.out.println("Invalid date: ");
            show();
            System.out.println();
            System.exit(1);
        }
        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 int getDay() { return day; }
    public int getMaxDay()    //获得当月有多少天
    {
        if (isLeapYear() && month == 2)
            return 29;
        else
            return DAYS_BEFORE_MONTH[month]- DAYS_BEFORE_MONTH[month - 1];
    }
    public void show()       //输出当前日期
    {
        System.out.print( getYear() + "-" + getMonth() + "-" + getDay());//这里不用println
    }
    //计算两个日期之间差多少天
    public int distance(final Date date) {
        return totalDays - date.totalDays;
    }

}

Accumulator类:

package SavingsAccount4;

public class Accumulator {//将某个数值按日累加

    private Date lastDate; //上次变更数值的时期
    private double value;  //数值的当前值
    private double sum;       //数值按日累加之和

    //构造函数,date为开始累加的日期,value为初始值
    public Accumulator(final Date date, double value)
    {
        lastDate=date;
        this.value=value;
        sum=0;
    }
    //获得到日期date的累加结果
    public double getSum(final Date date){
        return sum + value * date.distance(lastDate);
    }

    //在date将数值变更为value
    public void change(final Date date, double value) {
        sum = getSum(date);
        lastDate = date;
        this.value = value;
    }

    //初始化,将日期变为date,数值变为value,累加器清零
    public void reset(final  Date date, double value) {
        lastDate = date;
        this.value = value;
        sum = 0;
    }
}

Account父类:

package SavingsAccount4;

public class Account {//账户类

    private String id; //帐号
    private double balance;    //余额
    private static double total = 0; //所有账户的总金额

    //供派生类调用的构造函数,id为账户
    protected Account(final Date date, final String id)
    {
        this.id=id;
        balance=0;
        date.show();
        System.out.println("\t#" + id + " created");
    }
    //记录一笔帐,date为日期,amount为金额,desc为说明
    protected void record(final Date date, double amount, final String desc)
    {
        amount = Math.floor(amount * 100 + 0.5) / 100; //保留小数点后两位
        balance += amount;
        total += amount;
        date.show();
        System.out.println( "\t#" + id + "\t" + amount + "\t" + balance + "\t" + desc );
    }
    //报告错误信息
    protected void error(final String msg)
    {
        System.out.println("Error(#" + id + "):" + msg);
    }

    public final String getId() { return id; }
    public double getBalance() { return balance; }
    public static double getTotal() { return total; }
    //显示账户信息
    public void show()
    {
        System.out.print(id + "\tBalance: " + balance);//这里不用println
    }
}

SavingsAccount子类:

package SavingsAccount4;

public class SavingsAccount extends Account {//储蓄账户类

    private Accumulator acc;   //辅助计算利息的累加器
    private double rate;      //存款的年利率

    //构造函数
    public SavingsAccount(final Date date,final String id, double rate)
    {
        super(date,id);
        this.rate = rate;
        acc=new Accumulator(date,0);
    }
    public double getRate() { return rate; }
    //存入现金
    public void deposit(final Date date, double amount,final String desc)
    {
        record(date, amount, desc);
        acc.change(date, getBalance());
    }
    //取出现金
    public void withdraw(final Date date, double amount,final String desc)
    {
        if (amount > getBalance()) {
            error("not enough money");
        } else {
            record(date, -amount, desc);
            acc.change(date, getBalance());
        }
    }
    //结算利息,每年1月1日调用一次该函数
    public void settle(final Date date)
    {
        double interest = acc.getSum(date) * rate / date.distance(new Date(date.getYear() - 1, 1, 1)); //计算年息
        if (interest != 0)
            record(date, interest, "interest");
        acc.reset(date, getBalance());
    }
}

CreditAccount子类:

package SavingsAccount4;

public class CreditAccount extends Account{//信用账户类

    private Accumulator acc;   //辅助计算利息的累加器
    private double credit;    //信用额度
    private double rate;      //欠款的日利率
    private double fee;          //信用卡年费

    private double getDebt() { //获得欠款额
        double balance = getBalance();
        return (balance < 0 ? balance : 0);
    }

    //构造函数
    public CreditAccount(final Date date,final String id, double credit, double rate, double fee)
    {
        super(date, id);
        this.credit=credit;
        this.rate=rate;
        this.fee=fee;
        acc = new Accumulator(date, 0);

    }
    public double getCredit() { return credit; }
    public double getRate() { return rate; }
    public double getFee() { return fee; }
    public double getAvailableCredit() {   //获得可用信用
        if (getBalance() < 0)
            return credit + getBalance();
        else
            return credit;
    }
    //存入现金
    public void deposit(final Date date, double amount,final String desc)
    {
        record(date, amount, desc);
        acc.change(date, getDebt());
    }
    //取出现金
    public void withdraw(final Date date, double amount,final String desc)
    {
        if (amount - getBalance() > credit) {
            error("not enough credit");
        } else {
            record(date, -amount, desc);
            acc.change(date, getDebt());
        }
    }
    //结算利息和年费,每月1日调用一次该函数
    public void settle(final Date date)
    {
        double interest = acc.getSum(date) * rate;
        if (interest != 0)
            record(date, interest, "interest");
        if (date.getMonth() == 1)
            record(date, -fee, "annual fee");
        acc.reset(date, getDebt());
    }
    public void show()
    {
        super.show();
        System.out.println( "\tAvailable credit:" + getAvailableCredit());
    }
}

主函数Run类:

package SavingsAccount4;

public class PersonalBank {

    public static void main(String[] args) {
        Date date = new Date(2008, 11, 1); //起始日期
        //建立几个账户
        //SavingsAccount sa1 = new SavingsAccount(date, "AAAAA", 0.015);
        //sa1.deposit(new Date(2008, 11, 5), 5000, "salary");
        //sa1.withdraw(new Date(2008, 12, 5), 500, "happy");
        //sa1.settle(new Date(2009, 1, 1));
        //System.out.println();
        //sa1.show(); System.out.println();
        //System.out.println();
        SavingsAccount sa2 = new SavingsAccount(date, "02342342", 0.015);
        CreditAccount ca = new CreditAccount(date, "BBBBB", 10000, 0.0005, 50);
        //11月份的几笔账目

        ca.withdraw(new Date(2008, 11, 15), 2000, "buy a cell");
        sa2.deposit(new Date(2008, 11, 25), 10000, "sell stock 0323");
        //结算信用卡
        //ca.show();
        //ca.withdraw(new Date(2008, 12, 1), 52000, "buy a car");
        //ca.settle(new Date(2008, 12, 30));
        //ca.settle(new Date(2008, 12, 31));
        //ca.settle(new Date(2009, 1, 1));
        //12月份的几笔账目
        //ca.show();
        //ca.deposit(new Date(2008, 12, 31), 2016, "repay the credit");

        //结算所有账户

        //sa2.settle(new Date(2009, 1, 1));
        //ca.settle(new Date(2009, 1, 1));
        //输出各个账户信息
        //System.out.println();

        sa2.show(); System.out.println();
        ca.show();
        System.out.println();
        System.out.println( "Total: " + Account.getTotal());
    }
}

版本五:8_8
这个版本是多态特性。
有个新知识点就是如何用java从键盘中接收一个字符。而java原本并不支持接收单个字符。所以可以先接收一个字符串,然后再取字符串的第一个字符即可。
还有就是java不能进行运算符重载!
还有就是注意抽象类的运用!

批量操作和统一接口,因为对象类型不一样,还要求统一实现,就用到虚函数,而函数接口设置为一样,在函数体额外加判断条件,来达到接口统一,更利于程序的封装性,安全性。
Java里面的输入输出很有趣,也是调用类库,比如java.text.DecimalFormat可以控制输出小数点,java.io.IOException和java.util.*则是输入的专用库,每种类型的输入还不一样,真的很方便啊!
Date类和Accumulator类和之前一样,CreditAccount只有show函数不太一样。

package SavingAccount5;

import java.text.DecimalFormat;

abstract public class Account {//账户类

    private String id; //帐号
    private double balance;    //余额
    private static double total = 0; //所有账户的总金额

    //供派生类调用的构造函数,id为账户
    protected Account(final Date date, final String id)
    {
        this.id=id;
        balance=0;
        date.show();
        System.out.println("\t#" + id + " created");
    }
    //记录一笔帐,date为日期,amount为金额,desc为说明
    protected void record(final Date date, double amount, final String desc)
    {
        amount = Math.floor(amount * 100 + 0.5) / 100; //保留小数点后两位
        balance += amount;
        total += amount;
        date.show();
        System.out.println( "\t#" + id + "\t" + amount + "\t" + df.format(balance) + "\t" + desc );
    }
    //报告错误信息
    protected void error(final String msg)
    {
        System.out.println("Error(#" + id + "):" + msg);
    }
    static DecimalFormat df = new DecimalFormat("0.0");
    public final String getId() { return id; }
    public double getBalance() { return balance; }
    public static double getTotal() { return total; }
    //存入现金,date为日期,amount为金额,desc为款项说明
    abstract void deposit(final Date date, double amount,final String desc);
    //取出现金,date为日期,amount为金额,desc为款项说明
    abstract void withdraw(final Date date, double amount,final String desc);
    //结算(计算利息、年费等),每月结算一次,date为结算日期
    abstract void settle(final Date date);
    //显示账户信息
    public void show(){
        System.out.print(id + "\tBalance: " + df.format(balance));
    }
}

SavingsAccount子类:

package SavingAccount5;

public class SavingsAccount extends Account {//储蓄账户类

    private Accumulator acc;   //辅助计算利息的累加器
    private double rate;      //存款的年利率

    //构造函数
    public SavingsAccount(final Date date,final String id, double rate)
    {
        super(date,id);
        this.rate = rate;
        acc=new Accumulator(date,0);
    }
    public double getRate() { return rate; }
    //存入现金
    public void deposit(final Date date, double amount,final String desc)
    {
        record(date, amount, desc);
        acc.change(date, getBalance());
    }
    //取出现金
    public void withdraw(final Date date, double amount,final String desc)
    {
        if (amount > getBalance()) {
            error("not enough money");
        } else {
            record(date, -amount, desc);
            acc.change(date, getBalance());
        }
    }
    //结算利息,每年1月1日调用一次该函数
    public void settle(final Date date)
    {
        if(date.getMonth() == 1) {
            double interest = acc.getSum(date) * rate / date.distance(new Date(date.getYear() - 1, 1, 1)); //计算年息
            if (interest != 0)
                record(date, interest, "interest");
            acc.reset(date, getBalance());
        }
    }
}

/*CreditAccount:
public void show()
        {
        super.show();
        System.out.print( "\t\tAvailable credit:" + getAvailableCredit());
        }*/

主函数Run类:

package SavingAccount5;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;
//8_8.cpp
public class PersonalBank {

    public static void main(String[] args) throws IOException {
        Date date = new Date(2008, 11, 1); //起始日期
        //建立几个账户
        SavingsAccount sa1 = new SavingsAccount(date, "S3755217", 0.015);
        SavingsAccount sa2 = new SavingsAccount(date, "02342342", 0.015);
        CreditAccount ca = new CreditAccount(date, "C5392394", 10000, 0.0005, 50);
        Account accounts[] = { sa1, sa2, ca };
        final int n = accounts.length; //账户总数

        System.out.println("(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit");
        char cmd;
        Scanner in=new Scanner(System.in); //使用Scanner类定义对象
        int index, day;
        double amount;
        String desc;
        DecimalFormat df = new DecimalFormat("0.0");
        do {
            //显示日期和总金额
            date.show();
            System.out.print("\tTotal: " + df.format(Account.getTotal()) + "\tcommand> ");

            cmd = (char)System.in.read();
            switch (cmd) {
                case 'd':  //存入现金
                    index = in.nextInt();
                    amount = in.nextDouble();
                    desc = in.nextLine();
                    accounts[index].deposit(date, amount, desc);
                    break;
                case 'w':  //取出现金
                    index = in.nextInt();
                    amount = in.nextDouble();
                    desc = in.nextLine();
                    accounts[index].withdraw(date, amount, desc);
                    break;
                case 's':  //查询各账户信息
                    for (int i = 0; i < n; i++) {
                        System.out.print("[" + i + "] ");
                        accounts[i].show();
                        System.out.println();
                        if(i<(n-1)) {System.in.read();}
                    }
                    break;
                case 'c':  //改变日期
                    day = in.nextInt();
                    if (day < date.getDay())
                        System.out.print("You cannot specify a previous day");
                    else if (day > date.getMaxDay())
                        System.out.print("Invalid day");
                    else
                        date = new Date(date.getYear(), date.getMonth(), day);
                    break;
                case 'n':  //进入下个月
                    if (date.getMonth() == 12)
                        date = new Date(date.getYear() + 1, 1, 1);
                    else
                        date = new Date(date.getYear(), date.getMonth() + 1, 1);
                    for (int i = 0; i < n; i++) {
                        accounts[i].settle(date);
                        if(i<(n-1)) {System.in.read();}
                    }
                    break;
            }
        } while (cmd != 'e');
        in.close();
    }
}

版本六:9_16
C++里面有动态数组模板Array,可以动态添加对象,java里面有ArrayList,对于这个一开始真的很烦,要去不停的查询,因为他是类库里的东西。各种方法都有。只是我不会用罢了,所以这次的改编基本都在查资料。不过ArrayList确实很方便使用。

package SavingAccount6;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;
//9_16.cpp
public class PersonalBank {

    public static void main(String[] args) throws IOException {
        Date date = new Date(2008, 11, 1); //起始日期
        ArrayList<Account> accounts = new ArrayList<Account>(0);//创建账户数组,元素个数为0
        //使用Account来构造ArrayList
        DecimalFormat df = new DecimalFormat("0.0");
        System.out.println("(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit");
        char cmd;
        Scanner in=new Scanner(System.in); //使用Scanner类定义对象
        do {
            //显示日期和总金额
            date.show();
            System.out.print("\tTotal: " + df.format(Account.getTotal()) + "\tcommand> ");
            char type;
            int index, day;
            double amount,credit,rate,fee;
            String id,desc;
            Account account;

            cmd = (char)System.in.read();
            if(cmd != 'a' && cmd != 'd' && cmd != 'w' && cmd != 's' && cmd != 'c' && cmd != 'n' && cmd != 'e')break;
            switch (cmd) {
                case 'a':  //增加账户
                    System.in.read();
                    type = (char)System.in.read();
                    id = in.next();
                    if (type == 's') {
                        rate = in.nextDouble();
                        account = new SavingsAccount(date, id, rate);
                    } else {
                        credit = in.nextDouble();
                        rate = in.nextDouble();
                        fee = in.nextDouble();
                        account = new CreditAccount(date, id, credit, rate, fee);
                    }
                    accounts.add(account);//添加元素到表尾
                    accounts.trimToSize();//去掉系统多申请的空间
                    break;
                case 'd':  //存入现金
                    index = in.nextInt();
                    amount = in.nextDouble();
                    desc = in.nextLine();
                    accounts.get(index).deposit(date, amount, desc);//accounts.get()返回下标为该数的存的值,所以返回的就是一个Account类
                    break;
                case 'w':  //取出现金
                    index = in.nextInt();
                    amount = in.nextDouble();
                    desc = in.nextLine();
                    accounts.get(index).withdraw(date, amount, desc);
                    break;
                case 's':  //查询各账户信息
                    for (int i = 0; i < accounts.size(); i++) {//size方法返回大小
                        System.out.print("[" + i + "] ");
                        accounts.get(i).show();
                        System.out.println();
                        if(i<(accounts.size()-1)) {System.in.read();}
                    }
                    break;
                case 'c':  //改变日期
                    day = in.nextInt();
                    if (day < date.getDay())
                        System.out.println("You cannot specify a previous day");
                    else if (day > date.getMaxDay())
                        System.out.println("Invalid day");
                    else
                        date = new Date(date.getYear(), date.getMonth(), day);
                    break;
                case 'n':  //进入下个月
                    if (date.getMonth() == 12)
                        date = new Date(date.getYear() + 1, 1, 1);
                    else
                        date = new Date(date.getYear(), date.getMonth() + 1, 1);
                    for (int i = 0; i < accounts.size(); i++) {
                        accounts.get(i).settle(date);
                        if(i<(accounts.size()-1)) {System.in.read();}
                    }
                    break;
            }
        } while (cmd != 'e');
        System.out.println("Closed!");
        in.close();
        for (int i = 0; i < accounts.size(); i++)
            accounts.remove(i);//删除指定位置的元素
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值