个人银行账户管理系统(c++改写java)第6章:增添字符串、对象数组
public class Account { private String id; //帐号 private double balance; //余额 private double rate; //存款的年利率 private date lastDate; //上次变更余额的时期 private double accumulation; //余额按日累加之和 private static double total = 0; //所有账户的总金额 //记录一笔帐,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); } //报告错误信息 private void error(String msg) { System.out.println("Error(#" + id + "): " + msg); } //获得到指定日期为止的存款金额按日累积值 private double accumulate(date date) { return accumulation + balance * date.distance(lastDate); } //构造函数 public Account(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( "\t#" + id + " created"); } 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 class date { //存储平年中某个月1日之前有多少天,为便于getMaxDay函数的实现,该数组多出一项 final int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; private int year; //年 private int month; //月 private int day; //日 private int totalDays; //该日期是从公元元年1月1日开始的第几天 boolean isLeapYear() { //判断当年是否为闰年 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; } public date(int year, int month, int day) //用年、月、日构造日期 { this.day = day; this.month = month; this.year = year; if (day <= 0 || day > getMaxDay()) { System.out.println("Invalid date: "); show(); 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 void show() //输出当前日期 { System.out.print(getYear() + "-" + getMonth() + "-" + getDay()); } //计算两个日期之间差多少天 int distance(date date) { return totalDays - date.totalDays; } }
public class main { public static void main(String[] args) { date date = new date(2008, 11, 1); //起始日期 //建立几个账户 Account accounts[] = { new Account(date, "S3755217", 0.015), new Account(date, "02342342", 0.015) }; 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), 20000, "buy a laptop"); //结算所有账户并输出各个账户信息 for (int i = 0; i < n; i++) { accounts[i].settle(new date(2009, 1, 1)); accounts[i].show(); } System.out.println("Total: " + Account.getTotal()); } }
1.Java里的字符串类型是一个类String;
2.Java数组必须先初始化才可以使用,初始化的时候会分配内存;动态初始化和静态初始化不能同时存在。

浙公网安备 33010602011771号