• 【Java基础-实验7】Banking_7 -添加银行透支扣款系统的 thorw异常机制


    实验基本要求:

    实验题目 7:(在6基础上修改) 
     将建立一个 OverdraftException 异常,它由 Account 类的withdraw()方法 抛出。 
    
    实验目的: 自定义异常 
     
    实验说明: 
     创建 OverdraftException 类 
     1. 在 banking.domain 包中建立一个共有类 OverdraftException. 这个类 扩展 Exception 类。  2. 添加一个 double 类型的私有属性 deficit.增加一个共有访问方法 getDeficit 
     3. 添加一个有两个参数的共有构造器。deficit 参数初始化 deficit 属性 
     
    修改 Account 类 
     
    4. 重写 withdraw 方法使它不返回值(即 void).声明方法抛出 overdraftException异常  
    5. 修改代码抛出新异常,指明“资金不足”以及不足数额(当前余额扣除请求的数额) 
     
    修改 CheckingAccount 类 
    
     6. 重写 withdraw 方法使它不返回值(即 void).声明方法抛出 overdraftException 异常  
    
    7. 修改代码使其在需要时抛出异常。两种情况要处理:第一是存在没有透支保 护的赤字,对这个异常使用“no overdraft protection”信息。第二是 overdraftProtection 数 额 不 足 以 弥 补 赤 字 : 对 这 个 异 常 可 使 用 ”Insufficient funds for overdraft protection” 信息 
    View Code

    工程组织:(未展示的文件和实验6的基本完全一直)

    CheckingAccount.java  (钱款+额度不足, 不再返回int)

    package Banking_7;
    
    
    public class CheckingAccount extends Account {
        private double overdraft;//超额限额保护
        public CheckingAccount(double balance,double overd){
            super(balance);
            this.overdraft=overd;
        }
        public CheckingAccount(double balance){
            super(balance);
            this.overdraft=0;
        }
        public void showinfo(){
            System.out.println("您的余额:"+this.getBalance()+"	"+
                    "您的可透支余额:"+this.overdraft);
        }
        public void withdraw(double amt) throws OverdraftException{
            if(amt<=super.getBalance())
                super.setBalance(super.getBalance()-amt );
            else{
                if(this.overdraft==0){
                    throw new OverdraftException("no overdraft protection(没有透支保护):",(amt-this.balance));
                }
    
                double val=amt-super.getBalance();
                if(val<=this.overdraft)
                {
                    super.setBalance(0);
                    this.overdraft-=val;
                }
                else{
                    throw new OverdraftException("Insufficient funds for overdraft protection: 数额不足以弥补赤字," +
                            "缺少资金或额度:",val-this.overdraft);
                }
            }
        }
    }
    View Code

    Account.java

    package Banking_7;
    public class Account {
    
        protected double balance=0;//余额  ,uml前该变量是 '-'
        public Account(double init_balance){
            balance=init_balance;
        }
        public double getBalance() {
            return balance;
        }
        public void setBalance(double b){this.balance=b;}
        //存钱
    
        public boolean deposit(double amt){
            this.balance+=amt;return true;
        }
        //取钱
        public void withdraw(double amt) throws OverdraftException{
            if(amt>this.balance){
                throw new OverdraftException("资金不足,缺少金额:",(amt-this.balance));
            }
            else{
                this.balance-=amt;
            }
        }
    }
    View Code

    OverdraftException.java (透支额度的 异常保护机制 )

    package Banking_7;
    
    public class OverdraftException extends Exception{
        private double deficit;
    
        public double getDeficit() {
            return deficit;
        }
    
        public void setDeficit(double deficit) {
            this.deficit = deficit;
        }
    
        public OverdraftException(double deficit) {
            this.deficit = deficit;
        }
    
        public OverdraftException(String message,double deficit) {
            super(message);
            this.deficit=deficit;
            System.out.println(message+deficit);
        }
    
    }
    View Code

    TestBanking_7.java  (测试类)

    package TestBanks;/*
     * This class creates the program to test the banking classes.
     * It creates a set of customers, with a few accounts each,
     * and generates a report of current account balances.
     */
    
    import Banking_7.*;
    
    public class TestBanking_7 {
    
      public static void main(String[] args) {
        Bank     bank = Bank.getBank();
        Customer customer;
        Account  account;
    
        // Create two customers and their accounts
        bank.addCustomer("Jane", "Simms");
        customer = bank.getCustomer(0);
        customer.addAccount(new SavingAccount(500.00, 0.05),1);
        customer.addAccount(new CheckingAccount(200.00, 500.00),2);
        bank.addCustomer("Owen", "Bryant");
        customer = bank.getCustomer(1);
        customer.addAccount(new CheckingAccount(200.00),2);
    
        // Test the checking account of Jane Simms (with overdraft protection)
        customer = bank.getCustomer(0);
        account = customer.getCheckingAccount();
        System.out.println("Customer [" + customer.getLastName()
                   + ", " + customer.getFirstName() + "]"
                   + " has a checking balance of "
                   + account.getBalance()
                 + " with a 500.00 overdraft protection.");
        try {
          System.out.println("Checking Acct [Jane Simms] : withdraw 150.00");
          account.withdraw(150.00);
          System.out.println("Checking Acct [Jane Simms] : deposit 22.50");
          account.deposit(22.50);
          System.out.println("Checking Acct [Jane Simms] : withdraw 147.62");
          account.withdraw(147.62);
          System.out.println("Checking Acct [Jane Simms] : withdraw 470.00");
          account.withdraw(470.00);
        } catch (OverdraftException e1) {
          System.out.println("Exception: " + e1.getMessage()
                 + "   Deficit: " + e1.getDeficit());
        } finally {
          System.out.println("Customer [" + customer.getLastName()
                 + ", " + customer.getFirstName() + "]"
                 + " has a checking balance of "
                 + account.getBalance());
        }
        System.out.println();
    
        // Test the checking account of Owen Bryant (without overdraft protection)
        customer = bank.getCustomer(1);
        account = customer.getCheckingAccount();
        System.out.println("Customer [" + customer.getLastName()
                   + ", " + customer.getFirstName() + "]"
                   + " has a checking balance of "
                   + account.getBalance());
        try {
          System.out.println("Checking Acct [Owen Bryant] : withdraw 100.00");
          account.withdraw(100.00);
          System.out.println("Checking Acct [Owen Bryant] : deposit 25.00");
          account.deposit(25.00);
          System.out.println("Checking Acct [Owen Bryant] : withdraw 175.00");
          account.withdraw(175.00);
        } catch (OverdraftException e1) {
          System.out.println("Exception: " + e1.getMessage()
                 + "   Deficit: " + e1.getDeficit());
        } finally {
          System.out.println("Customer [" + customer.getLastName()
                 + ", " + customer.getFirstName() + "]"
                 + " has a checking balance of "
                 + account.getBalance());
        }
      }
    }
    View Code

    运行结果:

    Customer [Simms, Jane] has a checking balance of 200.0 with a 500.00 overdraft protection.
    Checking Acct [Jane Simms] : withdraw 150.00
    Checking Acct [Jane Simms] : deposit 22.50
    Checking Acct [Jane Simms] : withdraw 147.62
    Checking Acct [Jane Simms] : withdraw 470.00
    Insufficient funds for overdraft protection: 数额不足以弥补赤字,缺少资金或额度:45.120000000000005
    Exception: Insufficient funds for overdraft protection: 数额不足以弥补赤字,缺少资金或额度:   Deficit: 45.120000000000005
    Customer [Simms, Jane] has a checking balance of 0.0
    
    Customer [Bryant, Owen] has a checking balance of 200.0
    Checking Acct [Owen Bryant] : withdraw 100.00
    Checking Acct [Owen Bryant] : deposit 25.00
    Checking Acct [Owen Bryant] : withdraw 175.00
    no overdraft protection(没有透支保护):50.0
    Exception: no overdraft protection(没有透支保护):   Deficit: 50.0
    Customer [Bryant, Owen] has a checking balance of 125.0
    View Code
  • 相关阅读:
    centos8防火墙设置
    算法名词释义
    Amazon AWS S3 云服务存储命令备份
    记一次svn迁移
    从 0 开始学架构架构设计的历史背景3
    Ribbon负载均衡架构图
    高效能人士的七个习惯
    从 0 开始学架构前阿里 P9 技术专家的实战架构心法1
    为什么要学习数据结构和算法?
    如何高效学习
  • 原文地址:https://www.cnblogs.com/zhazhaacmer/p/9786216.html
Copyright © 2020-2023  润新知