Java教程

第7周大作业 C++改Java——个人银行账户管理系统

本文主要是介绍第7周大作业 C++改Java——个人银行账户管理系统,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

共6次改写

在这里插入图片描述

一,4_9: 第四章——类

package java4_9;

//储蓄账户类
class SavingsAccount{
	private int id,lastDate;		//账号 上次变更余额的时期		
	private	double balance,rate,accumulation;	//余额 存款的年利率 余额按日累加之和
	
	//构造函数
	public SavingsAccount(int date, int id, double rate){
		this.id = id;
		this.balance = 0;
		this.rate = rate;
		this.lastDate = date;
		this.accumulation = 0;
		System.out.println( date+"\t#"+ id +" is created" );
	}		
	
	//记录一笔帐,date为日期,amount为金额,desc为说明
	private	void record(int date, double amount){
		accumulation = accumulate(date);
		lastDate = date;
		balance += amount;
		System.out.println( date + "  #" + id + "   " + String.format("%.2f", amount) 
		+ "    "+ String.format("%.2f", balance) );
	}
	
	//获得到指定日期为止的存款金额按日累积值
	private	double accumulate(int date){
		return accumulation + balance * (date - lastDate);
	}
	
	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 java4_9 {
	
	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		//建立几个账户
		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();	
	}
}

二,5_11:第五章——增添静态属性与方法

增加静态的属性total,方便记录所有账号的总金额,同时静态方法,方便查询total.

静态方法可以直接通过"类名.方法"的形式进行方法调用。

package java5_11;

//储蓄账户类
class SavingsAccount{
	private int id,lastDate;		//账号 上次变更余额的时期		
	private	double balance,rate,accumulation;	//余额 存款的年利率 余额按日累加之和
	private static double total=0;	//所有账户的总金额
	
	//构造函数
	public SavingsAccount(int date, int id, double rate){
		this.id = id;
		this.balance = 0;
		this.rate = rate;
		this.lastDate = date;
		this.accumulation = 0;
		System.out.println( date+"\t#"+ id +" is created" );
	}			
	
	//记录一笔帐,date为日期,amount为金额,desc为说明
	private	void record(int date, double amount){
		accumulation = accumulate(date);
		lastDate = date;
		balance += amount;
		total += amount;
		System.out.println( date + "  #" + id + "   " + String.format("%.2f", amount) 
		+ "    "+ String.format("%.2f", balance) );
	}
	
	//获得到指定日期为止的存款金额按日累积值
	private	double accumulate(int date){
			return accumulation + balance * (date - lastDate);
	}
	
	public int getId() { return id; }
	public double getBalance() { return balance; }
	public double getRate() { return rate; }
	public static double getTotal() { return total; }

	//存入现金
	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 java5_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();System.out.println();	
		sa1.show();System.out.println();
		System.out.println( "Total: " + SavingsAccount.getTotal() );
	}
}

三,6_25:第六章——使用了对象数组,增添了字符串,多出一个类Date

在这里插入图片描述

Date.java

package java6_25;

public class Date{	//日期类

	public final int[] DAYS_BEFORE_MONTH = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
	private int year,month,day,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.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 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.print(getYear()+"-"+getMonth()+"-"+ getDay());
	}
	
	//计算两个日期之间差多少天	
	public final int distance(final Date date) {
		return totalDays - date.totalDays;
	}
}

java6_25.java

package java6_25;

class SavingsAccount{
	private String id; //账号
	private Date lastDate;		// 上次变更余额的时期		
	private	double balance,rate,accumulation;	//余额 存款的年利率 余额按日累加之和
	private static double total=0;	//所有账户的总金额
	
	//构造函数
	public	SavingsAccount(final Date date, final 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+" is created");
	}		
	
	//记录一笔帐,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	double accumulate(final Date date){
		return accumulation + balance * date.distance(lastDate);
	}
	//报告错误信息
	public 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 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.print(id+"\tBalance: "+balance);
	}
}

public class java6_25 {
	
	 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; // => sizeof(accounts) / sizeof(SavingsAccount); 账户总数
        //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() );
	 }
}

四,7_10:第七章——多了一个类Accumulator及用了类的继承

抽象出了父类使得代码扩展性更高,父类Account,派生出子类creditAccount和savingsaccount。
继承写法:
class CreditAccount extends Account
class SavingsAccount extends Account

在这里插入图片描述

主函数:account7.java

package java7_10;

//账户类
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("\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 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.print(id + "\tBalance: " + balance);
	}

}


//储蓄账户类
class SavingsAccount extends Account { 
	private	Accumulator acc;	//辅助计算利息的累加器
	private double rate;		//存款的年利率

	//构造函数
	public	SavingsAccount(final Date date, final String id, double rate){
		super(date, id);
		//若一个父类只要有一个带参数的构造方法,
		// 那么在写其子类的构造方法时必须先通过super调用父类的构造方法而且super只能写在子类构造方法体内的第一行。
		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());
		}
	}
	
	//结算利息,每年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,rate,fee;		//信用额度 欠款的日利率 信用卡年费

	//构造函数
	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);
	}
	
	//获得欠款额
	private	final double getDebt() {	
		double balance = getBalance();
		return (balance < 0 ? balance : 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.print("\tAvailable credit:" + getAvailableCredit());
	}
}


public class account7 {

	public static void main(String[] args){
		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);
		//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));
		//输出各个账户信息
		System.out.println();
		sa1.show(); System.out.println();
		sa2.show(); System.out.println();
		ca.show(); System.out.println();
		System.out.println( "Total: " + Account.getTotal() );
	}
}

Accumulator.java

package java7_10;

public class Accumulator {	//将某个数值按日累加

	private Date lastDate;	//上次变更数值的时期
	private	double value,sum;	//数值的当前值 数值按日累加之和

	//构造函数,date为开始累加的日期,value为初始值
	public Accumulator(final Date date, double value)
	{ 
		this.lastDate=date;
		this.value=value;
		this.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;
	}
}

Date.java

package java7_10;

public class Date{	//日期类
	
	public final int[] DAYS_BEFORE_MONTH = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
	private int year,month,day,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.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 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.print(getYear()+"-"+getMonth()+"-"+ getDay());
	}
	
	//计算两个日期之间差多少天	
	public final int distance(final Date date) 
	{
		return totalDays - date.totalDays;
	}
}

五,8_8:第八章——用了多态

java里的多态主要体现在重载和覆盖,注意java里没有运算符的重载。类似c++里的虚函数,java里面有abstract关键字,只要某给个类里含有abstract方法,就必须在类前声明abstract。

主函数:account8.java

package java8_8;

import java.util.Scanner;

//账户类
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( "\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 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为款项说明
    public abstract void deposit(final Date date, double amount, final String desc);
    //取出现金,date为日期,amount为金额,desc为款项说明
    public abstract void withdraw(final Date date, double amount,final String desc);
    //结算(计算利息、年费等),每月结算一次,date为结算日期
    public abstract void settle(final Date date);
    
	
	//显示账户信息
	public void show(){
		System.out.println(id + "\tBalance: " + balance);
	}

}


//储蓄账户类
class SavingsAccount extends Account { 
	
	private	Accumulator acc;	//辅助计算利息的累加器
	private double rate;		//存款的年利率

	//构造函数
	public SavingsAccount(final Date date, final String id, double rate){
		super(date, id);
		//若一个父类只要有一个带参数的构造方法,
		// 那么在写其子类的构造方法时必须先通过super调用父类的构造方法而且super只能写在子类构造方法体内的第一行。
		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());
		}
	}
	
	//结算利息,每年1月1日调用一次该函数
	public	void settle(final Date date){
		double interest = acc.getSum(date) * rate	//计算年息
				/ date.operator(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,rate,fee;		//信用额度 欠款的日利率 信用卡年费

	//构造函数
	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);
	}
	
	//获得欠款额
	private	final double getDebt() {	
		double balance = getBalance();
		return (balance < 0 ? balance : 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() );
	}
}


public class account8 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		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;// => sizeof(accounts) / sizeof(SavingsAccount); 账户总数

		System.out.println( "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" );
		char cmd;
		do {
			//显示日期和总金额
			date.show();
			System.out.println( "\tTotal: " +Account.getTotal() + "\tcommand> " );

			int index, day;
			double amount;
			String desc; 

			@SuppressWarnings("resource")
			Scanner input=new Scanner(System.in);
			String c=input.next();
            cmd=c.charAt(0);

			switch (cmd) {
			case 'd':	//存入现金
				index=input.nextInt();
                amount=input.nextDouble();
                desc=input.next();
                accounts[index].deposit(date, amount, desc);
				break;
			case 'w':	//取出现金
				index=input.nextInt();
				amount=input.nextDouble();
				desc=input.next();
				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();
				}
				break;
			case 'c':	//改变日期
				day=input.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');
	}

}

Accumulator.java

package java8_8;

//将某个数值按日累加
public class Accumulator {	

	private Date lastDate;	//上次变更数值的时期
	private	double value,sum;	//数值的当前值 数值按日累加之和

	//构造函数,date为开始累加的日期,value为初始值
	public Accumulator(final Date date, double value){ 
		this.lastDate=date;
		this.value=value;
		this.sum=0;
	}

	//获得到日期date的累加结果
	public final double getSum(final Date date) {
		return sum + value * (date.operator(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;
	}
}

Date.java

package java8_8;

//日期类
public class Date{	
	
	public final int[] DAYS_BEFORE_MONTH = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
	private int year,month,day,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("\n");
			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 operator (final  Date date) {
		return totalDays - date.totalDays;
	}
}

九,9_16:第九章——使用了容器来代替数组

注意到java中没有指针和模板,同时java里也没有char。
输入字符串,然后通过charAt(0)取出第一个字符。

主函数:account9.java

package java9_16;

import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.*;
import java.util.Scanner;

//账户类
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( "\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 void error(final String msg) {
		System.out.println("Error(#" + id + "): " + msg + "\n");
	}
	
	public final String getId()  { return id; }
	public final double getBalance()  { return balance; }
	public static double getTotal() { return total; }
	
	//存入现金,date为日期,amount为金额,desc为款项说明
    public abstract void deposit(final Date date, double amount, final String desc);
    //取出现金,date为日期,amount为金额,desc为款项说明
    public abstract void withdraw(final Date date, double amount,final String desc);
    //结算(计算利息、年费等),每月结算一次,date为结算日期
    public abstract void settle(final Date date);
    
	//显示账户信息
	public void show(){
		System.out.println( id + "\tBalance: " + balance );
	}

}


//储蓄账户类
class SavingsAccount extends Account { 
	
	private	Accumulator acc;	//辅助计算利息的累加器
	private double rate;		//存款的年利率

	//构造函数
	public SavingsAccount(final Date date, final String id, double rate){
		super(date, id);
		//若一个父类只要有一个带参数的构造方法,
		// 那么在写其子类的构造方法时必须先通过super调用父类的构造方法才能完成子类的构造方法而且super只能写在子类构造方法体内的第一行。
		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());
		}
	}

	//结算利息,每年1月1日调用一次该函数
	public	void settle(final Date date){
		double interest = acc.getSum(date) * rate	//计算年息
				/ date.operator(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,rate,fee;		//信用额度 欠款的日利率 信用卡年费

	//构造函数
	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);
	}
	
	//获得欠款额
	private	final double getDebt() {	
		double balance = getBalance();
		return (balance < 0 ? balance : 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() );
	}
}




public class account9 {
	
	public static void main(String[] args) {
        Date date=new Date(2008, 11, 1);	//起始日期
        
        //Array<Account *> accounts(0); 创建账户数组,元素个数为0
        ArrayList<Account> accounts = new ArrayList<>();
        
        System.out.println("(a)add account (d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" );
 
        char cmd;
        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;
 
            @SuppressWarnings("resource")
			Scanner input=new Scanner(System.in);
            String c=input.next();
            cmd=c.charAt(0);
            switch (cmd) {
                case 'a':	//增加账户
                    Scanner inputt=new Scanner(System.in);
                    String t=inputt.next();
                    type=t.charAt(0);
                    id=input.next();
                    if (type == 's') {
                        rate=input.nextDouble();
                        account = new SavingsAccount(date, id, rate);
                    } else {
                        credit=input.nextDouble();
                        rate=input.nextDouble();
                        fee=input.nextDouble();
                        account = new CreditAccount(date, id, credit, rate, fee);
                    }
//                  accounts.resize(accounts.getSize() + 1);
//        			accounts[accounts.getSize() - 1] = account;
                    accounts.add(account);
                    break;
                case 'd':	//存入现金
                    index=input.nextInt();
                    amount=input.nextDouble();
                    desc=input.next();
                    accounts.get(index).deposit(date, amount, desc);
                    break;
                case 'w':	//取出现金
                    index=input.nextInt();
                    amount=input.nextDouble();
                    desc=input.next();
                    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();
                    }
                    break;
                case 'c':	//改变日期
                    day=input.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);
                    break;
            }
        } while (cmd != 'e');
 
        for (int i = 0; i < accounts.size(); i++)
            accounts.remove(i);
    }
}

Accumulator.java

package java9_16;

//将某个数值按日累加
public class Accumulator {

	private Date lastDate;	//上次变更数值的时期
	private	double value,sum;	//数值的当前值 数值按日累加之和

	//构造函数,date为开始累加的日期,value为初始值
	public Accumulator(final Date date, double value){ 
		this.lastDate=date;
		this.value=value;
		this.sum=0;
	}

	//获得到日期date的累加结果
	public final double getSum(final Date date) {
		return sum + value * (date.operator(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;
	}
}

Date.java

package java9_16;

//日期类
public class Date{	
	
	public final int[] DAYS_BEFORE_MONTH = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
	private int year,month,day,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("\n");
			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 operator (final  Date date) {
		return totalDays - date.totalDays;
	}
}

这篇关于第7周大作业 C++改Java——个人银行账户管理系统的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!