Accumulator.java一、课程设计要求与目的
- 模仿个人银行账户管理系统的C++版本(第4章-第9章),使用Java语言重新实现该系统,比较C++与Java在实现上的异同,熟练掌握Java基础及语法。
- 根据业务需求的增加,对个人银行账户管理系统进行功能上的完善,利用面向对象的思想对代码进行重构,体会抽象、封装、继承、多态等特性在实际系统中的应用,掌握构造可复用、可扩展、可维护软件的基本技能。
二、课程设计版本记录
个人银行账户管理系统版本0.1(对应第4章记录)
系统需求
- 创建银行账户
- 对账户进行操作
- 输出每一条操作记录
- 输出账户余额
系统设计
- 设计SavingAccount类,包括id(帐号),lastDate(上次变更日期),balance(余额),rate(存款的年利率),accumulation(余额按日累加之和)等数据成员
- 设计私有成员record记录一笔账
- 设计相关函数,实现基本操作,如:存入现金,取出现金,利息结算等
系统实现
设计类的成员变量(id,lastDate,balance,rate,accumulation)
设计构造函数,完成账户的创建,完成创建时输出“….is created”语句提示账户创建成功
设计方法(记录一笔账,存入现金,取出现金,结算利息,显示账户信息)
record方法
记录每一笔帐,引用accumulata方法进行现金余额累加,并更新日期、余额等。引用floor方法,进行向下取整。
accumulate方法
计算限定日期内的金额累加值
getxxxx系列 方法
返回id,balance,rate等信息
deposit方法
存入现金,调用record方法记录账单
withdraw方法
若取出现金比余额多,则报错,否则,取出现金,调用record方法记录账单
settle方法
结算利息,分别调用accumulate方法进行计算,用record方法进行记账
show方法
显示账户信息
系统测试
- 测试的功能有:账户的建立、存现金、取现金、计算利息等
- 结果1-2表示两个账户创建成功,实现账户建立功能
- 3-6行表示对两个账户进行存、取现金,计算年息等操作
- 7-8行表示输出两个账户的基本信息
体会心得
- 在Java中,类的定义和C++几乎一样,唯一不同的是没有标志着结束的分号
- Java中的方法都是在类中定义的,而C++可以在类外定义方法
- Java中没有C++中的作用域范围运算符“::”
- Java利用分隔符“.”可以做任何事情
个人银行账户管理系统版本0.2
系统需求
在私有变量中,增加属性total,记录所有账号的总金额
系统设计
total属于一个类的属性,设计为静态属性
定义
系统实现
record方法相关操作完善,每记录一笔账时total会同步更新
获取total
输出total
系统测试
- 测试的功能是:total实现记录账户总金额
- 最后一行输出两个账户余额的总和
体会心得
- 静态变量可以用于引用所有对象的公共属性
- 静态变量的优点:能使程序存储器高效,即节省内存
- 当一个变量被类的多个对象所共享,实现多个对象之间的通信,或用于记录已被创建的对象个数时,使用静态变量。
- 在此版本中total就是被多个对象共同使用,记录数据,所以设定为静态变量
- 静态方法可以直接通过:“类名.方法”的形式进行方法调用
- Java和c++的Static的使用方法类似
个人银行管理系统版本0.3
系统需求
- 账号id改为string类型
- 用数组创建账号
- 将日期改为年月日
系统设计
增加Date类
在SavingAccount类中使用Date类作为数据成员
系统实现
Date类
用isLeapYear方法判断是否为闰年
用show方法输出当前日期
用distance方法计算两个日期之间差几天
SavingAccount类
SavingAccount类中结算所有账户并输出各个账户信息
用error方法报告错误信息
Settle方法中引用Date类中的distance方法
系统测试
- 测试功能:账户建立、记录账单、报告错误信息等功能
- 1-2行表示账户创建成功
- 3-6行表示记录账单
- 7行表示报告错误信息
- 8-11行表示各个账户信息
- 12行输出总账单金额
体会心得
封装的好处:
- 编程更加简单,找对象、调方法更简单
- 让数据更安全
- 提高代码的复用性
通过类的组合,使代码间相互独立,但类与类之间又可以相互调用以实现更多的功能
个人银行账户管理系统版本0.4
系统需求
创建信用账户,实现透支、拥有欠款利息等功能
系统设计
抽象出父类Account,包括id,balance,total等数据成员,Account,record,error,getBalabce,getTotal,show等方法
- 派生出子类CreditAccount,包括acc,credit,rate,fee等数据成员,getDebt,getCredit,getRate,getFee,getAvailableCredit,deposit,withdraw,settle,show等方法
派生出子类Savingsaccount,包括acc,rate等数据成员,getRate,deposit,withdraw,settle等方法
增加Accumulate类,作为其他类的利息累加器,包括lastDate,value,sum等数据成员,Accumulator,getSum,change,reset等方法
系统实现
Accumulator类
getSum方法,计算按日累加的金额之和
change方法,更新数据
CreditAccount类
getAvailableCredit方法,获得可用信用
getDebt方法,获得欠款额
withdraw方法,取出现金增加判断条件,当所取金额与余额差大于信用额度时,无法借款,报错
settle方法,调用acc方法,结算利息和年费
系统测试
顺利完成建立账户,记录11月账单,结算信用卡,记录12月账单结算所有账户,输出各个账户信息等工作。
体会心得
- 继承减少代码的冗余,提高代码的运行速度,便于功能的拓展,为多态性提供了使用前提
- 抽象出父类使代码扩展性更高
- 父类中声明的private属性和方法,子类中是不能直接获取,但是仍然可以获取到父类中已经私有的属性和方法
- 子类继承父类后,仍可以声明自己特有的属性或方法,子类和父类不同于子类和集合的关系,实现功能的拓展
个人银行管理系统版本0.5
系统需求
- 用户可以通过输入自己选择操作功能
- 完善类间的关系
系统设计
- 将父类Account定义为抽象类
通过输入的语句判断操作,调用对应的方法
系统实现
Account变为抽象类
输入“d”,进行存入现金操作
输入“w”,进行取出现金操作
输入“s”,进行查询各账号信息操作
输入“c”,进行改变日期操作
输入“n”,进行进入下个月操作
系统测试
账号完成创建
用户操作输入提示
相关功能测试
c5:进入这个月5号
进行存入、取出现金、结算年息等操作
n:时间变为1月1日
s:显示所有账户的信息
e:退出测试程序
体会心得
- 功能的选择能更好地方便用户操作,提高工作效率
- 使用抽象类思想结合多态覆写的方法,使最后调用方法实现各项功能时更加简单易懂
个人银行管理系统版本0.6
系统需求
使用Java的容器代替数组,动态存储账户,提高操作效率
系统设计
导入ArrayList包,通过容器创建数组
系统实现
增加账户。用type变量区分账户类型,再分别进行相应操作。使用容器内置add方法增加新账户
用remove方法清空容器
系统测试
测试结果
增加账户(a)功能能正常完成账户创建工作
体会心得
- 使用Java封装好的容器可以提高运行速度
三、课程设计总结
1)功能:
基类Account
- 封装账户的基本信息,比如:id,balance等数据成员
- 设计一系列方法实现个人需求。比如用record方法实现记录一笔账功能,用error方法报告错误,用deposit,withdraw方法实现存、取现金,用settle方法计算利息、年费
SavingsAccount类
- 储蓄账号类
- 有存入现金、取出现金、结算利息等方法
CreditAccount类
- 信用账户类
- 相对SavingsAccount类增加获得欠款额,获得可用信用等方法
Date类
- 创建日期
- 获得当月有多少天
- 判断当年是否是闰年
- 计算两个日期之间差多少天
Accumulator类
- 进行累加操作
- 计算账户余额按日累加之和
2)面向对象思想
封装:通过类的组合,使代码间相互独立,但类与类之间又可以相互调用以实现更多的功能。并且使编程更简单,数据更安全,提高代码的复写性
继承:由父类Account派生出SavingAccount和CreditAccount类,继承减少代码的冗余,提高代码的运行速度,便于功能的拓展,为多态性提供了使用前提
多态:多态的方法重写能够帮助子类账户更好的实现其需求,也使系统更加的完善、严谨
3)问题及解决
数据输入
- Java中输入一般通过Scanner类来实现
- 创建Scanner对象,接受从控制台输入
Scanner input=new Scanner(System.in);
实验中要将所有类放到同一个package中,以便查找,调用,改进
Java与C++的不同点
- C++支持指针,而Java没有指针的概念。Java中起类似作用的是引用
- 操作符重载被认为是C++的突出特征,而Java不支持操作符重载
- C++支持多继承,而Java不支持多重继承,但允许一个类实现多个接口
Java类的继承
是通过extends关键字实现的,子类继承了父类,也将获得父类的全部成员变量和方法。extends 很好的体现了子类和父类的关系,即子类是对父类的扩展,子类是一种特殊的父类。
Java封装
通过访问限制符修改类中属性的可见性,从而限制对类中属性的访问
收获:
通过实验逐步了解到C++与Java编程的不同点,两者相互比较。在一次次的修改中,对Java编程有更深刻的认识,在应用中理解知识,并且更加了解Java的继承、封装、多态。一次次的报错、上网查询资料掌握了许多经常被忽略的小知识点。实验操作是学习编程语言必不可少的一步。会在接下来的学习中,勇于动手操作实验,提高动手能力。
四、附录
yh4.java
package yinhang;
class SavingsAccount{
private int id,lastDate;// 账号,上次变更余额的日期
private double balance,rate,accumulation;//余额,存款的年利率,余额按日累加之和
private void record(int date,double amount){ //记录一笔帐,date为日期,amount为金额,desc为说明
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.id = id;
this.balance = 0;
this.rate = rate;
this.lastDate = date;
this.accumulation = 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); //调用record,记录账单
}
//取出现金
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);
//cout << "#" << id << "\tBalance: " << balance;
}
}
public class yh4 {
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, 10000);
sa0.deposit(45, 5500);
sa1.withdraw(60, 4000);
//开户后第90天到了银行的计息日,结算所有账户的年息
sa0.settle(90);
sa1.settle(90);
//输出各个账户信息
sa0.show();
sa1.show();
//System.out.println(SavingsAccount.getTotal());
}
}
yh5.java
package yinhang5;
class SavingsAccount{
private int id,lastDate;//账号,上次变更余额的时期
private double balance,rate,accumulation;//余额,存款的年利率,余额按日累加之和
private static double total=0; //增加静态属性total,记录所有账户的总金额
private void record(int date, double amount){//记录一笔帐,date为日期,amount为金额,desc为说明
accumulation = accumulate(date);
lastDate = date;
amount = Math.floor(amount * 100 + 0.5) / 100;//保留小数点后两位
balance += amount;
total += amount;
System.out.printf(date + "\t#" + id + "\t" + amount + "\t" + balance +"\n");//输出余额
}
private double accumulate(int date){//获得到指定日期为止的存款金额按日累积值
return accumulation + balance * (date - lastDate);
}
//构造函数
public SavingsAccount(int date, int id, double rate){ //创建账户
this.id = id;
this.balance = 0;
this.rate = rate;
this.lastDate = date;
this.accumulation = 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 );
//cout << "#" << id << "\tBalance: " << balance;
}
}
public class yh5 {
public static void main(String[] args){
//建立几个账户
yinhang5.SavingsAccount sa0 = new yinhang5.SavingsAccount(1, 21325302, 0.015);
yinhang5.SavingsAccount sa1 = new yinhang5.SavingsAccount(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: " + yinhang5.SavingsAccount.getTotal() );
}
}
yh6.java
package yinhang6;
class SavingsAccount{
private String id; //账号
private Date lastDate; // 上次变更余额的时期
private double balance,rate,accumulation; //余额 存款的年利率 余额按日累加之和
private static double total=0; //所有账户的总金额
//构造函数
public SavingsAccount(final Date date, final 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+" is created");
}
//记录一笔帐,date为日期,amount为金额,desc为说明
private void record(final Date date, double amount, final 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 double accumulate(final Date date){
return accumulation + balance * date.distance(lastDate);
}
//报告错误信息
public final void error(final String msg)
{
System.out.println("Error(#" + id +"): " + msg );
}
public final String getId() { return id; }
public final double getBalance() { return balance; }
public final double getRate() { return rate; }
public static double getTotal() { return total; }
//存入现金
public void deposit(final Date date, double amount, final String desc){
record(date, amount, desc);
}
//取出现金
public void withdraw(final Date date, double amount, final String desc){
if (amount > getBalance())
error("not enough money");
else
record(date, -amount, desc);
}
//结算利息,每年1月1日调用一次该函数
public void settle(final Date date){
double interest = accumulate(date) * rate //计算年息
/ date.distance(new Date(date.getYear() - 1, 1, 1));
if (interest != 0)
record(date, interest, "interest");
accumulation = 0;
}
//显示账户信息
public final void show(){
System.out.print(id+"\tBalance: "+balance);
}
}
public class yh6 {
public static void main(String[] args) {
Date date = new Date(2008, 11, 1); //起始日期
//建立几个账户
SavingsAccount[] accounts= {
new SavingsAccount(date, "S3755217", 0.015),
new SavingsAccount(date, "02342342", 0.015)
};
final int n = accounts.length; // => sizeof(accounts) / sizeof(SavingsAccount); 账户总数
//11月份的几笔账目
accounts[0].deposit(new Date(2008, 11, 5), 5000, "salary");
accounts[1].deposit(new Date(2008, 11, 25), 10000, "sell stock 0323");
//12月份的几笔账目
accounts[0].deposit(new Date(2008, 12, 5), 5500, "salary");
accounts[1].withdraw(new Date(2008, 12, 20), 4000, "buy a laptop");
//错误信息
accounts[1].withdraw(new Date(2008,12,22), 10000000, "buy a car");
//结算所有账户并输出各个账户信息
for (int i = 0; i < n; i++) {
accounts[i].settle(new Date(2009, 1, 1));
accounts[i].show();
System.out.println();
}
System.out.println( "Total: "+ SavingsAccount.getTotal() );
}
}
Data.java
package yinhang6;
public class Date{ //日期类
public final int[] DAYS_BEFORE_MONTH = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
private int year,month,day,totalDays; //年 月 日 该日期是从公元元年1月1日开始的第几天
//用年、月、日构造日期
public Date(int year, int month, int day){
this.year=year;
this.month=month;
this.day=day;
if (day <= 0 || day > getMaxDay()) {
System.out.print( "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 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 Date date) {
return totalDays - date.totalDays;
}
}
account7.java
package yinhang7;
//账户类
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;
this.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 final double getBalance() { return balance; }
public static double getTotal() { return total; }
//显示账户信息
public void show(){
System.out.print(id + "\tBalance: " + balance);
}
}
//储蓄账户类
class SavingsAccount extends Account {
private Accumulator acc; //辅助计算利息的累加器
private double rate; //存款的年利率
//构造函数
public SavingsAccount(final Date date, final String id, double rate){
super(date, id);
//若一个父类只要有一个带参数的构造方法,
// 那么在写其子类的构造方法时必须先通过super调用父类的构造方法而且super只能写在子类构造方法体内的第一行。
this.rate=rate;
acc=new Accumulator(date,0);
}
public final 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());
}
}
//信用账户类
class CreditAccount extends Account {
private Accumulator acc; //辅助计算利息的累加器
private double credit,rate,fee; //信用额度 欠款的日利率 信用卡年费
//构造函数
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);
}
//获得欠款额
private final double getDebt() {
double balance = getBalance();
return (balance < 0 ? balance : 0);
}
public final double getCredit() { return credit; }
public final double getRate() { return rate; }
public final double getFee() { return fee; }
//获得可用信用
public final 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 final void show() {
super.show();
System.out.print("\tAvailable credit:" + getAvailableCredit());
}
}
public class account7 {
public static void main(String[] args){
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);
//11月份的几笔账目
sa1.deposit(new Date(2008, 11, 5), 5000, "salary");
ca.withdraw(new Date(2008, 11, 15), 2000, "buy a cell");
sa2.deposit(new Date(2008, 11, 25), 10000, "sell stock 0323");
//结算信用卡
ca.settle(new Date(2008, 12, 1));
//12月份的几笔账目
ca.deposit(new Date(2008, 12, 1), 2016, "repay the credit");
sa1.deposit(new Date(2008, 12, 5), 5500, "salary");
//结算所有账户
sa1.settle(new Date(2009, 1, 1));
sa2.settle(new Date(2009, 1, 1));
ca.settle(new Date(2009, 1, 1));
//输出各个账户信息
System.out.println();
sa1.show(); System.out.println();
sa2.show(); System.out.println();
ca.show(); System.out.println();
System.out.println( "Total: " + Account.getTotal() );
}
}
Accumulator.java
package yinhang7;
public class Accumulator { //将某个数值按日累加
private Date lastDate; //上次变更数值的时期
private double value,sum; //数值的当前值 数值按日累加之和
//构造函数,date为开始累加的日期,value为初始值
public Accumulator(final Date date, double value)
{
this.lastDate=date;
this.value=value;
this.sum=0;
}
//获得到日期date的累加结果
public final 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;
}
}
Data.java
package yinhang7;
public class Date{ //日期类
public final int[] DAYS_BEFORE_MONTH = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
private int year,month,day,totalDays; //年 月 日 该日期是从公元元年1月1日开始的第几天
//用年、月、日构造日期
public Date(int year, int month, int day)
{
this.year=year;
this.month=month;
this.day=day;
if (day <= 0 || day > getMaxDay()) {
System.out.print( "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 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 Date date)
{
return totalDays - date.totalDays;
}
}
account8.java
package yinhang8;
import java.util.Scanner;
//账户类
abstract 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;
this.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 final double getBalance() { return balance; }
public static double getTotal() { return total; }
//存入现金,date为日期,amount为金额,desc为款项说明
public abstract void deposit(final Date date, double amount, final String desc);
//取出现金,date为日期,amount为金额,desc为款项说明
public abstract void withdraw(final Date date, double amount,final String desc);
//结算(计算利息、年费等),每月结算一次,date为结算日期
public abstract void settle(final Date date);
//显示账户信息
public void show(){
System.out.println(id + "\tBalance: " + balance);
}
}
//储蓄账户类
class SavingsAccount extends Account {
private Accumulator acc; //辅助计算利息的累加器
private double rate; //存款的年利率
//构造函数
public SavingsAccount(final Date date, final String id, double rate){
super(date, id);
//若一个父类只要有一个带参数的构造方法,
// 那么在写其子类的构造方法时必须先通过super调用父类的构造方法而且super只能写在子类构造方法体内的第一行。
this.rate=rate;
acc=new Accumulator(date,0);
}
public final 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.operator(new Date(date.getYear() - 1, 1, 1));
if (interest != 0)
record(date, interest, "interest");
acc.reset(date, getBalance());
}
}
//信用账户类
class CreditAccount extends Account {
private Accumulator acc; //辅助计算利息的累加器
private double credit,rate,fee; //信用额度 欠款的日利率 信用卡年费
//构造函数
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);
}
//获得欠款额
private final double getDebt() {
double balance = getBalance();
return (balance < 0 ? balance : 0);
}
public final double getCredit() { return credit; }
public final double getRate() { return rate; }
public final double getFee() { return fee; }
//获得可用信用
public final 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 final void show() {
super.show();
System.out.println( "\tAvailable credit:" + getAvailableCredit() );
}
}
public class account8 {
public static void main(String[] args) {
// TODO Auto-generated method stub
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;// => sizeof(accounts) / sizeof(SavingsAccount); 账户总数
System.out.println( "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" );
char cmd;
do {
//显示日期和总金额
date.show();
System.out.println( "\tTotal: " +Account.getTotal() + "\tcommand> " );
int index, day;
double amount;
String desc;
@SuppressWarnings("resource")
Scanner input=new Scanner(System.in);
String c=input.next();
cmd=c.charAt(0);
switch (cmd) {
case 'd': //存入现金
index=input.nextInt();
amount=input.nextDouble();
desc=input.next();
accounts[index].deposit(date, amount, desc);
break;
case 'w': //取出现金
index=input.nextInt();
amount=input.nextDouble();
desc=input.next();
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();
}
break;
case 'c': //改变日期
day=input.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 < n; i++)
accounts[i].settle(date);
break;
}
} while (cmd != 'e');
}
}
Accumulator.java
package yinhang8;
//将某个数值按日累加
public class Accumulator {
private Date lastDate; //上次变更数值的时期
private double value,sum; //数值的当前值 数值按日累加之和
//构造函数,date为开始累加的日期,value为初始值
public Accumulator(final Date date, double value){
this.lastDate=date;
this.value=value;
this.sum=0;
}
//获得到日期date的累加结果
public final double getSum(final Date date) {
return sum + value * (date.operator(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;
}
}
Data.java
package yinhang8;
//日期类
public class Date{
public final int[] DAYS_BEFORE_MONTH = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
private int year,month,day,totalDays; //年 月 日 该日期是从公元元年1月1日开始的第几天
//用年、月、日构造日期
public Date(int year, int month, int day){
this.year=year;
this.month=month;
this.day=day;
if (day <= 0 || day > getMaxDay()) {
System.out.println("Invalid date: ");
show();
System.out.println("\n");
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 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.println( getYear() + "-" + getMonth() + "-" + getDay() );
}
//计算两个日期之间差多少天
public final int operator (final Date date) {
return totalDays - date.totalDays;
}
}
account9.java
package yinhang9;
import java.util.ArrayList;
import java.util.Scanner;
//账户类
abstract 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;
this.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 + "\n");
}
public final String getId() { return id; }
public final double getBalance() { return balance; }
public static double getTotal() { return total; }
//存入现金,date为日期,amount为金额,desc为款项说明
public abstract void deposit(final Date date, double amount, final String desc);
//取出现金,date为日期,amount为金额,desc为款项说明
public abstract void withdraw(final Date date, double amount,final String desc);
//结算(计算利息、年费等),每月结算一次,date为结算日期
public abstract void settle(final Date date);
//显示账户信息
public void show(){
System.out.println( id + "\tBalance: " + balance );
}
}
//储蓄账户类
class SavingsAccount extends Account {
private Accumulator acc; //辅助计算利息的累加器
private double rate; //存款的年利率
//构造函数
public SavingsAccount(final Date date, final String id, double rate){
super(date, id);
//若一个父类只要有一个带参数的构造方法,
// 那么在写其子类的构造方法时必须先通过super调用父类的构造方法才能完成子类的构造方法而且super只能写在子类构造方法体内的第一行。
this.rate=rate;
acc=new Accumulator(date,0);
}
public final 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.operator(new Date(date.getYear() - 1, 1, 1));
if (interest != 0)
record(date, interest, "interest");
acc.reset(date, getBalance());
}
}
//信用账户类
class CreditAccount extends Account {
private Accumulator acc; //辅助计算利息的累加器
private double credit,rate,fee; //信用额度 欠款的日利率 信用卡年费
//构造函数
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);
}
//获得欠款额
private final double getDebt() {
double balance = getBalance();
return (balance < 0 ? balance : 0);
}
public final double getCredit() { return credit; }
public final double getRate() { return rate; }
public final double getFee() { return fee; }
//获得可用信用
public final 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 final void show() {
super.show();
System.out.println( "\tAvailable credit:" + getAvailableCredit() );
}
}
public class account9 {
public static void main(String[] args) {
Date date=new Date(2008, 11, 1); //起始日期
//Array<Account *> accounts(0); 创建账户数组,元素个数为0
ArrayList<Account> accounts = new ArrayList<>();
System.out.println("(a)add account (d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" );
char cmd;
do {
//显示日期和总金额
date.show();
System.out.println("\tTotal: " +Account.getTotal() + "\tcommand> ");
char type;
int index, day;
double amount, credit, rate, fee;
String id, desc;
Account account;
@SuppressWarnings("resource")
Scanner input=new Scanner(System.in);
String c=input.next();
cmd=c.charAt(0);
switch (cmd) {
case 'a': //增加账户
Scanner inputt=new Scanner(System.in);
String t=inputt.next();
type=t.charAt(0);
id=input.next();
if (type == 's') {
rate=input.nextDouble();
account = new SavingsAccount(date, id, rate);
} else {
credit=input.nextDouble();
rate=input.nextDouble();
fee=input.nextDouble();
account = new CreditAccount(date, id, credit, rate, fee);
}
accounts.add(account);
break;
case 'd': //存入现金
index=input.nextInt();
amount=input.nextDouble();
desc=input.next();
accounts.get(index).deposit(date, amount, desc);
break;
case 'w': //取出现金
index=input.nextInt();
amount=input.nextDouble();
desc=input.next();
accounts.get(index).withdraw(date, amount, desc);
break;
case 's': //查询各账户信息
for (int i = 0; i < accounts.size(); i++) {
System.out.print("["+i+"] ");
accounts.get(i).show();
System.out.println();
}
break;
case 'c': //改变日期
day=input.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);
break;
}
} while (cmd != 'e');
for (int i = 0; i < accounts.size(); i++)
accounts.remove(i);
}
}
Accumulator.java
package yinhang9;
//将某个数值按日累加
public class Accumulator {
private Date lastDate; //上次变更数值的时期
private double value,sum; //数值的当前值 数值按日累加之和
//构造函数,date为开始累加的日期,value为初始值
public Accumulator(final Date date, double value){
this.lastDate=date;
this.value=value;
this.sum=0;
}
//获得到日期date的累加结果
public final double getSum(final Date date) {
return sum + value * (date.operator(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;
}
}
Date.java
package yinhang9;
//日期类
public class Date{
public final int[] DAYS_BEFORE_MONTH = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
private int year,month,day,totalDays; //年 月 日 该日期是从公元元年1月1日开始的第几天
//用年、月、日构造日期
public Date(int year, int month, int day){
this.year=year;
this.month=month;
this.day=day;
if (day <= 0 || day > getMaxDay()) {
System.out.println("Invalid date: ");
show();
System.out.println("\n");
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 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.println( getYear() + "-" + getMonth() + "-" + getDay() );
}
//计算两个日期之间差多少天
public final int operator (final Date date) {
return totalDays - date.totalDays;
}
}