zoukankan      html  css  js  c++  java
  • CS61b lab6

    主要加深了对Exception用法的理解,以前一直不太清楚。

    part1:在main中多加了一个try和catch块。

    part2 part3:

    将VirtualTeller和AccountData中的方法抛出异常并在BankApp中处理。

    最开始抛出两行异常,更改后只抛出一行。另外,part1中说的withdraw的小bug应该是输入负数时没有抛出错误信息吧。

    更改过的方法:

    public class BadTransactionException extends Exception {
    BadTransactionException(int i){
        super("Invalid amount number "+i);
    }
    }
    public void withdraw(int amt) throws BadTransactionException {
        if (amt <= balance&&amt>=0) {
          balance = balance - amt;
        } else {
          throw new BadTransactionException(amt);
        }
      }
    
      /**
       *  deposit() deposits "amt" dollars into this account.
     * @throws BadTransactionException 
       **/
      public void deposit(int amt) throws BadTransactionException {
        if (amt >= 0) {
          balance = balance + amt;
        } else {
          throw new BadTransactionException(amt);
        }
      }
    AccountData
    public void withdraw(int acct, int amount) throws BadAccountException, BadTransactionException {
       AccountData account = findAccount(acct);
       account.withdraw(amount);
      
      }
    
      /**
       *  deposit() deposits "amount" dollars into the bank account whose number is
       *  "acct".  Assumes that amount >= 0.  If "acct" is invalid, no action is
       *  taken.
       *  @param acct is an account number.
       *  @param amount an amount of money.
     * @throws BadAccountException 
     * @throws BadTransactionException 
       */
      public void deposit(int acct, int amount) throws BadAccountException, BadTransactionException {
        AccountData account = findAccount(acct);
          account.deposit(amount);
       }
    
      /**
       *  balanceInquiry() finds the balance on the account whose number is "acct".
       *  If "acct" is an invalid number, returns -1.
       *  @param acct an account number.
       *  @return the balance, or -1 if the account number is invalid.
     * @throws BadAccountException 
       */
      public int balanceInquiry(int acct) throws BadAccountException {
      
           AccountData account = findAccount(acct);
           return account.getBalance();
      }
      
    
      /**
       *  findAccount() gets the AccountData object associated with account number
       *  "acct".  If "acct" does not refer to a valid account, returns null.
       *  @param acct is an account number.
       *  @return the AccountData object associated with the account number.
     * @throws BadAccountException 
       */
      private AccountData findAccount(int acct) throws BadAccountException  {
          
        AccountData account = (AccountData) accounts.find(acct);
        if(account==null)
                throw new BadAccountException(acct);
        return account;
     
      }
    VirtualTeller
     1 private void doDeposit() throws IOException {
     2     // Get account number.
     3     int acctNumber = readInt("Enter account number: ");
     4     int amount = readInt("Enter amount to deposit: ");
     5 try
     6     {ATM.deposit(acctNumber, amount);
     7     System.out.println("New balance for #" + acctNumber + " is " +
     8                        ATM.balanceInquiry(acctNumber));}
     9 catch(Exception e){
    10     System.out.println(e);
    11     doDeposit();
    12 }
    13     
    14   }
    15 
    16   /**
    17    *  doWithdraw() prompts the user for an account number and tries
    18    *  to perform a withdrawal transaction from that account.
    19    *  @exception IOException if there are problems reading user input.
    20    */
    21   private void doWithdraw() throws IOException {
    22     // Get account number.
    23     int acctNumber = readInt("Enter account number: ");
    24     int amount = readInt("Enter amount to withdraw: ");
    25 try{
    26     ATM.withdraw(acctNumber, amount);
    27     System.out.println("New balance for #" + acctNumber + " is " +
    28                        ATM.balanceInquiry(acctNumber));
    29 }catch(Exception e){
    30     System.out.println(e);
    31     doWithdraw();
    32 }
    33   }
    34 
    35   /**
    36    *  doInquire() prompts the user for an account number, then attempts to
    37    *  discover and print that account's balance.
    38    *  @exception IOException if there are problems reading user input.
    39    */
    40   private void doInquire() throws IOException {
    41     int acctNumber = readInt("Enter account number: ");
    42 
    43    try{ System.out.println("Balance for #" + acctNumber + " is " +
    44                        ATM.balanceInquiry(acctNumber));}
    45    catch(Exception e){
    46        System.out.println(e);
    47        doInquire();
    48    }
    49   }
    BankApp

    最后的附加题:提示说只需要多加一行,应该就是在catch块的最后再加一个recursion就好了。

  • 相关阅读:
    R语言入门视频笔记--2--一些简单的命令
    Java 虚拟机内存优化
    Apollo 配置中心
    Apollo本地缓存文件
    SpringBoot 远程调试
    SpringCloud微服务架构下 部署在ECS上 403
    Nginx 常用命令
    阿里云数据库Redis版 ERR invalid password
    Apollo配置中心搭建
    Gateway 访问超时 返回504
  • 原文地址:https://www.cnblogs.com/lyz1995/p/7223057.html
Copyright © 2011-2022 走看看