模块五、Account类的两个子类

练习1、在银行项目中创建Account的两个子类:SavingAccount和CheckingAccount
在这里插入图片描述
实训目的:
继承、多态、方法的重写。
提示:
创建Account 类的两个子类:SavingAccount和CheckingAccount子类
a. 修改Account 类;将balance 属性的访问方式改为protected
b. 创建SavingAccount类,该类继承Account 类
c. 该类必须包含一个类型为double 的interestRate属性
d. 该类必须包括带有两个参数(balance 和interest_rate)的公有构造器。该构造器必须通过调用super(balance)将balance 参数传递给父类构造器。

实现CheckingAccount类
1.CheckingAccount类必须扩展Account 类
2.该类必须包含一个类型为double 的overdraftProtection属性。
3.该类必须包含一个带有参数(balance)的共有构造器。该构造器必须通过调用super(balance)将balance 参数传递给父类构造器。
4.给类必须包括另一个带有两个参数(balance 和protect)的公有构造器。该构造器必须通过调用super(balance)并设置overdragtProtection属性,将balance 参数传递给父类构造器。
5.CheckingAccount类必须覆盖withdraw 方法。此方法必须执行下列检查。如果当前余额足够弥补取款amount,则正常进行。如果不够弥补但是存在透支保护,则尝试用overdraftProtection得值来弥补该差值(balance-amount)。如果弥补该透支所需要的金额大于当前的保护级别。则整个交易失败,但余额未受影响。
6.编写并执行TestBanking程序。

//修改Account 类;将balance 属性的访问方式改为protected
在这里插入图片描述
SavingAccount类:

package banking;

public class SavingAccount extends Account {

	//声明一个私有变量interestRate表示利率
	private double interestRate;
	public SavingAccount(double balance,double interestRate) {
		super(balance);
		this.interestRate =interestRate; 
	}
	
}

CheckingAccount类:

package banking;

public class CheckingAccount extends Account{

	//声明一个变量overdraftProtection用来表示可以透支的额度
	private double overdraftProtection;
	
	public CheckingAccount(double balance) {
		super(balance);
	}
	public CheckingAccount(double balance,double overdraftProtection) {
		super(balance);
		this.overdraftProtection = overdraftProtection;
	}
	@Override
	public boolean withdraw(double amt) {
		// 如果当前余额足够弥补取款amount,则正常进行
		// 如果不够弥补但是存在透支保护,
		// 则尝试用overdraftProtection得值来弥补该差值(balance-amount)
		if (balance >= amt) {
			balance-=amt;
			return true;
		}else if (overdraftProtection +balance>= amt){
			amt -=balance;
			overdraftProtection -=amt;
			// 对balance设置值为0
			this.balance = 0;
			return true;
		}
		return false;
	}
}

package banking;

import org.omg.CORBA.PRIVATE_MEMBER;

public class TestBanking5 {
     
	public static void main(String[] args) {
		// 创建一个bank对象
		Bank bank = new Bank(); 
		Account account;
		Customer customer;
		// 用来记录每次账户操作的结果
		boolean result;
		
		//1.针对每个客户, 首先先在Bank()中进行构造--调用addCustomer()方法实现新建客户
		// 然后 bank.getCustomer() 返回该customer 对象;   
        //2.接着声明一个账户account类 ,调用方法  customer.setAccount()设置其本人的账户;
        //3.通过步骤1-2,把bank类,customer类和account类的关系链接到了一起
		bank.addCustomer("Jane", "Smith");
		account = new SavingAccount(500.00, 0.03);
		customer = bank.getCustomer(0);
		customer.setAccount(account);
		System.out.println("Creating the customer Jane Smith.");
		System.out.println("Creating her Savings Account with a 500.00 balance and 3% interest.");
		
		bank.addCustomer("Owen", "Bryant");
		System.out.println("Creating the customer Owen Bryant.");
		account = new CheckingAccount(500.00,0);
		customer = bank.getCustomer(1);
		customer.setAccount(account);
		System.out.println("Creating his Checking Account with a 500.00 balance and no overdraft protection.");
		
		bank.addCustomer("Tim", "Soley");
		account  = new CheckingAccount(500.00, 500.00);
		customer = bank.getCustomer(2);
		customer.setAccount(account);
		System.out.println("Creating the customer Tim Soley");
		System.out.println("Creating his Checking Account with a 500.00 balance and 500.00 in overdraft protection.");
		
		bank.addCustomer("Maria", "Soley");
		customer = bank.getCustomer(3);
		account = bank.getCustomer(2).getAccount();
		customer.setAccount(account);
		System.out.println("Creating the customer Maria Soley.");
		System.out.println("Maria shares her Checking Account with her husband Tim.");
		System.out.println("Retrieving the customer Jane Smith with her savings account.");
		
		customer = bank.getCustomer(0);
		// 取钱
		result = customer.getAccount().withdraw(150.0);
		System.out.println("Withdraw 150.00: "+result);
		// 存钱
		result = customer.getAccount().deposit(22.50);
		System.out.println("Deposit 22.50: "+result);
		// 取钱
		result = customer.getAccount().withdraw(47.62);
		System.out.println("Withdraw 47.62: "+result);
				
		result = customer.getAccount().withdraw(400.0);
		System.out.println("Withdraw 400.00: "+result);
		System.out.println("Customer ["+customer.getLastName()+","+customer.getFirstName()
				 +"] has a balance of "+customer.getAccount().getBalance());
		
		
		// 操作Owen Bryant的账户
		System.out.println("Retrieving the customer Owen Bryant with his checking account with no overdraft protection.");
	    customer = bank.getCustomer(1);
	    result = customer.getAccount().withdraw(150.0);
		System.out.println("Withdraw 150.00: "+result);
		
		result = customer.getAccount().deposit(22.50);
		System.out.println("Deposit 22.50: "+result);
		
		result = customer.getAccount().withdraw(47.62);
		System.out.println("Withdraw 47.62: "+result);
				
		result = customer.getAccount().withdraw(400.0);
		System.out.println("Withdraw 400.00: "+result);
		System.out.println("Customer ["+customer.getLastName()+","+customer.getFirstName()
				 +"] has a balance of "+customer.getAccount().getBalance());
		
		// 操作Tim Soley的账户
		System.out.println("Retrieving the customer Tim Soley with his checking account that has overdraft protection.");
	    customer = bank.getCustomer(2);
	    result = customer.getAccount().withdraw(150.0);
		System.out.println("Withdraw 150.00: "+result);
		
		result = customer.getAccount().deposit(22.50);
		System.out.println("Deposit 22.50: "+result);
		
		result = customer.getAccount().withdraw(47.62);
		System.out.println("Withdraw 47.62: "+result);
				
		result = customer.getAccount().withdraw(400.0);
		System.out.println("Withdraw 400.00: "+result);
		System.out.println("Customer ["+customer.getLastName()+","+customer.getFirstName()
				 +"] has a balance of "+customer.getAccount().getBalance());
		
		// Maria Soley使用她丈夫的账户
		System.out.println("Retrieving the customer Maria Soley with her joint checking account with husband Tim.");
		customer = bank.getCustomer(3);
		result = customer.getAccount().deposit(150.0);
		System.out.println("Deposit 150.0: "+result);
		result = customer.getAccount().withdraw(750.0);
		System.out.println("Withdraw 750.0: "+result);
		System.out.println("Customer ["+customer.getLastName()+","+customer.getFirstName()
		 +"] has a balance of "+customer.getAccount().getBalance());
	}
}

在这里插入图片描述

  • 15
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值