lab6打卡

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lab6打卡相关的知识,希望对你有一定的参考价值。

PART1:因为 readline函数抛出了异常没有处理,所以在main里面合适的地方加上try catch即可

PART2:在合适的地方加上BadAccountException

PART3:自己写一个BadTransactionException,形式和BadAccountException是差不多的,然后在主程序上,需要判断正负问题的函数处加上这个exception。

技术分享
  1 package lab6;
  2 /*  VirtualTeller.java */
  3 
  4 import sortedlist.*;
  5 
  6 /**
  7  *  An implementation of a virtual automated teller machine.
  8  **/
  9 public class VirtualTeller {
 10   private static int nextAccountID = 100;
 11   private SortedList accounts;
 12 
 13   /**
 14    *  Constructs a new virtual teller.
 15    **/
 16   public VirtualTeller() {
 17     accounts = new SortedList();
 18   }
 19 
 20   /**
 21    *  openAccount() creates a new account for the customer "name".
 22    *  @param name the customer‘s name.
 23    *  @return the new account‘s ID number.
 24    **/
 25   public int openAccount(String name) {
 26     AccountData newData = new AccountData(name, nextAccountID);
 27     accounts.insert(newData);
 28 
 29     nextAccountID++;
 30     return newData.getNumber();
 31   }
 32 
 33   /**
 34    *  withdraw() withdraws "amount" dollars from the account whose number is
 35    *  "acct".  Assumes that amount >= 0.  If "acct" is invalid, no action is
 36    *  taken.
 37    *  @param acct is an account number.
 38    *  @param amount an amount of money.
 39    */
 40   public void withdraw(int acct, int amount)throws BadAccountException,BadTransactionException {
 41     AccountData account = findAccount(acct);
 42     if(amount<0) {
 43         throw new BadTransactionException(amount);
 44     }
 45     //if (account == null) {   // Didn‘t find the account.
 46     //  System.out.println("Error:  Couldn‘t find account number `" +
 47     //                     acct + "‘" );
 48     //} else {
 49       account.withdraw(amount);
 50     //}
 51                          
 52   }
 53 
 54   /**
 55    *  deposit() deposits "amount" dollars into the bank account whose number is
 56    *  "acct".  Assumes that amount >= 0.  If "acct" is invalid, no action is
 57    *  taken.
 58    *  @param acct is an account number.
 59    *  @param amount an amount of money.
 60    */
 61   public void deposit(int acct, int amount) throws BadAccountException,BadTransactionException {
 62     AccountData account = findAccount(acct);
 63     if(amount<0) {
 64         throw new BadTransactionException(amount);
 65     }
 66    // if (account == null) { 
 67    //  System.out.println("Error:  Couldn‘t find account number `" +
 68    //                     acct + "‘");
 69    // } else {
 70       account.deposit(amount);
 71    //}
 72   }
 73 
 74   /**
 75    *  balanceInquiry() finds the balance on the account whose number is "acct".
 76    *  If "acct" is an invalid number, returns -1.
 77    *  @param acct an account number.
 78    *  @return the balance, or -1 if the account number is invalid.
 79    */
 80   public int balanceInquiry(int acct)throws BadAccountException {
 81     AccountData account = findAccount(acct);
 82 
 83     if (account == null) {
 84     //  System.out.println("Error:  Couldn‘t find account number `" +
 85     //                    acct + "‘" );
 86       return -1;
 87     } else {
 88       return account.getBalance();
 89     }
 90   }
 91 
 92   /**
 93    *  findAccount() gets the AccountData object associated with account number
 94    *  "acct".  If "acct" does not refer to a valid account, returns null.
 95    *  @param acct is an account number.
 96    *  @return the AccountData object associated with the account number.
 97    */
 98   private AccountData findAccount(int acct) throws BadAccountException{
 99     AccountData account = (AccountData) accounts.find(acct);
100     if(account==null) {
101         throw new BadAccountException(acct);
102     }
103     return account;
104   }
105 }
VirtualTeller.java
技术分享
  1 package lab6;
  2 /* BankApp.java */
  3 
  4 import java.io.*;
  5 import sortedlist.*;
  6 
  7 /**
  8  *  A bank application.  Allows a user to create and manipulate 
  9  *  banking accounts, using an ATM that is shared by all banking applications.
 10  */
 11 public class BankApp {
 12   private BufferedReader bReader =
 13           new BufferedReader(new InputStreamReader(System.in));
 14   private VirtualTeller ATM = new VirtualTeller();
 15 
 16   public static void main(String[] args) {
 17     greeting();
 18     usage();
 19     BankApp bankApp = new BankApp();
 20 
 21     try {
 22         String command = bankApp.readLine("--> ");
 23         while (!command.equals("quit")) {
 24             try {
 25                 if (command.equals("open")) {
 26                     bankApp.open();
 27                 } else if (command.equals("deposit")) {
 28                     bankApp.doDeposit();
 29                 } else if (command.equals("withdraw")) {
 30                     bankApp.doWithdraw();
 31                 } else if (command.equals("inquire")) {
 32                     bankApp.doInquire();
 33                 } else {
 34                     System.err.println("Invalid command: " + command);
 35                     usage();
 36                 }
 37                 }catch(BadTransactionException e2) {
 38                     System.err.println(e2);
 39                 }catch(BadAccountException e1) {
 40                     System.err.println(e1);
 41                 }catch(IOException e) {
 42                 System.err.println(e);
 43                 }
 44             command = bankApp.readLine("--> ");
 45         }
 46     }catch(IOException e) {
 47             System.err.println(e);
 48         }
 49   }
 50   public BankApp() {
 51     // The field declarations have initializers;
 52     //   no initialization is needed here.
 53   }
 54 
 55   /**
 56    *  open() prompts the user to create an account and creates one in the ATM.
 57    *  @exception IOException if there are problems reading user input.
 58    */
 59   private void open() throws IOException {
 60     String name = readLine("Enter name: ");
 61     int newNum = ATM.openAccount(name);
 62 
 63     System.out.println(name + ", your new account number is: " + newNum);
 64     System.out.println("Thanks for opening an account with us!");
 65   }
 66 
 67  /**
 68   *  doDeposit() prompts the user for an account number and tries to perform a 
 69   *  deposit transaction on that account. 
 70   *  @exception IOException if there are problems reading user input.
 71   */
 72   private void doDeposit() throws IOException, BadAccountException,BadTransactionException {
 73     // Get account number.
 74     int acctNumber = readInt("Enter account number: ");
 75     int amount = readInt("Enter amount to deposit: ");
 76 
 77     ATM.deposit(acctNumber, amount);
 78     System.out.println("New balance for #" + acctNumber + " is " +
 79                        ATM.balanceInquiry(acctNumber));
 80   }
 81 
 82   /**
 83    *  doWithdraw() prompts the user for an account number and tries
 84    *  to perform a withdrawal transaction from that account.
 85    *  @exception IOException if there are problems reading user input.
 86    */
 87   private void doWithdraw() throws IOException, BadAccountException,BadTransactionException {
 88     // Get account number.
 89     int acctNumber = readInt("Enter account number: ");
 90     int amount = readInt("Enter amount to withdraw: ");
 91 
 92     ATM.withdraw(acctNumber, amount);
 93     System.out.println("New balance for #" + acctNumber + " is " +
 94                        ATM.balanceInquiry(acctNumber));
 95   }
 96 
 97   /**
 98    *  doInquire() prompts the user for an account number, then attempts to
 99    *  discover and print that account‘s balance.
100    *  @exception IOException if there are problems reading user input.
101    */
102   private void doInquire() throws IOException, BadAccountException {
103     int acctNumber = readInt("Enter account number: ");
104 
105     System.out.println("Balance for #" + acctNumber + " is " +
106                        ATM.balanceInquiry(acctNumber));
107   }
108 
109   /**
110    *  greeting() displays a greeting message on the screen.
111    */
112   private static void greeting() {
113     System.out.println("-------------------");
114     System.out.println("Welcome to the bank");
115     System.out.println("-------------------");
116     System.out.println();
117   }
118 
119   /**
120    *  usage() displays instructions on using the command line arguments.
121    */
122   private static void usage() {
123     System.out.println("Valid commands are: " +
124                        "open, deposit, withdraw, inquire, quit");
125   }
126 
127   /**
128    *  readLine() prints the given prompt and returns a string from the
129    *  input stream.
130    *  @param prompt is the string printed to prompt the user.
131    */
132   private String readLine(String prompt) throws IOException {
133     System.out.print(prompt);
134     System.out.flush();
135     return bReader.readLine();
136   }
137 
138   /**
139    *  readInt() returns an integer from the input stream after prompting
140    *  the user.
141    *  @param prompt is the string printed to prompt the user.
142    *  @return an int read from the user.
143    */
144   private int readInt(String prompt) throws IOException {
145     String text = readLine(prompt);
146     return Integer.valueOf(text).intValue();
147   }
148 }
BankApp.java

以上是关于lab6打卡的主要内容,如果未能解决你的问题,请参考以下文章

操作系统ucore lab6实验报告

HIT Software Construction Lab6引发出来对锁的问题的探究

MIT6.S081-Lab6 Lab Multithreading

software construction in Java Lab6

Lab6实验的一些思考

lab6:分析Linux内核创建一个新进程的过程