- 个人银行管理系统版本0.4(对应第7章记录)
这个版本相较于以前的版本,在语法方面没什么明显改变,本版本加入抽象类,来方便实例化两个有些相似的子类,大体方法是和c++一样的,只是一些关键字不太一样。
package PersonalBank4;
public class Date {
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;
public 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.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 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());
}
public int distance(final Date date) {
return totalDays - date.totalDays;
}
}
package PersonalBank4;
public class Accumulator {
private Date lastDate;
private double value;
private double sum;
public Accumulator(final Date date, double value)
{
lastDate=date;
this.value=value;
sum=0;
}
public double getSum(final Date date){
return sum + value * date.distance(lastDate);
}
public void change(final Date date, double value) {
sum = getSum(date);
lastDate = date;
this.value = value;
}
public void reset(final Date date, double value) {
lastDate = date;
this.value = value;
sum = 0;
}
}
package PersonalBank4;
public class Account {
private String id;
private double balance;
private static double total = 0;
protected Account(final Date date, final String id)
{
this.id=id;
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 void error(final String msg)
{
System.out.println("Error(#" + id + "):" + msg);
}
public final String getId() { return id; }
public double getBalance() { return balance; }
public static double getTotal() { return total; }
public void show()
{
System.out.print(id + "\tBalance: " + balance);
}
}
package PersonalBank4;
public 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;
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)
{
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());
}
}
package PersonalBank4;
public 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;
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.println( "\tAvailable credit:" + getAvailableCredit());
}
}
package PersonalBank4;
public class PersonalBank {
public static void main(String[] args) {
Date date = new Date(2008, 11, 1);
SavingsAccount sa2 = new SavingsAccount(date, "02342342", 0.015);
CreditAccount ca = new CreditAccount(date, "BBBBB", 10000, 0.0005, 50);
ca.withdraw(new Date(2008, 11, 15), 2000, "buy a cell");
sa2.deposit(new Date(2008, 11, 25), 10000, "sell stock 0323");
sa2.show(); System.out.println();
ca.show();
System.out.println();
System.out.println( "Total: " + Account.getTotal());
}
}
- 个人银行管理系统版本0.5(对应第8章记录)
1)在改代码过程中对于show函数是否定为虚函数有很大麻烦,因为两种语言格式不一样,最后取消了纯虚函数的设置,不过也加深了对java的理解。
2)本版本实现类2个很常见的要求:批量操作和统一接口,因为对象类型不一样,还要求统一实现,就用到虚函数,而函数接口设置为一样,在函数体额外加判断条件,来达到接口统一,更利于程序的封装性,安全性。
3)java里面的输入输出很有趣,也是调用类库,比如java.text.DecimalFormat可以控制输出小数点,java.io.IOException和java.util.*则是输入的专用库,每种类型的输入还不一样,真的很方便啊!
Date类和Accumulator类和之前一样,CreditAccount只有show函数不太一样。
package PersonalBank5;
import java.text.DecimalFormat;
abstract public class Account {
private String id;
private double balance;
private static double total = 0;
protected Account(final Date date, final String id)
{
this.id=id;
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" + df.format(balance) + "\t" + desc );
}
protected void error(final String msg)
{
System.out.println("Error(#" + id + "):" + msg);
}
static DecimalFormat df = new DecimalFormat("0.0");
public final String getId() { return id; }
public double getBalance() { return balance; }
public static double getTotal() { return total; }
abstract void deposit(final Date date, double amount,final String desc);
abstract void withdraw(final Date date, double amount,final String desc);
abstract void settle(final Date date);
public void show(){
System.out.print(id + "\tBalance: " + df.format(balance));
}
}
package PersonalBank5;
public 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;
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.distance(new Date(date.getYear() - 1, 1, 1));
if (interest != 0)
record(date, interest, "interest");
acc.reset(date, getBalance());
}
}
}
CreditAccount:
public void show()
{
super.show();
System.out.print( "\t\tAvailable credit:" + getAvailableCredit());
}
package PersonalBank5;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;
public class PersonalBank {
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[] = { sa1, sa2, ca };
final int n = accounts.length;
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);
int index, day;
double amount;
String desc;
DecimalFormat df = new DecimalFormat("0.0");
do {
date.show();
System.out.print("\tTotal: " + df.format(Account.getTotal()) + "\tcommand> ");
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();
if(i<(n-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 < n; i++) {
accounts[i].settle(date);
if(i<(n-1)) {System.in.read();}
}
break;
}
} while (cmd != 'e');
in.close();
}
}
- 个人银行管理系统版本0.6(对应第9章记录)
1)先说些两种程序的不同点。C++里面有动态数组模板Array,可以动态添加对象,java里面有ArrayList,对于这个一开始真的很烦,要去不停的查询,因为他是类库里的东西。各种方法都有。只是我不会用罢了,所以这次的改编基本都在查资料。不过ArrayList确实很方便使用。
只有主函数和上一版本不一样,其他的类一模一样
package PersonalBank6;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;
public class PersonalBank {
public static void main(String[] args) throws IOException {
Date date = new Date(2008, 11, 1);
ArrayList<Account> accounts = new ArrayList<Account>(0);
DecimalFormat df = new DecimalFormat("0.0");
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: " + df.format(Account.getTotal()) + "\tcommand> ");
char type;
int index, day;
double amount,credit,rate,fee;
String id,desc;
Account account;
cmd = (char)System.in.read();
if(cmd != 'a' && cmd != 'd' && cmd != 'w' && cmd != 's' && cmd != 'c' && cmd != 'n' && cmd != 'e')break;
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.println("You cannot specify a previous day");
else if (day > date.getMaxDay())
System.out.println("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);
}
}