zoukankan      html  css  js  c++  java
  • 练习5 练习目标:继承、多态、方法的重写。

    练习目标:继承、多态、方法的重写。 在本练习中,将在银行项目中创建Account的两个子类:SavingAccount 和 CheckingAccount。


    创建 Account类的两个子类:SavingAccount(存款账户) 和 CheckingAccount(透支账户)子类

    1.修改Account类;将balance属性的访问方式改为protected
    2.创建 SavingAccount 类,该类继承Account类
    3.该类必须包含一个类型为double的interestRate(利率)属性
    4.该类必须包括带有两个参数(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.在主exercise1目录中,编译并执行TestBanking程序。输出应为:
    Creating the customer Jane Smith.
    Creating her Savings Account with a 500.00 balance and 3% interest.
    Creating the customer Owen Bryant.
    Creating his Checking Account with a 500.00 balance and no overdraft protection.

    Creating the customer Tim Soley.
    Creating his Checking Account with a 500.00 balance and 500.00 in overdraft prot
    ection.
    Creating the customer Maria Soley.
    Maria shares her Checking Account with her husband Tim.

    Retrieving the customer Jane Smith with her savings account.
    Withdraw 150.00: true
    Deposit 22.50: true
    Withdraw 47.62: true
    Withdraw 400.00: false
    Customer [Simms, Jane] has a balance of 324.88

    Retrieving the customer Owen Bryant with his checking account with no overdraft
    protection.
    Withdraw 150.00: true
    Deposit 22.50: true
    Withdraw 47.62: true
    Withdraw 400.00: false
    Customer [Bryant, Owen] has a balance of 324.88

    Retrieving the customer Tim Soley with his checking account that has overdraft p
    rotection.
    Withdraw 150.00: true
    Deposit 22.50: true
    Withdraw 47.62: true
    Withdraw 400.00: true
    Customer [Soley, Tim] has a balance of 0.0

    Retrieving the customer Maria Soley with her joint checking account with husband
    Tim.
    Deposit 150.00: true
    Withdraw 750.00: false
    Customer [Soley, Maria] has a balance of 150.0

    ///SavingAccount
    package banking;

    public class SavingAccount extends Account {
    private double interestRate;

    public SavingAccount(double b) {
    	super(b);
    }
    
    public double getInterestRate() {
    	return interestRate;
    }
    
    public SavingAccount(double b, double i) {
    	super(b);
    	this.interestRate = i;
    }
    

    }

    ///CheckingAccount

    package banking;

    public class CheckingAccount extends Account {
    public double getOverdraftProtection() {
    return overdraftProtection;
    }

    double overdraftProtection;
    
    public CheckingAccount(double b) {
    	super(b);
    }
    
    public CheckingAccount(double b, double protect) {
    	super(b);
    	this.overdraftProtection = protect;
    }
    
    public boolean withdraw(double i)
    {
    	if(balance>=i)
    	{
    	balance-=i;
    	System.out.print("Withdraw "+i);
    	return true;
    	}
    	else if(balance+overdraftProtection>=i)
    	{
    		balance=0;
    		System.out.print("Withdraw "+i);
    		return true;
    	}
    	else
    	{
    		System.out.print("Withdraw "+i);
    		return false;
    	}
    }
    

    }

    ///Testbanking 类

    public class TestBanking {
    private static void d(Bank bk1)
    {
    System.out.println("create the customer "+bk1.getCustomer(bk1.getNumOfCustomers()).getFirstName()+" "+bk1.getCustomer(bk1.getNumOfCustomers()).getLastName()+".");
    }

    public static void main(String[] args) {
    
    
      
        Bank bk1=new Bank();
        bk1.addCustomer( "Jane" , "Smith" );
        d(bk1);
        SavingAccount a1=new SavingAccount(500,3);
        System.out.println("Creating her Savings Account with a "+a1.getBalance()+" balance "+"and"+a1.getInterestRate()+"% "+"interest.");
        bk1.addCustomer("Owen","Bryant");
        d(bk1);
        CheckingAccount a2=new CheckingAccount (500,0);
        System.out.println("Creating her Checking Account with a "+a2.getBalance()+" balance "+"and "+a2.getOverdraftProtection()+" overdraftprotection.");
        System.out.println();	    
        bk1.addCustomer("Tim","Soley");
        d(bk1);
        CheckingAccount a3=new CheckingAccount (500,500);
        System.out.println("Creating his Checking Account with a "+a3.getBalance()+" balance "+"and "+a3.getOverdraftProtection()+" overdraftprotection.");
        bk1.addCustomer("Marry","Soley");
        d(bk1);
        System.out.println("Maria shares her Checking Account with her husband Tim.");
        System.out.println();
        System.out.println("Retrieving the customer Owen Bryant with his checking account with no overdraft protection.");
        System.out.println(":"+a1.withdraw(150));
        System.out.println(":"+a1.deposit(22.50));
        System.out.println(":"+a1.withdraw(47.62));
        System.out.println(":"+a1.withdraw(400));
        System.out.println("Customer ["+bk1.getCustomer(1).getFirstName()+","+bk1.getCustomer(1).getLastName()+"] has a balance of "+a1.getBalance());
        System.out.println();
        
        System.out.println("Retrieving the customer Owen Bryant with his checking account with no overdraft protection.");
        System.out.println(":"+a2.withdraw(150));
        System.out.println(":"+a2.deposit(22.50));
        System.out.println(":"+a2.withdraw(47.62));
        System.out.println(":"+a1.withdraw(400));
        System.out.println("Customer ["+bk1.getCustomer(2).getFirstName()+","+bk1.getCustomer(2).getLastName()+"] has a balance of "+a2.getBalance());
        System.out.println();
        
        System.out.println("Retrieving the customer Tim Soley with his checking account that has overdraft protection.");
        System.out.println(":"+a3.withdraw(150));
        System.out.println(":"+a3.deposit(22.50));
        System.out.println(":"+a3.withdraw(47.62));
        System.out.println(":"+a3.withdraw(400));
        System.out.println("Customer ["+bk1.getCustomer(3).getFirstName()+","+bk1.getCustomer(3).getLastName()+"] has a balance of "+a3.getBalance());
        System.out.println();
        
        System.out.println("Retrieving the customer Maria Soley with her joint checking account with husband Tim.");
        System.out.println(":"+a3.deposit(150));
        System.out.println(":"+a3.withdraw(750));
        System.out.println("Customer ["+bk1.getCustomer(4).getFirstName()+","+bk1.getCustomer(4).getLastName()+"] has a balance of "+a3.getBalance());
    
    }
    

    }
    ///运行结果
    create the customer Jane Smith.
    Creating her Savings Account with a 500.0 balance and3.0% interest.
    create the customer Owen Bryant.
    Creating her Checking Account with a 500.0 balance and 0.0 overdraftprotection.

    create the customer Tim Soley.
    Creating his Checking Account with a 500.0 balance and 500.0 overdraftprotection.
    create the customer Marry Soley.
    Maria shares her Checking Account with her husband Tim.

    Retrieving the customer Owen Bryant with his checking account with no overdraft protection.
    Withdraw 150.0:true
    Deposit 22.5:true
    Withdraw 47.62:true
    Withdraw 400.0:false
    Customer [Jane,Smith] has a balance of 324.88

    Retrieving the customer Owen Bryant with his checking account with no overdraft protection.
    Withdraw 150.0:true
    Deposit 22.5:true
    Withdraw 47.62:true
    Withdraw 400.0:false
    Customer [Owen,Bryant] has a balance of 324.88

    Retrieving the customer Tim Soley with his checking account that has overdraft protection.
    Withdraw 150.0:true
    Deposit 22.5:true
    Withdraw 47.62:true
    Withdraw 400.0:true
    Customer [Tim,Soley] has a balance of 0.0

    Retrieving the customer Maria Soley with her joint checking account with husband Tim.
    Deposit 150.0:true
    Withdraw 750.0:false
    Customer [Marry,Soley] has a balance of 150.0

  • 相关阅读:
    CentOS7.4下载与安装
    Windows 环境下vue+webpack前端开发环境搭建
    PHPSSO通信一直失败。
    TortoiseGit安装和使用的图文教程
    TortoiseGit安装教程
    HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth之完全详解
    linux 安装xamp
    linux的rpm命令
    0和空的判断
    mysql中 case when的使用
  • 原文地址:https://www.cnblogs.com/nicebaby/p/5910776.html
Copyright © 2011-2022 走看看