银行管理系统C++代码转Java代码6_25

C++代码源自《C++语言程序设计(第4版)》(清华大学计算机系列教材) 郑莉, 董渊, 何江舟 清华大学出版社

6_25

C++源码

//6_25.cpp
#include "account.cpp"
#include <iostream>
using namespace std;

int main() {
	Date date(2008, 11, 1);	//起始日期
	//建立几个账户
	SavingsAccount accounts[] = {
		SavingsAccount(date, "S3755217", 0.015),
		SavingsAccount(date, "02342342", 0.015)
	};
	const int n = sizeof(accounts) / sizeof(SavingsAccount); //账户总数
	//11月份的几笔账目
	accounts[0].deposit(Date(2008, 11, 5), 5000, "salary");
	accounts[1].deposit(Date(2008, 11, 25), 10000, "sell stock 0323");
	//12月份的几笔账目
	accounts[0].deposit(Date(2008, 12, 5), 5500, "salary");
	accounts[1].withdraw(Date(2008, 12, 20), 4000, "buy a laptop");

	//结算所有账户并输出各个账户信息
	cout << endl;
	for (int i = 0; i < n; i++) {
		accounts[i].settle(Date(2009, 1, 1));
		accounts[i].show();
		cout << endl;
	}
	cout << "Total: " << SavingsAccount::getTotal() << endl;
	return 0;
}

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

double SavingsAccount::total = 0;

//SavingsAccount类相关成员函数的实现
SavingsAccount::SavingsAccount(const Date &date, const string &id, double rate)
	: id(id), balance(0), rate(rate), lastDate(date), accumulation(0) {
	date.show();
	cout << "\t#" << id << " created" << endl;
}

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

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

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

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

void SavingsAccount::settle(const Date &date) {
	double interest = accumulate(date) * rate	//计算年息
		/ date.distance(Date(date.getYear() - 1, 1, 1));
	if (interest != 0)
		record(date, interest, "interest");
	accumulation = 0;
}

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

//account.h
#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__
#include "date.cpp"
#include <string>

class SavingsAccount { //储蓄账户类
private:
	std::string id;		//帐号
	double balance;		//余额
	double rate;		//存款的年利率
	Date lastDate;		//上次变更余额的时期
	double accumulation;	//余额按日累加之和
	static double total;	//所有账户的总金额

	//记录一笔帐,date为日期,amount为金额,desc为说明
	void record(const Date &date, double amount, const std::string &desc);
	//报告错误信息
	void error(const std::string &msg) const;
	//获得到指定日期为止的存款金额按日累积值
	double accumulate(const Date& date) const {
		return accumulation + balance * date.distance(lastDate);
	}
public:
	//构造函数
	SavingsAccount(const Date &date, const std::string &id, double rate);
	const std::string &getId() const { return id; }
	double getBalance() const { return balance; }
	double getRate() const { return rate; }
	static double getTotal() { return total; }

	//存入现金
	void deposit(const Date &date, double amount, const std::string &desc);
	//取出现金
	void withdraw(const Date &date, double amount, const std::string &desc);
	//结算利息,每年1月1日调用一次该函数
	void settle(const Date &date);
	//显示账户信息
	void show() const;
};

#endif //__ACCOUNT_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();
}

//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 distance(const Date& date) const {
		return totalDays - date.totalDays;
	}
};

#endif //__DATE_H__

C++运行结果

6_25C++运行结果

Java代码

//Test.java
public class Test {

	public static void main(String[] args) {
		Date date=new Date(2008, 11, 1);	//起始日期
		//建立几个账户
		SavingsAccount[] accounts = {
			new SavingsAccount(date, "S3755217", 0.015),
			new SavingsAccount(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: " + SavingsAccount.getTotal() +"\n");
	}

}

//SavingsAccount.java
public class SavingsAccount {
	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,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(Date date) {
		return accumulation + balance * (date.distance(lastDate));
	}
	private final void error(final String msg)  {
		System.out.println("Error(#" + id + "): " + msg +"\n");
	}
	// 构造函数
	SavingsAccount(final 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");
	}
	

	final String getId() { return id; }
	final double getBalance() {	return balance;}
	final double getRate() {return rate;}
	static double getTotal() {	return total;}

	// 存入现金
	void deposit(final Date date, double amount,final String desc) {
		record(date, amount,desc);
	}

	// 取出现金
	void withdraw(final Date date, double amount,final String desc) {
		if (amount > getBalance())
			error("not enough money\n");
		else
			record(date, -amount,desc);
	}

	// 结算利息,每年1月1日调用一次该函数
	void settle(final 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;
	}

	// 显示账户信息
	void show() {
		System.out.println(id + "\tBalance: " + balance);
	}

}

//Date.java
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; // 年
	int month; // 月
	int day; // 日
	int totalDays; // 该日期是从公元元年1月1日开始的第几天

	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++;
	}// 用年、月、日构造日期

	final int getYear() {
		return year;
	}

	final int getMonth() {
		return month;
	}

	final int getDay() {
		return day;
	}

	final int getMaxDay() {
			if (isLeapYear() && month == 2)
				return 29;
			else
				return DAYS_BEFORE_MONTH[month]- DAYS_BEFORE_MONTH[month - 1];
		}; // 获得当月有多少天

	final boolean isLeapYear() {	//判断当年是否为闰年
			return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
		}

	final void show() {
		System.out.print( getYear()+ "-" + getMonth()+"-" + getDay());//输出当前日期
	}
		//计算两个日期之间差多少天	

	final int distance(final Date date)  {
			return totalDays - date.totalDays;
		}
}

Java运行结果

6_25Java运行结果

posted @ 2019-10-09 20:55  MY知北  阅读(208)  评论(0编辑  收藏  举报