package bank8_8;
import java.lang.reflect.Array;
import java.io.IOException;
import java.util.*;
class Date {
// 日期类
private int year; // 年
private int month; // 月
private int day; // 日
private int totalDays; // 该日期是从公元元年1月1日开始的第几天
private static int[] DAYS_BEFORE_MONTH = new int[] {
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
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++;
}
final int getYear() {
return year;
}
final int getMonth() {
return month;
}
final int getDay() {
return day;
}
final int getMaxDay() {
if (isLeapYear() && month == 2)
return 29;
else
return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];
} // 获得当月有多少天
final boolean isLeapYear() {
// 判断当年是否为闰年
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
final void show() {
System.out.print(getYear() + "-" + getMonth() + "-" + getDay());
}// 输出当前日期
// 计算两个日期之间差多少天
final int distance(final Date date) {
return totalDays - date.totalDays;
}
};
abstract class Account {
// 储蓄账户类
private String id; // 账号
private double balance; // 余额
private static double total = 0; // 所有账户的总金额
// 记录一笔帐,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 Account(final Date date, final String id) {
this.id = id;
balance = 0;
date.show();
System.out.println("\t#" + id + " created");
}
final String getId() {
return id;
}
final double getBalance() {
return balance;
}
static double getTotal() {
return total;
}
protected final void error(final String msg) {
System.out.println("Error(#" + id + "): " + msg);
}
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);
// 显示账户信息
void show() {
System.out.println(id + "\tBalance: " + balance);
}
};
class SavingsAccount extends Account {
// 储蓄账户类
private Accumulator acc; // 辅助计算利息的累加器
private double rate; // 存款的年利率
// 构造函数
SavingsAccount(final Date date, final String id, double rate) {
super(date, id);
this.rate = rate;
acc = new Accumulator(date, 0);
}
final double getRate() {
return rate;
}
// 存入现金
void deposit(final Date date, double amount, final String desc) {
record(date, amount, desc);
acc.change(date, getBalance());
}
// 取出现金
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日调用一次该函数
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 {
// 信用账户类
final Accumulator acc; // 辅助计算利息的累加器
private double credit; // 信用额度
private double rate; // 欠款的日利率
private double fee; // 信用卡年费
final double getDebt() {
// 获得欠款额
double balance = getBalance();
return (balance < 0 ? balance : 0);
}
// 构造函数
CreditAccount(final Date date, final String id, double credit, double rate, double fee) {
super(date, id);
this.credit = credit;
this.rate = rate;
acc = new Accumulator(date, 0);
}
final double getCredit() {
return credit;
}
final double getRate() {
return rate;
}
final double getFee() {
return fee;
}
final double getAvailableCredit() {
// 获得可用信用
if (getBalance() < 0)
return credit + getBalance();
else
return credit;
}
// 存入现金
void deposit(final Date date, double amount, final String desc) {
record(date, amount, desc);
acc.change(date, getDebt());
}
// 取出现金
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日调用一次该函数
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());
}
void show() {
super.show();
System.out.print("\tAvailable credit:" + getAvailableCredit());
}
};
class Accumulator {
// 将某个数值按日累加
private Date lastDate; // 上次变更数值的时期
private double value; // 数值的当前值
private double sum; // 数值按日累加之和
// 构造函数,date为开始累加的日期,value为初始值
Accumulator(final Date date, double value) {
lastDate = date;
this.value = value;
sum = 0;
}
// 获得到日期date的累加结果
final double getSum(final Date date) {
return sum + value * date.distance(lastDate);
}
// 在date将数值变更为value
void change(final Date date, double value) {
sum = getSum(date);
lastDate = date;
this.value = value;
}
// 初始化,将日期变为date,数值变为value,累加器清零
void reset(final Date date, double value) {
lastDate = date;
this.value = value;
sum = 0;
}
};
public class SavingAccount {
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 = new Account[] {
sa1, sa2, ca };
final int n = Array.getLength(accounts); // 账户总数
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);
do {
// 显示日期和总金额
date.show();
System.out.print("\tTotal: " + Account.getTotal() + "\tcommand> ");
int index, day;
double amount;
String desc;
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();
}
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);
break;
}
} while (cmd != 'e');
in.close();
}
}
比较C++与Java:
package bank9_16;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
class Date {
// 日期类
private int year; // 年
private int month; // 月
private int day; // 日
private int totalDays; // 该日期是从公元元年1月1日开始的第几天
private static int[] DAYS_BEFORE_MONTH = new int[] {
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
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++;
}
final int getYear() {
return year;
}
final int getMonth() {
return month;
}
final int getDay() {
return day;
}
final int getMaxDay() {
if (isLeapYear() && month == 2)
return 29;
else
return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];
} // 获得当月有多少天
final boolean isLeapYear() {
// 判断当年是否为闰年
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
final void show() {
System.out.print(getYear() + "-" + getMonth() + "-" + getDay());
}// 输出当前日期
// 计算两个日期之间差多少天
final int distance(final Date date) {
return totalDays - date.totalDays;
}
};
abstract class Account {
// 储蓄账户类
private String id; // 账号
private double balance; // 余额
private static double total = 0; // 所有账户的总金额
// 记录一笔帐,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 Account(final Date date, final String id) {
this.id = id;
balance = 0;
date.show();
System.out.println("\t#" + id + " created");
}
final String getId() {
return id;
}
final double getBalance() {
return balance;
}
static double getTotal() {
return total;
}
protected final void error(final String msg) {
System.out.println("Error(#" + id + "): " + msg);
}
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);
// 显示账户信息
void show() {
System.out.println(id + "\tBalance: " + balance);
}
};
class SavingsAccount extends Account {
// 储蓄账户类
private Accumulator acc; // 辅助计算利息的累加器
private double rate; // 存款的年利率
// 构造函数
SavingsAccount(final Date date, final String id, double rate) {
super(date, id);
this.rate = rate;
acc = new Accumulator(date, 0);
}
final double getRate() {
return rate;
}
// 存入现金
void deposit(final Date date, double amount, final String desc) {
record(date, amount, desc);
acc.change(date, getBalance());
}
// 取出现金
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日调用一次该函数
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 {
// 信用账户类
final Accumulator acc; // 辅助计算利息的累加器
private double credit; // 信用额度
private double rate; // 欠款的日利率
private double fee; // 信用卡年费
final double getDebt() {
// 获得欠款额
double balance = getBalance();
return (balance < 0 ? balance : 0);
}
// 构造函数
CreditAccount(final Date date, final String id, double credit, double rate, double fee) {
super(date, id);
this.credit = credit;
this.rate = rate;
acc = new Accumulator(date, 0);
}
final double getCredit() {
return credit;
}
final double getRate() {
return rate;
}
final double getFee() {
return fee;
}
final double getAvailableCredit() {
// 获得可用信用
if (getBalance() < 0)
// 存入现金
void deposit(final Date date, double amount, final String desc) {
record(date, amount, desc);
acc.change(date, getDebt());
}
// 取出现金
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日调用一次该函数
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());
}
void show() {
super.show();
System.out.print("\tAvailable credit:" + getAvailableCredit());
}
};
class Accumulator {
// 将某个数值按日累加
private Date lastDate; // 上次变更数值的时期
private double value; // 数值的当前值
private double sum; // 数值按日累加之和
// 构造函数,date为开始累加的日期,value为初始值
Accumulator(final Date date, double value) {
lastDate = date;
this.value = value;
sum = 0;
}
// 获得到日期date的累加结果
final double getSum(final Date date) {
return sum + value * date.distance(lastDate);
}
// 在date将数值变更为value
void change(final Date date, double value) {
sum = getSum(date);
lastDate = date;
this.value = value;
}
// 初始化,将日期变为date,数值变为value,累加器清零
void reset(final Date date, double value) {
lastDate = date;
this.value = value;
sum = 0;
}
};
public class SvAccount {
public static void main(String[] args) throws IOException {
Date date = new Date(2008, 11, 1); // 起始日期
ArrayList<Account> accounts = new ArrayList<Account>(0); // 创建账户数组,元素个数为0
System.out.println("(a)add account (d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit");
char cmd;
Scanner in = new Scanner(System.in);
do {
// 显示日期和总金额
date.show();
System.out.print("\tTotal: " + Account.getTotal() + "\tcommand> ");
char type;
int index, day;
double amount, credit, rate, fee;
String id, desc;
Account account;
cmd = (char) System.in.read();
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);
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++) {
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.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 < 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);
}
}
比较C++与Java:
在C++代码中有Array的模板类,可以动态添加对象。而Java不支持实现模板类,但是在Java的util的包中有已写好的ArrayList类可以通过查找参考它的方法直接使用。
重构:
在之前版本的基础上,由于Java提供了写好的ArrayList类,直接调用方法可以精简程序,可读性更强。
从以上版本的演变过程,可以发现充分利用了面向对象的语言的功能特点,即封装、继承和多态,增强了程序的安全性、可读性和可扩展性。
以银行个人账户信息系统为例:类的封装,数据成员与方法属性的设置,使得可以数据隐藏或者不能被修改,大大提高了程序的安全性;利用抽象类Account与两个实现类CreditAccount和SavingsAccount,使得程序实际功能分工更明确,也更全面,同时,还可动态添加更多的实现类,体现了较好的可扩展性。