转换过程中,其中C++与Java的不同,从第一个版本到最后一个版本使用面向对象特性进行代码重构的体会。
第四章~~~第六章
第四章
这个版本是最基本的类,改写起来比较容易,只需要改几个简单的语法。
accumulate函数在原C++文件里是double accumulate(int date) const,Java要给它加上final修饰。
代码:
package 第四章;
public class SavingsAccount4 {
private int id;
private double balance;
private double rate;
private int lastDate;
private double 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 SavingsAccount4 (int date, int id, double rate){
this.lastDate=date;
this.id=id;
this.rate=rate;
balance=0;
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.println( "#" + id + "\tBalance: " + balance);
}
public static void main(String args[]){
SavingsAccount4 sa0=new SavingsAccount4(1, 21325302, 0.015);
SavingsAccount4 sa1=new SavingsAccount4(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();
}
}
第五章
这个版本是增添静态属性与方法,关键在于对静态属性与方法的理解。
代码:
package 第五章;
public class SavingsAccount5 {
private static double total=0;
private int id;
private double balance;
private double rate;
private int lastDate;
private double 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 SavingsAccount5 (int date, int id, double rate){
this.lastDate=date;
this.id=id;
this.rate=rate;
balance=0;
accumulation=0;
System.out.println(date+"\t#"+id+" is created");
}
public int getId() {
return id; }
public static double getTotal(){
return total;}
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[]){
SavingsAccount5 sa0=new SavingsAccount5(1, 21325302, 0.015);
SavingsAccount5 sa1=new SavingsAccount5(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: " +getTotal() );
}
}
第六章
这个版本是增添字符串、对象数组。
代码:
package 第六章;
public class Date {
private int year;
private int month;
private int day;
private int totalDays;
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("");
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 distance(final Date date) {
return totalDays - date.totalDays;
}
private final int DAYS_BEFORE_MONTH[] = {
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
}
package 第六章;
public class SavingsAccount6 {
private String id;
private double balance;
private double rate;
private Date lastDate;
private double accumulation;
private static double total=0;
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 final double accumulate(final Date date) {
return accumulation + balance * date .distance(lastDate);
}
private final void error(final String msg){
System.out.println("Error(#"+id+"):"+msg);
}
public SavingsAccount6 (final Date date,final String id,double rate){
this.lastDate=date;
this.id=id;
this.rate=rate;
balance=0;
accumulation=0;
date.show();
System.out.println("\t#"+id+" created");
}
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);
}
public void settle(final Date date){
Date date0=new Date(date.getYear()-1,1,1);
double interest = accumulate(date) * rate //计算年息
/ date.distance(date0);
if (interest != 0)
record(date, interest, "interest");
accumulation = 0;
}
public final void show(){
System.out.println( "#" + id + "\tBalance: " + balance);
}
public static void main(String args[]){
Date date=new Date(2008, 11, 1); //起始日期
//建立几个账户
SavingsAccount6[] accounts = {
new SavingsAccount6(date, "S3755217", 0.015),
new SavingsAccount6(date, "02342342", 0.015)
};
final int n=accounts.length;
//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");
//结算所有账户并输出各个账户信息
System.out.println();
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: " + getTotal());
}
}