个人银行管理系统5(C改Java)

  • C语言版本
//date.h
#ifndef __DATE_H__
#define __DATE_H__

class Date {	//日期类
private:
	int year;		//年
	int month;		//月
	int day;		//日
	int totalDays;	//该日期是从公元元年1月1日开始的第几天

public:
	Date(int year, int month, int day);	//用年、月、日构造日期
	int getYear() const { return year; }
	int getMonth() const { return month; }
	int getDay() const { return day; }
	int getMaxDay() const;		//获得当月有多少天
	bool isLeapYear() const {	//判断当年是否为闰年
		return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
	}
	void show() const;			//输出当前日期
	//计算两个日期之间差多少天	
	int operator - (const Date& date) const {
		return totalDays - date.totalDays;
	}
};

#endif //__DATE_H__

//date.cpp
#include "date.h"
#include <iostream>
#include <cstdlib>
using namespace std;

namespace {	//namespace使下面的定义只在当前文件中有效
	//存储平年中某个月1日之前有多少天,为便于getMaxDay函数的实现,该数组多出一项
	const int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
}

Date::Date(int year, int month, int day) : year(year), month(month), day(day) {
	if (day <= 0 || day > getMaxDay()) {
		cout << "Invalid date: ";
		show();
		cout << endl;
		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++;
}

int Date::getMaxDay() const {
	if (isLeapYear() && month == 2)
		return 29;
	else
		return DAYS_BEFORE_MONTH[month]- DAYS_BEFORE_MONTH[month - 1];
}

void Date::show() const {
	cout << getYear() << "-" << getMonth() << "-" << getDay();
}

//accumulator.h
#ifndef __ACCUMULATOR_H__
#define __ACCUMULATOR_H__
#include "date.h"

class Accumulator {	//将某个数值按日累加
private:
	Date lastDate;	//上次变更数值的时期
	double value;	//数值的当前值
	double sum;		//数值按日累加之和
public:
	//构造函数,date为开始累加的日期,value为初始值
	Accumulator(const Date &date, double value)
		: lastDate(date), value(value), sum(0) { }

	//获得到日期date的累加结果
	double getSum(const Date &date) const {
		return sum + value * (date - lastDate);
	}

	//在date将数值变更为value
	void change(const Date &date, double value) {
		sum = getSum(date);
		lastDate = date;
		this->value = value;
	}

	//初始化,将日期变为date,数值变为value,累加器清零
	void reset(const Date &date, double value) {
		lastDate = date;
		this->value = value;
		sum = 0;
	}
};

#endif //__ACCUMULATOR_H__
//account.h
#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__
#include "date.h"
#include "accumulator.h"
#include <string>

class Account { //账户类
private:
	std::string id;	//帐号
	double balance;	//余额
	static double total; //所有账户的总金额
protected:
	//供派生类调用的构造函数,id为账户
	Account(const Date &date, const std::string &id);
	//记录一笔帐,date为日期,amount为金额,desc为说明
	void record(const Date &date, double amount, const std::string &desc);
	//报告错误信息
	void error(const std::string &msg) const;
public:
	const std::string &getId() const { return id; }
	double getBalance() const { return balance; }
	static double getTotal() { return total; }
	//存入现金,date为日期,amount为金额,desc为款项说明
	virtual void deposit(const Date &date, double amount, const std::string &desc) = 0;
	//取出现金,date为日期,amount为金额,desc为款项说明
	virtual void withdraw(const Date &date, double amount, const std::string &desc) = 0;
	//结算(计算利息、年费等),每月结算一次,date为结算日期
	virtual void settle(const Date &date) = 0;
	//显示账户信息
	virtual void show() const;
};

class SavingsAccount : public Account { //储蓄账户类
private:
	Accumulator acc;	//辅助计算利息的累加器
	double rate;		//存款的年利率
public:
	//构造函数
	SavingsAccount(const Date &date, const std::string &id, double rate);
	double getRate() const { return rate; }
	virtual void deposit(const Date &date, double amount, const std::string &desc);
	virtual void withdraw(const Date &date, double amount, const std::string &desc);
	virtual void settle(const Date &date);
};

class CreditAccount : public Account { //信用账户类
private:
	Accumulator acc;	//辅助计算利息的累加器
	double credit;		//信用额度
	double rate;		//欠款的日利率
	double fee;			//信用卡年费

	double getDebt() const {	//获得欠款额
		double balance = getBalance();
		return (balance < 0 ? balance : 0);
	}
public:
	//构造函数
	CreditAccount(const Date &date, const std::string &id, double credit, double rate, double fee);
	double getCredit() const { return credit; }
	double getRate() const { return rate; }
	double getFee() const { return fee; }
	double getAvailableCredit() const {	//获得可用信用
		if (getBalance() < 0) 
			return credit + getBalance();
		else
			return credit;
	}
	virtual void deposit(const Date &date, double amount, const std::string &desc);
	virtual void withdraw(const Date &date, double amount, const std::string &desc);
	virtual void settle(const Date &date);
	virtual void show() const;
};

#endif //__ACCOUNT_H__
//account.cpp
#include "account.h"
#include <cmath>
#include <iostream>
using namespace std;

double Account::total = 0;

//Account类的实现
Account::Account(const Date &date, const string &id)
	: id(id), balance(0) {
	date.show();
	cout << "\t#" << id << " created" << endl;
}

void Account::record(const Date &date, double amount, const string &desc) {
	amount = floor(amount * 100 + 0.5) / 100;	//保留小数点后两位
	balance += amount;
	total += amount;
	date.show();
	cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl;
}

void Account::show() const {
	cout << id << "\tBalance: " << balance;
}

void Account::error(const string &msg) const {
	cout << "Error(#" << id << "): " << msg << endl;
}

//SavingsAccount类相关成员函数的实现
SavingsAccount::SavingsAccount(const Date &date, const string &id, double rate)
	: Account(date, id), rate(rate), acc(date, 0) { }

void SavingsAccount::deposit(const Date &date, double amount, const string &desc) {
	record(date, amount, desc);
	acc.change(date, getBalance());
}

void SavingsAccount::withdraw(const Date &date, double amount, const string &desc) {
	if (amount > getBalance()) {
		error("not enough money");
	} else {
		record(date, -amount, desc);
		acc.change(date, getBalance());
	}
}

void SavingsAccount::settle(const Date &date) {
	if (date.getMonth() == 1) {	//每年的一月计算一次利息
		double interest = acc.getSum(date) * rate
			/ (date - Date(date.getYear() - 1, 1, 1));
		if (interest != 0)
			record(date, interest, "interest");
		acc.reset(date, getBalance());
	}
}

//CreditAccount类相关成员函数的实现
CreditAccount::CreditAccount(const Date& date, const string& id, double credit, double rate, double fee)
	: Account(date, id), credit(credit), rate(rate), fee(fee), acc(date, 0) { }

void CreditAccount::deposit(const Date &date, double amount, const string &desc) {
	record(date, amount, desc);
	acc.change(date, getDebt());
}

void CreditAccount::withdraw(const Date &date, double amount, const string &desc) {
	if (amount - getBalance() > credit) {
		error("not enough credit");
	} else {
		record(date, -amount, desc);
		acc.change(date, getDebt());
	}
}

void CreditAccount::settle(const 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());
}

void CreditAccount::show() const {
	Account::show();
	cout << "\tAvailable credit:" << getAvailableCredit();
}

  • Java版本
package Bank8_8;

abstract class Account {
	private String id;//账号
	private double balance;//余额
	private static double total = 0;//所有账户的总金额
	//供派生类调用的构造函数,id为账户
	protected Account(final Date date,final String id) {
		this.id = id;
		this.balance = 0;
		date.show();
		System.out.println(id + " created");
	}
	//记录一笔账,date为日期,amount为金额,desc为说明
	protected void record(final Date date,double amount,final String desc) {
		balance += amount;
		total += amount;
		date.show();
		System.out.println(id + "  " + amount + "  " + balance + "  " + desc);
	}
	//报告错误信息
	protected void error(final String msg) {
		System.out.println("Error(#" + id + "):" + msg);
	}
	public String GetId() {
		return id;
	}
	public double GetBalance() {
		return balance;
	}
	public static double GetTotal() {
		return total;
	}
	public void show() {
		System.out.println(id + " Balance:" + balance);
	}
	//取出现金,date为日期,amount为金额,desc为款项说明
	abstract void withdraw(final Date date,double amount,final String desc);
	//存入现金,date为日期,amount为金额,desc为款项说明
	abstract void deposit(final Date date,double amount,final String desc);
	//结算(计算利息、年费等),每月结算一次,date为结算日期
	abstract void settle(final Date date);
}
class SavingsAccount extends Account{//储蓄账户类
	private Accumulator acc;//辅助计算利息的累加器
	private double rate;//存款的年利率
	//构造函数
	public SavingsAccount(Date date,String id,double rate) {
		super(date,id);
		this.rate = rate;
		acc = new Accumulator(date,0);
	}
	private double GatRate() {
		return rate;
	}
	//存入现金
	void deposit(final Date date,double amount,final String desc) {
		record(date,amount,desc);
		acc.change(date,GetBalance());
	}
	//取出现金
	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());
		}
	}
	//结算利息,每年1月1日调用一次该函数
	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());
	}
}
//信用账户类
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 GteRate() {
		return rate;
	}
	public double GetFee() {
		return fee;
	}
	//获得可用信用
	public double GetAvailableCredit() {
		if(GetBalance() < 0) {
			return credit + GetBalance();
		}
		else {
			return credit;
		}
	}
	//存入现金
	void deposit(final Date date,double amount,final String desc) {
		record(date,amount,desc);
		acc.change(date,GetDebt());
	}
	//取出现金
	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());
		}
	}
	//结算利息和年费,每月1日调用一次该函数
	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("Available credit:" + GetAvailableCredit());
	}
}
package Bank8_8;

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.distance(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;
	}
}
package Bank8_8;

public class Date {
	private int year;//年
	private int month;//月
	private int day;//日
	private int totalDays;//该日期是从公元元年1月1日开始的第几天
	private int[] a = new int[]{0,31,59,90,120,151,181,212,243,273,304,334,365};
	//存储平年中某个月1日之前有多少天
	//构造函数
	public Date(int year,int month,int day) {
		this.year = year;
		this.month = month;
		this.day = day;
		if(day <= 0 || day > this.getMaxDay()) {
			System.out.println("Invalid date:");
			this.show();
			System.exit(1);
		}
		int years = year - 1;
		totalDays = years * 365 + years/4 - years/100 + years/400 + a[month-1]+day;
		if(this.LeapYear() == true && month >2) {
			totalDays++;
		}
	}
	//判断是否是闰年
	public boolean LeapYear() {
		if((year%4 == 0 && year%100 != 0)||(year%400 ==0)) {
			return true;
		}
		else {
			return false;
		}
	}
	//求某个月的天数
	public int getMaxDay() {
		if(this.LeapYear() == true && month == 2) {
			return 29;
		}
		else {
			return a[month]-a[month-1];
		}
	}
	//输出
	public void show() {
		System.out.println(year + "-" + month + "-" +day);
	}
	//计算两个日期之间相差的天数
	public int distance(Date date) {
		return totalDays - date.totalDays;
	}
	public int getYear() {
		return year;
	}
	public int getMonth() {
		return month;
	}
	public int getDay() {
		return day;
	}
}
package Bank8_8;

import java.lang.reflect.Array;
import java.io.IOException;
import java.util.*;

public class Run {
	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 = new Account[]{ sa1, sa2, ca };
		final int n = Array.getLength(accounts);//账户总数
		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.println("\tTotal: " + Account.GetTotal() + "\tcommand>");
			int index, day;
			double amount;
			String desc;
			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.println("[" + i + "]");
					accounts[i].show();
				}
				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 < n; i++)
					accounts[i].settle(date);
				break;
			}
		} while (cmd != 'e');
	}
}

  • 总结反思
    这一版本和之前四个最大的区别在于纯虚函数(抽象方法)的使用。

C++中的虚函数的作用主要是实现了多态的机制,虚函数必须是基类的非静态成员函数,其访问权限可以是protected或public,其关键字为virtual。
什么函数不能声明为虚函数:
1.构造函数不能声明为虚函数
2.静态成员函数不能声明为虚函数
3.友元函数不能声明为虚函数
4.赋值运算符重载函数最好不要声明为虚函数
5.析构函数可以声明为虚函数
6.普通函数不声明为虚函数
7.内联函数不能声明为虚函数
8.存在多态时析构函数声明为虚函数

在Java中,没有纯虚函数的概念,与之对应的是用“abstract”修饰的抽象类、抽象方法。
使用抽象类时需注意:
1.抽象类中可以有零个或多个抽象方法,也可以包含非抽象的方法。
2.抽象类不能创建对象,创建对象的工作由抽象类派生的具体子类来实现,但可以有声明。
3.抽象类和具体类的关系也就是一般类和特殊类的关系,是一种被继承和继承的关系。
4.对于抽象方法来说,在抽象类中只指定其方法名及参数类型,而不必书写其实现代码。
5.在抽象类当中,非抽象方法——已实现的方法也可以调用抽象方法,因为在具体子类中抽象方法必定被实现,从而在具体子类中又是具体方法调用具体方法。
6.abstract不能与final并列修饰同一个类。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值