c++代码:
//4_9.cpp
#include
#include
using namespace std;
class SavingsAccount { //储蓄账户类
private:
int id; //账号
double balance; //余额
double rate; //存款的年利率
int lastDate; //上次变更余额的时期
double accumulation; //余额按日累加之和
//记录一笔帐,date为日期,amount为金额,desc为说明
void record(int date, double amount);
//获得到指定日期为止的存款金额按日累积值
double accumulate(int date) const {
return accumulation + balance * (date - lastDate);
}
public:
//构造函数
SavingsAccount(int date, int id, double rate);
int getId() { return id; }
double getBalance() { return balance; }
double getRate() { return rate; }
//存入现金
void deposit(int date, double amount);
//取出现金
void withdraw(int date, double amount);
//结算利息,每年1月1日调用一次该函数
void settle(int date);
//显示账户信息
void show();
};
//SavingsAccount类相关成员函数的实现
SavingsAccount::SavingsAccount(int date, int id, double rate)
: id(id), balance(0), rate(rate), lastDate(date), accumulation(0) {
cout << date << "\t#" << id << " is created" << endl;
}
void SavingsAccount::record(int date, double amount) {
accumulation = accumulate(date);
lastDate = date;
amount = floor(amount * 100 + 0.5) / 100; //保留小数点后两位
balance += amount;
cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl;
}
void SavingsAccount::deposit(int date, double amount) {
record(date, amount);
}
void SavingsAccount::withdraw(int date, double amount) {
if (amount > getBalance())
cout << "Error: not enough money" << endl;
else
record(date, -amount);
}
void SavingsAccount::settle(int date) {
double interest = accumulate(date) * rate / 365; //计算年息
if (interest != 0)
record(date, interest);
accumulation = 0;
}
void SavingsAccount::show() {
cout << "#" << id << "\tBalance: " << balance;
}
int main() {
//建立几个账户
SavingsAccount sa0(1, 21325302, 0.015);
SavingsAccount sa1(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(); cout << endl;
sa1.show(); cout << endl;
return 0;
}
转Java:
class SavingsAccount{
private int id,lastDate;
private double balance,rate,accumulation;
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) {
id=_id;
lastDate=_date;
balance=0;
rate=_rate;
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);
}
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.print("#"+id+"\tBalance: "+balance);
}
}
public class Main4_9{
public static void main(String MAINs[]) {
SavingsAccount sa0=new SavingsAccount(1,21325302,0.015);
SavingsAccount sa1=new SavingsAccount(1,58320201,0.015);
sa0.deposit(5,5000);
sa1.deposit(25, 10000);
sa0.deposit(45,5500);
sa1.withdraw(60,4000);
sa0.settle(90);
sa1.settle(90);
sa0.show();
System.out.println();
sa1.show();
System.out.println();
}
}
Java代码:
//Data.java
public class Date {
private int year,month,day,totalDays;
static private final int[] DAYS_BEFORE_MONTH= { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
public Date(int year,int month, int 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 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 boolean isLeapYear(){ //判断当年是否为闰年
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
public void show() {
System.out.print(""+getYear()+"-"+getMonth()+"-"+getDay());
}; //输出当前日期
//计算两个日期之间差多少天
public int sub (final Date date) {
return totalDays - date.totalDays;
}
};
//account.java
public abstract class account{
private String id;
private double balance;
static private double total=0;
public account() {}
protected account(final Date date,final String id){
this.id=id;
this.balance=0;
date.show();
System.out.println("\t#"+id+" created");
}
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 final void error(final String msg){
System.out.println("Error(#"+id+"): "+msg);
}
public String getId(){ return id; }
public double getBalance() { return balance; }
static public double getTotal() { return total; }
//存入现金,date为日期,amount为金额,desc为款项说明
abstract public void deposit(final Date date, double amount, final String desc);
//取出现金,date为日期,amount为金额,desc为款项说明
abstract public void withdraw(final Date date, double amount, final String desc);
//结算(计算利息、年费等),每月结算一次,date为结算日期
abstract public void settle(final Date date) ;
//显示账户信息
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);
this.rate=rate;
this.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());
}
};
public void settle(final Date date) {
if (date.getMonth() == 1) { //每年的一月计算一次利息
double interest = acc.getSum(date) * rate
/ (date.sub(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; //信用额度
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;
this.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());
}
}
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.print("\t+Available credit:"+getAvailableCredit());
};
};
//accumulator.java
public class accumulator { //将某个数值按日累加
private Date lastDate; //上次变更数值的时期
private double value; //数值的当前值
private double sum; //数值按日累加之和
//构造函数,date为开始累加的日期,value为初始值
public accumulator(final Date date, double value){
this.lastDate=date;
this.value=value;
this.sum=0;
}
//获得到日期date的累加结果
public double getSum(final Date date) {
return sum + value * (date.sub(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;
}
};
//Main.java
import java.util.*;
public class Main {
static public void main(String MAINs[]) {
Scanner cin=new Scanner(System.in);
Date date =new Date(2008, 11, 1); //起始日期
ArrayList accounts=new ArrayList(0); //创建账户数组,元素个数为0
System.out.println("(a)add account (d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit");
//cout << "(a)add account (d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl;
String cmd;
do {
//显示日期和总金额
date.show();
//cout << "\tTotal: " << Account::getTotal() << "\tcommand> ";
System.out.print("\tTotal: "+account.getTotal()+"\tcommand> ");
String type;
int index, day;
double amount, credit, rate, fee;
String id, desc;
account _account;
cmd=cin.next();
switch (cmd) {
case "a": //增加账户
type=cin.next();
id=cin.next();
//cin >> type >> id;
if (type == "s") {
rate=cin.nextDouble();
//cin >> rate;
_account = new SavingsAccount(date, id, rate);
} else {
credit=cin.nextDouble();
rate=cin.nextDouble();
fee=cin.nextDouble();
//cin >> credit >> rate >> fee;
_account = new CreditAccount(date, id, credit, rate, fee);
}
accounts.add(_account);
//accounts.resize(accounts.Size() + 1);
//accounts[accounts.getSize() - 1] = account;
break;
case "d": //存入现金
index=cin.nextInt();
amount=cin.nextDouble();
desc=cin.nextLine();
accounts.get(index).deposit(date,amount,desc);
//cin >> index >> amount;
//getline(cin, desc);
//accounts[index]->deposit(date, amount, desc);
break;
case "w": //取出现金
index = cin.nextInt();
amount = cin.nextDouble();
desc = cin.nextLine();
accounts.get(index).withdraw(date, amount, desc);
// cin >> index >> amount;
// getline(cin, desc);
// accounts[index]->withdraw(date, amount, desc);
break;
case "s": //查询各账户信息
for (int i = 0; i < accounts.size(); i++) {
System.out.printf("[%d]", i);
accounts.get(i).show();
System.out.println("");
}
// for (int i = 0; i < accounts.getSize(); i++) {
// cout << "[" << i << "] ";
// accounts[i]->show();
// cout << endl;
// }
break;
case "c": //改变日期
day = cin.nextInt();
Date localDay = new Date(date.getYear(), date.getMonth(), day);
if (daydate.getMaxDay()){
System.out.print("Invalid day");
}else {
date = localDay;
}
// cin >> day;
// if (day < date.getDay())
// cout << "You cannot specify a previous day";
// else if (day > date.getMaxDay())
// cout << "Invalid day";
// else
// date = Date(date.getYear(), date.getMonth(), day);
break;
case "n": //进入下个月
if (date.getMonth() == 12) {
date = new Date(date.getYear(), 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 (date.getMonth() == 12)
// date = Date(date.getYear() + 1, 1, 1);
// else
// date = Date(date.getYear(), date.getMonth() + 1, 1);
// for (int i = 0; i < accounts.getSize(); i++)
// accounts[i]->settle(date);
break;
}
} while (!cmd.equals("e"));
cin.close();
}
};