银行管理系统C++源码改Java,1.0 to 6.0(第4章到第9章)

1.0版本(第四章)

第四章就是基本的类。
改动的一些点:
(1)JAVA里面很多基本语法说明它不能先声明一个方法,再给出一个实现;私有/公有的变量/方法的private/public都要写出来;输入输出用System.out.println()的形式;在主函数中创建的是对象引用而不是对象,所以要用到new()。
(2)在构造函数中,当形参与域变量同名时,用this来指代域变量。而C++里用到了复制(拷贝)构造函数。
(3)JAVA里的floor函数因为没有了头文件cmath,要用已经定义好的类Math去调用,即Math.floor()。
C++源码:

#include <iostream>
#include <cmath>
using namespace std;

class SavingsAccount { //储蓄账户类
private:
	int id;				//账号
	double balance;		//余额
	double rate;		//存款的年利率
	int lastDate;		//上次变更余额的时期
	double accumulation;	//余额按日累加之和

	//记录一笔帐,date为日期,amount为金额,desc为说明
	void record(int date, double amount);
	//获得到指定日期为止的存款金额按日累积值
	double accumulate(int date) const {
		return accumulation + balance * (date - lastDate);
	}
public:
	//构造函数
	SavingsAccount(int date, int id, double rate);
	int getId() { return id; }
	double getBalance() { return balance; }
	double getRate() { return rate; }

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

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

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

void SavingsAccount::deposit(int date, double amount) {
	record(date, amount);
}

void SavingsAccount::withdraw(int date, double amount) {
	if (amount > getBalance())
		cout << "Error: not enough money" << endl;
	else
		record(date, -amount);
}

void SavingsAccount::settle(int date) {
	double interest = accumulate(date) * rate / 365;	//计算年息
	if (interest != 0)
		record(date, interest);
	accumulation = 0;
}

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

int main() {
	//建立几个账户
	SavingsAccount sa0(1, 21325302, 0.015);
	SavingsAccount sa1(1, 58320212, 0.015);

	//几笔账目
	sa0.deposit(5, 5000);
	sa1.deposit(25, 10000);
	sa0.deposit(45, 5500);
	sa1.withdraw(60, 4000);

	//开户后第90天到了银行的计息日,结算所有账户的年息
	sa0.settle(90);
	sa1.settle(90);

	//输出各个账户信息
	sa0.show();	cout << endl;
	sa1.show();	cout << endl;
	return 0;
}

改成Java代码:

package 银行管理系统4;
class SavingsAccount { //储蓄账户类
	private int id;				//账号
	private double balance;		//余额
	private double rate;		//存款的年利率
	private int lastDate;		//上次变更余额的时期
	private double accumulation;	//余额按日累加之和

	//记录一笔帐,date为日期,amount为金额,desc为说明
	private void record(int date, double amount) {
		accumulation = accumulate(date);
		lastDate = date;
		amount =Math.floor(amount * 100 + 0.5) / 100;	//保留小数点后两位
		balance += amount;
		System.out.println(date + "\t#" + id + "\t"+amount + "\t"+ balance);
	}
	//获得到指定日期为止的存款金额按日累积值
	private final double accumulate(int date)  {
		return accumulation + balance * (date - lastDate);
	}
	//构造函数
	public SavingsAccount(int date, int id, double rate) {
		this.id=id;
		balance=0;
		this.rate=rate;
		lastDate=date;
		accumulation=0;
		System.out.println(date + "\t#" + id + " is created");
	}
	public int getId() { return id; }
	public double getBalance() { return balance; }
	public double getRate() { return rate; }

	//存入现金
	public void deposit(int date, double amount) {
		record(date, amount);
	}
	//取出现金
	public void withdraw(int date, double amount) {
		if (amount > getBalance())
			System.out.println("Error: not enough money");
		else
			record(date, -amount);
	}
	//结算利息,每年1月1日调用一次该函数
	public void settle(int date) {
		double interest = accumulate(date) * rate / 365;	//计算年息
		if (interest != 0)
			record(date, interest);
		accumulation = 0;
	}
	//显示账户信息
	public void show() {
		System.out.println("#"+id + "\tBalance: " + balance);
	}
};
public class m4_7 {
	public static void main(String[] args) {
	//建立几个账户
		SavingsAccount sa0=new SavingsAccount(1, 21325302, 0.015);
		SavingsAccount sa1=new SavingsAccount(1, 58320212, 0.015);

		//几笔账目
		sa0.deposit(5, 5000);
		sa1.deposit(25, 10000);
		sa0.deposit(45, 5500);
		sa1.withdraw(60, 4000);

		//开户后第90天到了银行的计息日,结算所有账户的年息
		sa0.settle(90);
		sa1.settle(90);

		//输出各个账户信息
		sa0.show();	
		sa1.show();
}
}

2.0版本(第五章)

第五章增添了静态属性与方法。
改动的一些点:
(1)由于C++里面可以把一个程序分成类定义头文件(.h)和类实现文件(.cpp),还有主函数文件。在JAVA里做的改动是在同一个包里建立两个类(这里是SavingsAccount和m5_11),在m5_11中去调用SavingsAccount。
(2)JAVA中静态变量的初始化可以直接在定义时初始化,而且静态方法在调用时可以用任何一个对象的引用(sa0,sa1)或者这个类,而且没有::符号(域操作符)。
(3)JAVA中的final与C++中const并不等价,final用在方法里还表示最终方法,不能被继承。

java改动后的源码:

package 银行管理系统5;
class SavingsAccount { //储蓄账户类
	private int id;				//账号
	private double balance;		//余额
	private double rate;		//存款的年利率
	private int lastDate;		//上次变更余额的时期
	private double accumulation;	//余额按日累加之和
	public static double total=0;

	//记录一笔帐,date为日期,amount为金额,desc为说明
	private void record(int date, double amount) {
		accumulation = accumulate(date);
		lastDate = date;
		amount = Math.floor(amount * 100 + 0.5) / 100;	//保留小数点后两位
		balance += amount;
		total += amount;
		System.out.println(date + "\t#" + id + "\t"+amount + "\t"+ balance);
	}
	//获得到指定日期为止的存款金额按日累积值
	private final double accumulate(int date)  {
		return accumulation + balance * (date - lastDate);
	}
	//构造函数
	public SavingsAccount(int date, int id, double rate) {
		this.id=id;
		balance=0;
		this.rate=rate;
		lastDate=date;
		accumulation=0;
		System.out.println(date + "\t#" + id + " is created");
	}
	public int getId() { return id; }
	public double getBalance() { return balance; }
	public double getRate() { return rate; }

	//存入现金
	public void deposit(int date, double amount) {
		record(date, amount);
	}
	//取出现金
	public void withdraw(int date, double amount) {
		if (amount > getBalance())
			System.out.println("Error: not enough money");
		else
			record(date, -amount);
	}
	//结算利息,每年1月1日调用一次该函数
	public void settle(int date) {
		double interest = accumulate(date) * rate / 365;	//计算年息
		if (interest != 0)
			record(date, interest);
		accumulation = 0;
	}
	//显示账户信息
	public void show() {
		System.out.println("#"+id + "\tBalance: " + balance);
	}
}

package 银行管理系统5;

public class m5_11 {
	public static void main(String[] args) {
		//建立几个账户
			SavingsAccount sa0=new SavingsAccount(1, 21325302, 0.015);
			SavingsAccount sa1=new SavingsAccount(1, 58320212, 0.015);

			//几笔账目
			sa0.deposit(5, 5000);
			sa1.deposit(25, 10000);
			sa0.deposit(45, 5500);
			sa1.withdraw(60, 4000);

			//开户后第90天到了银行的计息日,结算所有账户的年息
			sa0.settle(90);
			sa1.settle(90);

			//输出各个账户信息
			sa0.show();	
			sa1.show();
			System.out.println("Total: "+ SavingsAccount.total);
	}
}

3.0版本(第六章)

第六章增添了字符串、对象数组。
改动的一些点:
(1)java中字符串类型不需要像C++那样还要调用std::string,而直接使用默认包lang中String。
(2)在使用Date对象引用作参数时,由于java没有引用类型的符号&,所以在作函数形参时直接使用Date对象。
(3)在创建Date对象时,java不能直接使用类名创建类的对象,而需要用Date date=new Date(int,int,int)才能声明并初始化一个Date对象,同样当需要创建一个Date对象作参数时也需要new一个Date对象。
(4)java里面对象数组有多种写法,其中的每一个对象也都需要和上面说的方法一样一个一个new。最简单的写法就是:SavingsAccount[]accounts={new SavingsAccount(date,String,double),new …}
(5)C++中的sizeof()函数用来计算对象所占内存空间大小,在java里没有。所以将n=sizeof(accounts)/sizeof(SavingsAccount)改成了accounts.length()。其中length()方法返回数组长度,实现的功能相同。

Java改动后的源码:

package 银行管理系统6;
import 银行管理系统6.Date;
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(final 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 void error(final String msg){
		System.out.println("Error(#" + id + "): " + msg);
	}
		//获得到指定日期为止的存款金额按日累积值
	private final double accumulate(final Date date){
			return accumulation + balance * date.distance(lastDate);
		}
		//构造函数
	public Account(final Date date, final String id, double rate) {
		this.id=id;
		balance=0; 
		this.rate=rate;
		lastDate=date;
		accumulation=0;
		date.show();
		System.out.println( "\t#" + id + " created");
	}
	public final String getId(){ return id; }
	public final double getBalance(){ return balance; }
	public final double getRate(){ return rate; }
	public static double getTotal() { return total;}
		//存入现金
	public void deposit(final Date date, double amount,final String desc) {
		record(date, amount, desc);
	}
		//取出现金
	public void withdraw(final Date date, double amount, final String desc) {
		if (amount > getBalance())
			error("not enough money");
		else
			record(date, -amount, desc);
	}
		//结算利息,每年1月1日调用一次该函数
	public 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;
	}
		//显示账户信息
	public final void show() {
		System.out.println(id + "\tBalance: " + balance);
	}
}
package 银行管理系统6;

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;	//该日期是从公元元年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.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 final int getDay(){ return day; }
	//获得当月有多少天
	public final int getMaxDay() {
		if (isLeapYear() && month == 2)
			return 29;
		else
			return DAYS_BEFORE_MONTH[month]- DAYS_BEFORE_MONTH[month - 1];
	}
	public final boolean isLeapYear(){	//判断当年是否为闰年
			return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
		}
	//输出当前日期
	public final void show() {
		System.out.println(getYear() + "-" + getMonth() + "-" + getDay());
	}
		//计算两个日期之间差多少天	
	public final int distance(final Date date) {
			return totalDays - date.totalDays;
		}
}
package 银行管理系统6;

public class m6_25 {
public static void main(String[] args) {
	Date date=new Date(2008, 11, 1);	//起始日期
	//建立几个账户
	Account[] accounts = new Account[2];
	accounts[0]=new Account(date, "S3755217", 0.015);
	accounts[1]=new Account(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");

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

4.0版本(第七章)

第七章主要改动继承与派生,抽象出父类,增添子类。
改动的一些点:
(1)java中类的继承用的是extends关键字,而非C++里的class SavingsAccount:public Account{};而且java没有C++里的继承类型。
(2)由于两类账户计息对象和周期不同,但都需要将某个数值(余额或欠款金额)按日累加,所以选择建立新类Accumulator。同上一章类似,在调用Accumulator对象时注意使用new。
(3)由于子类2中的show()函数显示的与父类不尽相同,在调用父类show(),之后还要将额外的信息输出。
(4)对于继承自父类的构造方法或者方法中需要调用父类同名方法,java与C++很大的不同是引入了关键字super,并且在子类构造方法中需要首先用super(Date,String)去初始化继承自父类那部分的域变量。

Java改动后的源码:
Date类同3.0版本。

package 银行管理系统7;
import 银行管理系统6.Date;
import 银行管理系统7.accumulator;
public class Account {
	private String id;	//帐号
	private double balance;	//余额
	private static double total=0; //所有账户的总金额
		//供派生类调用的构造函数,id为账户
	protected Account( Date date, String id) {
		this.id=id;
		balance=0; 
		date.show();
		System.out.println( "\t#" + id + " created");
	}
		//记录一笔帐,date为日期,amount为金额,desc为说明
	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 final void error(final String msg) {
		System.out.println( "Error(#" + id + "): " + msg);
	}
	
	public final String getId()  { return id; }
	public final double getBalance() { return balance; }
	public static double getTotal() { return total; }
		//显示账户信息
	public void show(){
		System.out.println( id + "\tBalance: " + balance);
	}
}
	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);
		}
		public final double getRate(){ return rate; }
		//存入现金
		public void deposit( Date date, double amount, String desc) {
			record(date, amount, desc);
			acc.change(date, getBalance());
		}
		//取出现金
		public void withdraw( Date date, double amount,  String desc) {
			if (amount > getBalance()) {
				error("not enough money");
			} else {
				record(date, -amount, desc);
				acc.change(date, getBalance());
			}
		}
		//结算利息,每年1月1日调用一次该函数
		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());
		}
	}

	class CreditAccount extends Account { //信用账户类

		private accumulator acc;	//辅助计算利息的累加器
		private double credit;		//信用额度
		private double rate;		//欠款的日利率
		private double fee;			//信用卡年费

		private final double getDebt(){	//获得欠款额
			double balance = getBalance();
			return (balance < 0 ? balance : 0);
		}
		//构造函数
		public CreditAccount( Date date,  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.0);
		}
		public final double getCredit(){ return credit; }
		public final double getRate() { return rate; }
		public final double getFee(){ return fee; }
		public final 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());
			}
		}
		//结算利息和年费,每月1日调用一次该函数
		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 final void show() {
			super.show();
			System.out.println( "\tAvailable credit:" + getAvailableCredit());
		}
}
package 银行管理系统7;
import 银行管理系统6.Date;
public class accumulator {

	private Date lastDate;	//上次变更数值的时期
	private double value;	//数值的当前值
	private double sum;		//数值按日累加之和
		//构造函数,date为开始累加的日期,value为初始值
	public accumulator(Date date, double value) {
		lastDate=date;
		this.value=value;
		sum=0;
	}
			//获得到日期date的累加结果
	public final 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 银行管理系统7;
import 银行管理系统6.Date;
public class m7_10 {
	public static void main(String[] args) {
	Date date=new Date(2008, 11, 1);	//起始日期
	//建立几个账户
	SavingsAccount sa2=new SavingsAccount(date, "02342342", 0.015);
	SavingsAccount sa1=new SavingsAccount(date, "S3755217", 0.015);
	CreditAccount ca=new CreditAccount(date, "C5392394", 10000, 0.0005, 50);
	//11月份的几笔账目
	sa1.deposit(new Date(2008, 11, 5), 5000, "salary");
	ca.withdraw(new Date(2008, 11, 15), 2000, "buy a cell");
	sa2.deposit(new Date(2008, 11, 25), 10000, "sell stock 0323");
	//结算信用卡
	ca.settle(new Date(2008, 12, 1));
	//12月份的几笔账目
	ca.deposit(new Date(2008, 12, 1), 2016, "repay the credit");
	sa1.deposit(new Date(2008, 12, 5), 5500, "salary");
	//结算所有账户
	sa1.settle(new Date(2009, 1, 1));
	sa2.settle(new Date(2009, 1, 1));
	ca.settle(new Date(2009, 1, 1));
	//输出各个账户信息
	sa1.show(); 
	sa2.show();
	ca.show(); 
	System.out.println("Total: " + Account.getTotal());
}
}

5.0版本(第八章)

这一版本新增了多态特性。
改动的一些点:
(1)由于C++中有运算符重载而JAVA中没有,故此点不做改动。
(2)在JAVA中没有C++中虚函数的virtual关键字,取而代之的是父类中的抽象方法,同C++一样,抽象父类都是使用abstract关键字,不过在JAVA中,子类实现父类方法时就相当于完成了C++中虚函数的功能。
(3)JAVA中没有sizeof()来计算数组所占内存空间大小,故在计算对象数组长度时,采用对象数组引用的length静态域变量直接得到数组长度。
(4)对于输入方面,JAVA中没有getline(cin,string)来读入输入的一行命令,而是通过扫描器类Scanner以及其中一系列nextXX()方法,如next()读入输入的字符串,nextLine()读入一行。输入的文本以空格等为分隔符。另外为了能够只读入一个字符,可以使用String类的charAt(index)方法。

Java改动后的源码:

package 银行管理系统8;

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;		//日
	public 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.print("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 final int getYear() { return year; }
	public final int getMonth(){ return month; }
	public final int getDay(){ return day; }
	//获得当月有多少天
	public final int getMaxDay() {
		if (isLeapYear() && month == 2)
			return 29;
		else
			return DAYS_BEFORE_MONTH[month]- DAYS_BEFORE_MONTH[month - 1];
	}
	public final boolean isLeapYear() {	//判断当年是否为闰年
			return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
		}
	//输出当前日期
	public final void show() {
		System.out.println( getYear() + "-" + getMonth() + "-" + getDay());		
	}
		//计算两个日期之间差多少天	
}

package 银行管理系统8;

public class accumulator {
	private Date lastDate;	//上次变更数值的时期
	private double value;	//数值的当前值
	private double sum;		//数值按日累加之和
		//构造函数,date为开始累加的日期,value为初始值
	public accumulator(final Date date, double value) {
		lastDate=date;
		this.value=value;
		sum=0;
	}
		//获得到日期date的累加结果
	public final double getSum(final Date date) {
			return sum + value * (date.totalDays - lastDate.totalDays);
		}

		//在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 银行管理系统8;

public abstract class Account {
	private String id;	//帐号
	private double balance;	//余额
	private static double total; //所有账户的总金额
		//供派生类调用的构造函数,id为账户
	protected Account(final Date date, final String id) {
		this.id=id;
		balance=0;
		date.show();
		System.out.println("\t#" + id + " created");
	}
		//记录一笔帐,date为日期,amount为金额,desc为说明
	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 final void error(final String msg) {
		System.out.println( "Error(#" + id + "): " + msg);
	}
	public final String getId() { return id; }
	public final double getBalance() { return balance; }
	public static double getTotal() { return total; }
		//存入现金,date为日期,amount为金额,desc为款项说明
	abstract public void deposit(final Date date, double amount, final String desc);
		//取出现金,date为日期,amount为金额,desc为款项说明
	abstract public void withdraw(final Date date, double amount, final String desc);
		//结算(计算利息、年费等),每月结算一次,date为结算日期
	abstract public void settle(final Date date) ;
		//显示账户信息
	abstract public void show();
	};

	 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 final 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.totalDays -(new Date(date.getYear() - 1, 1, 1)).totalDays);
				if (interest != 0)
					record(date, interest, "interest");
				acc.reset(date, getBalance());
			}
		}
		public void show() {
			System.out.println( getId() + "\tBalance: " + getBalance());
		}
	};

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

		private final 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 final double getCredit(){ return credit; }
		public final double getRate() { return rate; }
		public final double getFee() { return fee; }
		public final 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() {
			System.out.println( getId() + "\tBalance: " + getBalance());
		}
}

package 银行管理系统8;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class m8_8 {
	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 s = new Scanner(System.in);
	do {
		//显示日期和总金额
		date.show();
		System.out.println("\tTotal: " + Account.getTotal() + "\tcommand> ");

		int index, day;
		double amount;
		String desc;
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		cmd=(char)br.read();
		switch (cmd) {
		case 'd':	//存入现金
			index=s.nextInt();
			amount=s.nextDouble();
			desc=s.nextLine();
			accounts[index].deposit(date, amount, desc);
			break;
		case 'w':	//取出现金
			index=s.nextInt();
			amount=s.nextDouble();
			desc=s.nextLine();
			accounts[index].withdraw(date, amount, desc);
			break;
		case 's':	//查询各账户信息
			for (int i = 0; i < n; i++) {
				System.out.print( "[" + i + "] ");
				accounts[i].show();
			}
			break;
		case 'c':	//改变日期
			day= s.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);
			break;
		}
	} while (cmd != 'e');
	s.close();
}
}

6.0版本(第九章)

这一章最难,C++新增了类模板和Array数组,我查阅了Java自带的ArrayList类才做出来。

Java改动后的源码:
Date类、accumulator类同5.0版本,C++源码中的Array类直接丢弃,使用Java.util包里的ArrayList类。

package 银行管理系统9;
import 银行管理系统8.Date;
import 银行管理系统8.accumulator;
abstract public 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;
		balance=0;
		date.show();
		System.out.println( "\t#" + id + " created");
	}
		//记录一笔帐,date为日期,amount为金额,desc为说明
	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 final 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; }
		//存入现金,date为日期,amount为金额,desc为款项说明
	abstract public void deposit(final Date date, double amount, final String desc);
		//取出现金,date为日期,amount为金额,desc为款项说明
	abstract public void withdraw(final Date date, double amount, final String desc);
		//结算(计算利息、年费等),每月结算一次,date为结算日期
	abstract public void settle(final Date date) ;
		//显示账户信息
	public void show() {
		System.out.println( id + "\tBalance: " + balance);
	}
	
	};

	class SavingsAccount extends Account { //储蓄账户类
		private accumulator acc;	//辅助计算利息的累加器
		double rate;		//存款的年利率
		//构造函数
		public SavingsAccount(final Date date, final String id, double rate) {
			super(date, id);
			this.rate=rate;
			acc=new accumulator(date, 0);
		}
		public final 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.totalDays - (new Date(date.getYear() - 1, 1, 1)).totalDays);
				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 final 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 final double getCredit() { return credit; }
		public final double getRate() { return rate; }
		public final double getFee(){ return fee; }
		public final 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 final void show() {
			super.show();
			System.out.println( "\tAvailable credit:" + getAvailableCredit());
		}
	}
package 银行管理系统9;
import java.util.*; 
import 银行管理系统8.Date;;
public class m9_16{
/**
 * @param args
 */
public static void main(String[] args){
		Date date=new Date(2008, 11, 1);	//起始日期
		ArrayList<Account > accounts=new ArrayList<Account>();	//创建账户数组,元素个数为0
		System.out.println( "(a)add account (d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" );
		char cmd;
		Scanner s=new Scanner(System.in);
		do {
			//显示日期和总金额
			date.show();
			System.out.println("\tTotal: " + Account.getTotal() + "\tcommand> ");

			char type;
			int index, day;
			double amount, credit, rate, fee;
			String id, desc;
			Account account;
            cmd=(s.next()).charAt(0);
			switch (cmd) {
			case 'a':	//增加账户
				type=(s.next()).charAt(0);
				id=s.next();
				if (type == 's') {
					rate=s.nextDouble();
					account = new SavingsAccount(date, id, rate);
				} else {
					credit=s.nextDouble();
					rate=s.nextDouble();
					fee=s.nextDouble();
					account = new CreditAccount(date, id, credit, rate, fee);
				}
				accounts.add(account);
				break;
			case 'd':	//存入现金
				index=s.nextInt();
				amount=s.nextDouble();
				desc=s.nextLine();
				((Account) accounts.toArray()[index]).deposit(date, amount, desc);
				break;
			case 'w':	//取出现金
				index=s.nextInt();
				amount=s.nextDouble();
				desc=s.nextLine();
				((Account) accounts.toArray()[index]).withdraw(date, amount, desc);
				break;
			case 's':	//查询各账户信息
				for (int i = 0; i < accounts.size(); i++) {
					System.out.print( "[" + i + "] ");
					((Account)accounts.toArray()[i]).show();
				}
				break;
			case 'c':	//改变日期
				day=s.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++)
					((Account)accounts.toArray()[i]).settle(date);
				break;
			}
		} while (cmd != 'e');
		s.close();
}
}
<think>好的,用户遇到了Git合并时本地更会被覆盖的问题,需要解决方案。根据之前的引用资料,解决方法主要有commit、stash和revert三种。首先,我得确认用户的具体情况,比如他们的本地更是否已经完成,是否需要保留,或者是否可以丢弃。用户可能不太清楚这三种方法的适用场景,需要详细解释每个选项的步骤和优缺点。比如,commit适用于需要保留更的情况,stash适合临时保存以便后续恢复,而revert则是直接撤销更,适用于不需要保留的情况。需要结构清晰,分点说明,每个方法都给出操作步骤和适用场景,帮助用户理解哪种情况最适合他们。还要注意使用中文,避免技术术语过于复杂,让用户容易理解。同时,生成相关问题,可能涉及如何恢复stash的内容,或者如何避免频繁冲突等,扩展用户的知识点。另外,要确保LaTeX格式正确,但在这个问题中可能用不上数学公式,所以主要关注代码块和引用标识。检查引用是否正确,比如引用[^1]和[^2]的来源是否相关,用户提供的引用内容是否准确对应解决方案。 最后,确保回答真实可靠,操作步骤准确无误,避免误导用户。可能还需要提醒用户根据具体情况选择方法,比如如果有未完成的实验性代码,stash可能是更好的选择,而如果是确定要保留的修,则应该commit。</think>要解决Git合并时本地更被覆盖的问题,可根据不同场景选择以下三种方法: ### 一、提交本地更(commit) **适用场景**:当本地更已测试通过且需要保留到版本历史中。 **操作步骤**: 1.动添加到暂存区: ```bash git add . # 添加所有文件 # 或指定文件 git add file1.txt file2.py ``` 2. 提交更并添加描述: ```bash git commit -m "完成用户登录模块优化" ``` 3. 执行合并操作: ```bash git merge origin/main ``` **优点**:保留完整的开发记录,便于后续追溯[^1]。 ### 二、暂存更(stash) **适用场景**:需要临时保存未完成的修(如调试代码),优先处理合并操作。 **操作步骤**: 1. 保存当前工作状态: ```bash git stash # 可选添加备注 git stash save "用户登录模块调试代码" ``` 2. 查看暂存记录列表: ```bash git stash list ``` 3. 合并远程分支: ```bash git merge origin/main ``` 4. 恢复暂存内容: ```bash git stash pop # 恢复并删除最近一次暂存 # 或指定特定暂存 git stash apply stash@{1} ``` **优点**:保持工作目录整洁,特别适合处理紧急合并任务[^2]。 ### 三、撤销本地更(revert) **适用场景**:本地修可丢弃(如实验性代码或错误修)。 **操作步骤**: 1. 撤销所有未提交的修: ```bash git checkout -- . # 全部文件 # 或指定文件 git checkout -- file1.txt ``` 2. 执行合并操作: ```bash git merge origin/main ``` **风险提示**:此操作不可逆,需确认文件无需保留。 --- ### 对比决策表 | 方法 | 保留记录 | 可恢复性 | 适用阶段 | |---------|----------|----------|------------------| | commit | ✅ | ✅ | 功能开发完成时 | | stash | ❌ | ✅ | 开发中途临时切换 | | revert | ❌ | ❌ | 放弃无效修 | ### 扩展应用场景 - 当存在多个暂存时,可用`git stash list`查看记录,通过`git stash apply stash@{n}`精准恢复 - 合并后出现冲突时,可使用`git mergetool`可视化工具解决冲突 - 长期分支开发推荐使用`git rebase`保持提交历史线性
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值