模块五 *扩展练习2、实现更为复杂的透支保护机制

7 篇文章 1 订阅

使用客户储蓄账户完成透支保护。
在这里插入图片描述

实训目的:
继承、多态、方法的重写。
说明:
修改SavingAccount类
1.仿照练习1“Account类的两个子类”一节实现SavingsAccount类。
2.SavingAccount类必须扩展Account类。
3.该类必须包含一个类型为double的interestRate属性
4.该类必须包括一个带有两个参数(balance和interest_rate)的公有构造器。该构造器必须通过调用super(balance)来将balance参数传递给父类构造器
修改CheckingAccount类
1.仿照练习1“Account类的两个子类”一节实现CheckingAccount类。
2.CheckingAccount类必须扩展Account类
3.该类必须包括一个关联属性,称作protectedBy,表示透支保护。该属性的类型为SavingAccount,缺省值必须是null。除此之外该类没有其他的数据属性。
4.该类必须包括一个带有参数(balance)的公有构造器,该构造器必须通过调用super(balance)将balance参数传递到父类构造器。
5.修改构造器为CheckingAccount(double balance,SavingAccount protect)构造器。该构造器必须通过调用super(balance)将balance参数传递给父类构造器。
6. CheckingAccount类必须覆盖withdraw方法。该类必须执行下面的检查:如果当前余额足够弥补取款amount,则正常进行。如果不够弥补但是存在透支保护则尝试用储蓄账户来弥补这个差值(balance-amount)。如果后一个交易失败,则整个交易一定失败,但余额未受影响。
在这里插入图片描述

修改Customer类,使其拥有两个账户:一个储蓄账户和一个支票账户:两个都是可选的。
在模块四的基础上修改,原来的Customer类包含一个称作account的关联属性,可用该属性控制一个Account对象。重写这个类,使其包含两个关联属性:
savingAccount和checkingAccount,这两个属性的缺省值是null
2.包含两个访问方法:getSaving和getChecking,这两个方法分别返回储蓄和支票总数。
7. 包含两个相反的方法:SetSaving和setChecking,这两个方法分别为savingAccount和checkingAccount这两个关联属性赋值。
8.
完成TestBanking5_2程序并运行

CheckingAccount 类:

package banking;

public class CheckingAccount extends Account{

	
	// 关联属性,表示透支保护
	private SavingAccount protectedBy = null;
	
	public CheckingAccount(double balance) {
		super(balance);
	}
	public CheckingAccount(double balance,SavingAccount protect) {
		super(balance);
		this.protectedBy = protect;
	}
	@Override
	public boolean withdraw(double amt) {
		// 如果当前余额足够弥补取款amount,则正常进行
		if (super.getBalance() >= amt) {
			this.balance-=amt;
			return true;
		}else if (protectedBy !=null) {  // 如果不够弥补但是存在透支保护,
			// 则尝试用储蓄账户来弥补这个差值(balance-amount)
			if (super.getBalance()+protectedBy.getBalance() >= amt){
				amt -=this.balance;
				double savingAccountMoney = protectedBy.getBalance();
				 savingAccountMoney-=amt;
				 protectedBy.balance=savingAccountMoney;
				 //System.out.println(protectedBy.getBalance());
				// 对balance设置值为0
				this.balance = 0.0;
				//System.out.println(super.getBalance());
				return true;
			}
		}
		return false;
	}
}

Customer 类:

package banking;

public class Customer {

	private String firstName;
	private String lastName;
	private Account[] accounts;
	private Bank bank;
	
	private SavingAccount savingAccount;
	private CheckingAccount checkingAccount;
	// 记录账户个数
	private int numberOfAccounts;
	// 构造方法
	public Customer(String firstName,String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
		accounts = new Account[5];
		numberOfAccounts = 0;
	}
	
	public SavingAccount SetSaving(double balance,double interestRate) {
		return this.savingAccount = new SavingAccount(balance, interestRate);
	}
	// 返回储蓄总数
	public double getSaving() {
		return savingAccount.getBalance();
	}
	// 不存在透支保护的账户
	public CheckingAccount setChecking(double balance) {
		return this.checkingAccount = new CheckingAccount(balance);
	}
	// 存在透支保护的账户
	public CheckingAccount setChecking(double balance,SavingAccount savingAccount) {
		return this.checkingAccount = new CheckingAccount(balance,savingAccount);
	}
	// 返回支票总数
	public double getChecking() {
		return checkingAccount.getBalance();
	}
	public void addAccount(Account account) {
		accounts[numberOfAccounts] = account;
		numberOfAccounts++;
	}
	public String getFirstName() {
		return firstName;
	}

	public String getLastName() {
		return lastName;
	}

	// 返回指定账户
	public Account getAccount(int index) {
		return accounts[index];
	}
	// 返回账户个数
	public int getNumOfAccounts() {
		return numberOfAccounts;
	}
}

TestBanking5_2 类:

package banking;

public class TestBanking5_2 {

	public static void main(String[] args) {
		
		// 创建一个银行对象
		Bank bank = new Bank();
		Customer customer;
		CheckingAccount checkingAccount;
		SavingAccount savingAccount;
		// 用于保存每次操作的结果
		boolean result = false;
		
		// 1 在银行对象里增加客户,Customer会创建一个对象
		bank.addCustomer("Simms", "Jane");
		// 2 获取刚创建的客户对象
		customer = bank.getCustomer(0);
		// 3 为客户创建一个储蓄账户
		savingAccount = customer.SetSaving(500.0, 0);
		// 4 为客户创建一个支票账户,并将客户的储蓄账户作为其可透支的对象
		checkingAccount = customer.setChecking(200.0,savingAccount);
		// 5 将创建的账户绑定给客户
		customer.addAccount(checkingAccount);
        // 通过1-5步,将Bank,Customer,Account和Account两个子类联系在一起。
		System.out.println("Customer [Simms, Jane] has a checking balance of 200.0 "
				+ "and a savings balance of 500.0");
		result = checkingAccount.withdraw(150.0);
		System.out.println("Checking Acct [Jane Simms] : withdraw 150.00 succeeds?"+result);
		result = checkingAccount.deposit(22.50);
		System.out.println("Checking Acct [Jane Simms] : deposit 22.50 succeeds?"+result);
		result = checkingAccount.withdraw(147.62);
		System.out.println("Checking Acct [Jane Simms] : withdraw 147.62 succeeds?"+result);
		
		System.out.println("Customer ["+customer.getFirstName()+","+customer.getLastName()+"] has a checking balance of " +customer.getChecking() + " and a savings balance of "+customer.getSaving());
		
		
		bank.addCustomer("Bryant", "Owen");
		customer = bank.getCustomer(1);
		checkingAccount = customer.setChecking(200.0);
		result = checkingAccount.withdraw(100.0);
		System.out.println("Checking Acct [Owen Bryant] : withdraw 100.00 succeeds? "+result);
		result = checkingAccount.deposit(25.00);
		System.out.println("Checking Acct [Owen Bryant] : deposit 25.00 succeeds? "+result);
		result = checkingAccount.withdraw(175.00);
		System.out.println("Checking Acct [Owen Bryant] : withdraw 175.00 succeeds? "+result);
		System.out.println("Customer ["+customer.getFirstName()+","+customer.getLastName()
		+"] has a checking balance of "+checkingAccount.getBalance());
		
	}

}

在上一个拓展项目的基础上仅仅修改了CheckingAccount和Customer类,其余类均为变动。
在这里插入图片描述

  • 1
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值