第六章
1.Java里没有像c++一样的&符号引用,创建对象时一律使用new。
2.Java里是不允许全局变量的,因为这会破坏封装性。
3.Java里的异常退出exit(1)需要调用System.exit(1)。
4.Java里的字符串类型是一个类String。
5.print不输出换行,println才换行。
package ob3;
public class Date {
//日期类
private int year; //年
private int month; //月
private int day; //日
private int totalDays; //该日期是从公元元年1月1日开始的第几天
//存储平年中某个月1日之前有多少天,为便于getMaxDay函数的实现,该数组多出一项
public final int[] DAYS_BEFORE_MONTH =new int[]{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
public void show(){
System.out.println( getYear()+"-"+getMonth()+"-"+getDay());
} //输出当前日期
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 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 int distance(Date date) {
return totalDays - date.totalDays;
}
}
package ob3;
public class SavingsAccount {
//储蓄账户类
private String id; //账号
private double balance;//余额
private double rate;//存款的年利率
Date lastDate; //上次变更余额的时期
private double accumulation; //余额按日累加之和
private static double total=0; //所有账户的总金额
//获得到指定日期为止的存款金额按日累积值
private double accumulate(Date date){
return accumulation + balance * date.distance(lastDate);
}
//记录一笔帐,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+ "\n");
}
//构造函数
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(date+"\t#"+id+"is created\n");
}
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, "interest");
accumulation = 0;
}
//显示账户信息
public void show(){
System.out.println(id+"\tBalance: "+balance);
}
public void error(String msg){
System.out.println("Error(#"+id+"): "+msg);
}
}
package ob3;
public class Run {
public static void main(String[] args){
//建立几个账户
Date date=new Date(2008,11,1);
SavingsAccount[] accounts;
accounts=new SavingsAccount[2];
int n = accounts.length; //账户总数
accounts[0]=new SavingsAccount(date, "S3755217", 0.015);
accounts[1]=new SavingsAccount(date, "02342342", 0.015);
//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: " + SavingsAccount.getTotal());
}
}