Docstoc

Printing Invoice

Document Sample
Printing Invoice Powered By Docstoc
					 CHAPTER 16

SYSTEM DESIGN
              CHAPTER GOALS
• To learn about the software life cycle
• To learn how to discover new classes and methods
• To understand the use of CRC cards for class discovery
• To be able to identify inheritance, aggregation, and
  dependence relationships between classes
• To master the use of UML class diagrams to describe
  class relationships
• To learn how to use object-oriented design to build
  complex programs
        Software Life Cycle

• Encompasses all activities from initial analysis
  until obsolescence
Development Process
• Analysis
• Design
• Implementation
• Testing
• Deployment
                Analysis

• Decide what the project is suppose to do
• Output: requirements document
                 Design

• Plan how to implement the system
• Output:
  o description of classes and methods
  o diagrams showing the relationships among the
    classes
          Implementation

• Write and compile the code
• Output: completed code
                  Testing

• Run tests to verify the program works correctly
• Output: a report of the tests and their results
              Deployment

• Install the program and begin using it
The Waterfall Model
         The Spiral Model

• Describes an iterative process in which
  design and implementation are repeated
The Spiral Model
      Extreme Programming

• Development methodology that strives for
  simplicity by removing formal structure and
  focusing on best practices
      Extreme Programming

• Realistic planning
  o Customers make business decisions
  o Programmers make technical decisions
      Extreme Programming

• Small releases
  o Release a useful system quickly
  o Release updates on a short cycle
      Extreme Programming

• Metaphor
  o A shared story to explain the system
      Extreme Programming

• Simplicity
  o Design as simply as possible instead of preparing
    for future complexities
      Extreme Programming

• Testing
  o Programmers and customers write test cases
  o Test continuously
      Extreme Programming

• Refactoring
  o Restructure the system continuously to improve
    code and eliminate duplication
      Extreme Programming

• Pair programming
  o Two programmers write code on the same
    computer
      Extreme Programming

• Collective ownership
  o All programmers change all code as needed
      Extreme Programming

• Continuous integration
  o Build the entire system and test it whenever a task
    is complete
      Extreme Programming

• 40-hour week
  o Don't cover up unrealistic schedules with heroic
    effort
      Extreme Programming

• On-site customer
  o A customer is accessible to the programming team
    at all times
      Extreme Programming

• Coding standards
  o Follow standards that emphasize self-documenting
    code
     Object-Oriented Design

• Discover classes
• Determine responsibilities of each class
• Describe the relationships among the classes
        Discovering Classes

• A class represents some useful concept
• Find classes by looking for nouns in the task
  description
• Define the behavior for each class
• Find methods by looking for verbs in the task
  description
               CRC Card

• Stands for classes, responsibilities,
  collaborators
• Use an index card for each class
• Pick the class that should be responsible for
  each method (verb)
• Write the responsibility onto the class card
• Indicate what other classes are needed to fulfill
  the responsibility - the collaborators
A CRC Card
Relationships Between Classes

     • Inheritance
     • Association
     • Dependency
          "Is-a" Relationship

• The "is-a" relationship is inheritance; use
  extends
  o a circle is an ellipse
  o a car is a vehicle
        "Has-a" Relationship

• The "has-a" relationship is association; use an
  instance variable
  o a tire has a circle as its boundary
  o a car has a set of tire
         Uses Relationship

• The "uses" relationship is dependency
  o an Applet uses a Graphics object
   "is-a" and "has-a"Example

class Car extends Vehicle //inheritance "is-a"
{
  ...
  private Tire[] tires;   //association "has-a"
}
  Association and Dependency

• One class is associated with another if you can
  navigate
  from its objects to objects of the other class
• One class depends on another if it comes into
  contact
  with the other class in some way.
• Association is a stronger form of dependency
UML Notation for Inheritance and
         Association
UML Relationship Symbols
Five-Part Development Process

• Gather requirements
• Use CRC cards to find classes, responsibilities,
  and collaborators
• Use UML diagrams to record class
  relationships
• Use javadoc to document method behavior
• Implement your program
Printing an Invoice - Requirements

• Print billing address, all line items, amount due
• Line item contains description, unit price,
  quantity ordered, total price
              Sample Invoice
INVOICE

Sam's Small
Appliances
100 Main Street
Anytown, CA
98765
Description       Price   Qty   Total
Toaster           29.95    3    89.85
Hair dryer        24.95    1    24.95
Car vacuum        19.99    2    39.98
Amount Due:
$154.78
Printing an Invoice - CRC Cards

   • Discover classes
   • Nouns are possible classes
Invoice          Price
Address          Quantity
Item             Total
Product          AmountDue
Description
Printing an Invoice - CRC Cards

  • Classes after a process of elimination

Invoice
Address
Item
Product
CRC Cards for Printing Invoice
CRC Cards for Printing Invoice
CRC Cards for Printing Invoice
CRC Cards for Printing Invoice
Printing an Invoice - UML Diagrams

 • The relationship between Invoice classes
  Printing an Invoice - Method
         Documentation
• Use javadoc documentation comments to
  record the behavior of the classes
• Leave the body of the methods blank
• Run the javadoc utility to obtain a formatted
  version of the documentation in HTML
  format
Method Documentation – Invoice class
 /**
   Describes an invoice for a set of purchased products.
 */
 class Invoice
 {
   /**
     Adds a charge for a product to this invoice.
     @param aProduct the product that the customer ordered
     @param quantity the quantity of the product
   */
   public void add(Product aProduct, int quantity)
   {
   }
   /**
     Formats the invoice.
     @return the formatted invoice
*/
 public String format()
 {
 }

    /**
      Computes the total amount due.
      @Return the amount due
    */
    public double getAmountDue()
    {
    }
}
Method Documentation - Item class
 /**
   Describes a quantity an article to purchase and its price.
 */
 Class Item
 {
   /**
     Computes the total cost of this item.
     @Return the total price
   */
   public double getTotalPrice()
   {
   }
   /**
     Formats this item.
     @Return a formatted string of this item
   */
public String format()
  {
  }
}
Method Documentation - Product class
 /**
   Describes a product with a description and a price
 */
 class Product
 {
   /**
     Gets the product description.
     @Return the description
   */
   public String getDescription()
   {
   }
   /**
     Gets the product price.
     @Return the unit price
   */
   public double getPrice()
   {
   }
 }
Method Documentation - Address class
 /**
   Describes a mailing address.
 */
 Class Address
 {
   /**
     Formats the address.
     @Return the address as a string with 3 lines
   */
   public String format()
   {
   }
 }
The Class Documentation in the
        HTML Format
Printing an Invoice - Implementation

  • The UML diagram will give instance variables
     o Look for associated classes
     o They yield instance variables
            Implementation
• Invoice is associated with Address and Item
• It will have Address and Item instance variables
         Implementation
• An Invoice has one Address.
  o Declare one Address instance variable.
     private Address billingAddress;
          Implementation
• An Invoice has multiple Items.
  o Use an ArrayList to hold Items.
     private ArrayList items
          File InvoiceTest.java
01: import java.util.Vector;
02:
03: /**
04: This program tests the invoice classes by printing
05: a sample invoice.
06: */
07: public class InvoiceTest
08: {
09: public static void main(String[] args)
10: {
11:     Address samsAddress
12:       = new Address("Sam's Small Appliances",
13:         "100 Main Street", "Anytown", "CA", "98765");
14:
15:     Invoice samsInvoice = new Invoice(samsAddress);
16:     samsInvoice.add(new Product("Toaster", 29.95), 3);
17:     samsInvoice.add(new Product("Hair dryer", 24.95), 1);
18:   samsInvoice.add(new Product("Car vacuum", 19.99), 2);
19:
20:   System.out.println(samsInvoice.format());
21: }
22: }
                    File Invoice.java
01: import java.util.ArrayList;
02:
03: /**
04: Describes an invoice for a set of purchased products.
05: */
06: class Invoice
07: {
08: /**
09:     Constructs an invoice.
10:     @param anAddress the billing address
11: */
12: public Invoice(Address anAddress)
13: {
14:     items = new ArrayList();
15:     billingAddress = anAddress;
16: }
17:
18:   /**
19:     Adds a charge for a product to this invoice.
20:     @param aProduct the product that the customer ordered
21:     @param quantity the quantity of the product
22:   */
23:   public void add(Product aProduct, int quantity)
24:   {
25:     Item anItem = new Item(aProduct, quantity);
26:     items.add(anItem);
27:   }
28:
29:   /**
30:     Formats the invoice.
31:     @return the formatted invoice
32:   */
33:   public String format()
34:   {
35:     String r = "            I N V O I C E\n\n"
36:       + billingAddress.format()
37:       + "\n\nDescription           Price Qty Total\n";
38:       for (int i = 0; i < items.size(); i++)
39:       {
40:         Item nextItem = (Item)items.get(i);
41:         r = r + nextItem.format() + "\n";
42:       }
43:
44:       r = r + "\nAMOUNT DUE: $" + getAmountDue();
45:
46:       return r;
47:   }
48:
49:   /**
50:     Computes the total amount due.
51:     @return the amount due
52:   */
53:   public double getAmountDue()
54:   {
55:     double amountDue = 0;
56:     for (int i = 0; i < items.size(); i++)
57:     {
58:     Item nextItem = (Item)items.get(i);
59:     amountDue = amountDue + nextItem.getTotalPrice();
60:   }
61:   return amountDue;
62: }
63:
64: private Address billingAddress;
65: private ArrayList items;
66: }
01: /**
                       File Item.java
02: Describes a quantity an article to purchase and its price.
03: */
04: class Item
05: {
06: /**
07:     Constructs an item from the product and quantity
08:     @param aProduct the product
09:     @param aQuantity the item quantity
10: */
11: public Item(Product aProduct, int aQuantity)
12: {
13:     theProduct = aProduct;
14:     quantity = aQuantity;
15: }
16:
17: /**
18:     Computes the total cost of this item.
19:     @return the total price
20:   */
21:   public double getTotalPrice()
22:   {
23:     return theProduct.getPrice() * quantity;
24:   }
25:
26:   /**
27:     Formats this item.
28:     @return a formatted string of this item
29:   */
30:   public String format()
31:   {
32:     final int COLUMN_WIDTH = 30;
33:     String description = theProduct.getDescription();
34:
35:    String r = description;
36:
37:    // pad with spaces to fill column
38:
39:   int pad = COLUMN_WIDTH - description.length();
40:   for (int i = 1; i <= pad; i++)
41:     r = r + " ";
42:
43:   r = r + theProduct.getPrice()
44:     + " " + quantity
45:     + " " + getTotalPrice();
46:
47:   return r;
48: }
49:
50: private int quantity;
51: private Product theProduct;
52: }
01: /**
                   File Product.java
02: Describes a product with a description and a price
03: */
04: class Product
05: {
06: /**
07:     Constructs a product from a description and a price.
08:     @param aDescription the product description
09:     @param aPrice the product price
10: */
11: public Product(String aDescription, double aPrice)
12: {
13:     description = aDescription;
14:     price = aPrice;
15: }
16:
17: /**
18:   Gets the product description.
19:   @return the description
20: */
21: public String getDescription()
22: {
23:   return description;
24: }
25:
26: /**
27:   Gets the product price.
28:   @return the unit price
29: */
30: public double getPrice()
31: {
32:   return price;
33: }
34:
35: private String description;
36: private double price;
37: }
01: /**
                 File Address.java
02: Describes a mailing address.
03: */
04: class Address
05: {
06: /**
07:     Constructs a mailing address.
08:     @param aName the recipient name
09:     @param aStreet the street
10:     @param aCity the city
11:     @param aState the 2-letter state code
12:     @param aZip the ZIP postal code
13: */
14: public Address(String aName, String aStreet,
15:     String aCity, String aState, String aZip)
16: {
17:     name = aName;
18:     street = aStreet;
19:   city = aCity;
20:   state = aState;
21:   zip = aZip;
22: }
23:
24: /**
25:   Formats the address.
26:   @return the address as a string with 3 lines
27: */
28: public String format()
29: {
30:   return name + "\n" + street + "\n"
31:     + city + ", " + state + " " + zip;
32: }
33:
34: private String name;
35: private String street;
36: private String city;
37: private String state;
38: private String zip;
39: }
An Automatic Teller Machine -
       Requirements
• ATM has
    o   Keypad
    o   Display
    o   Buttons A,B,C
    o   The button function depends on the state
        of the machine.
        Requirements
• ATM is used by bank customers. A
  customer has a
   o Checking account
   o Savings account
   o Customer number
   o Pin
         Requirements

• At start up the customer is
  expected to
  o Enter customer number
  o Press the A button.
  o The display shows:
   Enter Customer Number
   A = OK
        Requirements

• The customer is expected to
  o Enter a PIN
  o Press A button.
  o The display shows:
   Enter PIN
   A = OK
          Requirements
• Search for the customer number and
  PIN
   o If it matches a bank customer, proceed
   o Else return to start up screen
          Requirements
• If the customer is authorized
  o The display shows:
    Select Account
    A = Checking
   B = Savings
    C = Exit
           Requirements
• If the user presses C
  o The Atm returns to the start up screen
• If the user presses A or B
  o The ATM remembers which
  o The display shows:
    Balance = balance in selected account
    Enter amount and select transaction
    A = Withdraw
    B = Deposit
    C = Cancel
           Requirements

• If the user presses A or B
  o The value entered is withdrawn or deposited.
  o The ATM reverts to previous state.
• If the user presses C
  • The ATM reverts to previous state.
User Interface of the Automatic
        Teller Machine
An Automatic Teller Machine -
           CRC
• Nouns are possible classes

ATM                             Bank account
User - same as Customer         Checking account
Keypad                          Savings account
Display - use JTextArea         Customer
Display message - use Strings   Customer number
Button - use JButton            PIN
State                           Bank
CRC Cards for Automatic Teller
          Machine
CRC Cards for Automatic Teller
          Machine
CRC Cards for Automatic Teller
          Machine
CRC Cards for Automatic Teller
          Machine
          ATM States

1.   START: Enter customer ID
2.   PIN: Enter PIN
3.   ACCOUNT: Select account
4.   TRANSACT: Select transaction
State Diagram for ATM Class
The Relationships Between the
        ATM Classes
An Automatic Teller Machine -
   Method Documentation
• Use javadoc documentation
  comments to record the behavior of
  the classes
• Leave the body of the methods
  blank
• Run the javadoc utility to obtain a
  formatted version of the
  documentation in HTML format
Method Documentation ATM Class
public class ATM
{
    /**
      Gets PIN from keypad, finds customer in bank.
      If found sets state to ACCOUNT, else to START.
    */
    public void selectCustomer()
    {
    }
    /**
      Sets current account to checking or savings. Sets
      state to TRANSACT
      @param account one of CHECKING_ACCOUNT or SAVINGS_ACCOUNT
    */
    public void selectAccount(int account)
    {
  }

/**
   Withdraws amount typed in keypad from current account.
   Sets state to ACCOUNT.
 */
 public void withdraw()
 {
 }

 /**
   Deposits amount typed in keypad to current account.
   Sets state to ACCOUNT.
 */
 Public void deposit()
 {
 }

 /**
   Sets state and updates display message.
      @param state the next state
    */
    public void setState(int newState)
    {
    }
}
An Automatic Teller Machine -
      Implementation
• Start implementation with classes that
  don't depend on others
  o Keypad
  o BankAccount
• Then implement Customer which
  depends only on BankAccount
• This bottom-up approach allows you to
  test your classes individually
Implementation for ATM class

• Associated classes in UML
  diagram give instance variables
  o private Bank theBank;
  o private KeyPad pad;
Implementation for ATM class

• Other instance variables needed
  o private int state;
  o private Customer currentCustomer;
  o private BankAccount
    currentAccount;
       File ATMSimulation.java
01: import javax.swing.JFrame;
02:
03: /**
04: A simulation of an automatic teller machine
05: */
06: public class ATMSimulation
07: {
08: public static void main(String[] args)
09: {
10:     JFrame frame = new ATM();
11:     frame.setTitle("First National Bank of Java");
12:     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
13:     frame.pack();
14:     frame.show();
15: }
16: }
                     File ATM.java
001: import java.awt.Container;
002: import java.awt.FlowLayout;
003: import java.awt.GridLayout;
004: import java.awt.event.ActionEvent;
005: import java.awt.event.ActionListener;
006: import java.io.IOException;
007: import javax.swing.JButton;
008: import javax.swing.JFrame;
009: import javax.swing.JOptionPane;
010: import javax.swing.JPanel;
011: import javax.swing.JTextArea;
012:
013: /**
014: A frame displaying the components of an ATM
015: */
016: class ATM extends JFrame
017: {
018:   /**
019:     Constructs the user interface of the ATM application.
020:   */
021:   public ATM()
022:   {
023:     // initialize bank and customers
024:
025:    theBank = new Bank();
026:    try
027:    {
028:      theBank.readCustomers("customers.txt");
029:    }
030:    catch(IOException e)
031:    {
032:      JOptionPane.showMessageDialog(null,
033:        "Error opening accounts file.");
034:    }
035:
036:    // construct components
037:
038:   pad = new KeyPad();
039:
040:   display = new JTextArea(4, 20);
041:
042:   aButton = new JButton(" A ");
043:   aButton.addActionListener(new AButtonListener());
044:
045:   bButton = new JButton(" B ");
046:   bButton.addActionListener(new BButtonListener());
047:
048:   cButton = new JButton(" C ");
049:   cButton.addActionListener(new CButtonListener());
050:
051:   // add components to content pane
052:
053:   JPanel buttonPanel = new JPanel();
054:   buttonPanel.setLayout(new GridLayout(3, 1));
055:   buttonPanel.add(aButton);
056:   buttonPanel.add(bButton);
057:   buttonPanel.add(cButton);
058:
059:       Container contentPane = getContentPane();
060:       contentPane.setLayout(new FlowLayout());
061:       contentPane.add(pad);
062:       contentPane.add(display);
063:       contentPane.add(buttonPanel);
064:
065:       setState(START_STATE);
066:   }
067:
068:   /**
069:     Sets the current customer number to the keypad value
070:     and sets state to PIN.
071:   */
072:   public void setCustomerNumber()
073:   {
074:     customerNumber = (int)pad.getValue();
075:     setState(PIN_STATE);
076:   }
077:
078:   /**
079:     Gets PIN from keypad, finds customer in bank.
080:     If found sets state to ACCOUNT, else to START.
081:   */
082:   public void selectCustomer()
083:   {
084:     int pin = (int)pad.getValue();
085:     currentCustomer
086:        = theBank.findCustomer(customerNumber, pin);
087:     if (currentCustomer == null)
088:        setState(START_STATE);
089:     else
090:        setState(ACCOUNT_STATE);
091:   }
092:
093:   /**
094:     Sets current account to checking or savings. Sets
095:     state to TRANSACT
096:     @param account one of CHECKING_ACCOUNT or SAVINGS_ACCOUNT
097:   */
098:   public void selectAccount(int account)
099:   {
100:     if (account == CHECKING_ACCOUNT)
101:        currentAccount = currentCustomer.getCheckingAccount();
102:     else
103:        currentAccount = currentCustomer.getSavingsAccount();
104:     setState(TRANSACT_STATE);
105:   }
106:
107:   /**
108:     Withdraws amount typed in keypad from current account.
109:     Sets state to ACCOUNT.
110:   */
111:   public void withdraw()
112:   {
113:     currentAccount.withdraw(pad.getValue());
114:     setState(ACCOUNT_STATE);
115:   }
116:
117:   /**
118:     Deposits amount typed in keypad to current account.
119:     Sets state to ACCOUNT.
120:   */
121:   public void deposit()
122:   {
123:     currentAccount.deposit(pad.getValue());
124:     setState(ACCOUNT_STATE);
125:   }
126:
127:   /**
128:     Sets state and updates display message.
129:     @param state the next state
130:   */
131:   public void setState(int newState)
132:   {
133:     state = newState;
134:     pad.clear();
135:     if (state == START_STATE)
136:        display.setText("Enter customer number\nA = OK");
137:     else if (state == PIN_STATE)
138:     display.setText("Enter PIN\nA = OK");
139:   else if (state == ACCOUNT_STATE)
140:     display.setText("Select Account\n"
141:       + "A = Checking\nB = Savings\nC = Exit");
142:   else if (state == TRANSACT_STATE)
143:     display.setText("Balance = "
144:        + currentAccount.getBalance()
145:        + "\nEnter amount and select transaction\n"
146:        + "A = Withdraw\nB = Deposit\nC = Cancel");
147: }
148:
149: private class AButtonListener implements ActionListener
150: {
151:   public void actionPerformed(ActionEvent event)
152:   {
153:     if (state == START_STATE)
154:        setCustomerNumber();
155:     else if (state == PIN_STATE)
156:        selectCustomer();
157:     else if (state == ACCOUNT_STATE)
158:             selectAccount(CHECKING_ACCOUNT);
159:           else if (state == TRANSACT_STATE)
160:             withdraw();
161:       }
162:   }
163:
164:   private class BButtonListener implements ActionListener
165:   {
166:     public void actionPerformed(ActionEvent event)
167:     {
168:       if (state == ACCOUNT_STATE)
169:          selectAccount(SAVINGS_ACCOUNT);
170:       else if (state == TRANSACT_STATE)
171:          deposit();
172:     }
173:   }
174:
175:   private class CButtonListener implements ActionListener
176:   {
177:     public void actionPerformed(ActionEvent event)
178:       {
179:           if (state == ACCOUNT_STATE)
180:              setState(START_STATE);
181:           else if (state == TRANSACT_STATE)
182:              setState(ACCOUNT_STATE);
183:       }
184:   }
185:
186:   private int state;
187:   private int customerNumber;
188:   private Customer currentCustomer;
189:   private BankAccount currentAccount;
190:   private Bank theBank;
191:
192:   private JButton aButton;
193:   private JButton bButton;
194:   private JButton cButton;
195:
196:   private KeyPad pad;
197:   private JTextArea display;
198:
199: private static final int START_STATE = 1;
200: private static final int PIN_STATE = 2;
201: private static final int ACCOUNT_STATE = 3;
202: private static final int TRANSACT_STATE = 4;
203:
204: private static final int CHECKING_ACCOUNT = 1;
205: private static final int SAVINGS_ACCOUNT = 2;
206: }
                  File KeyPad.java
001: import java.awt.BorderLayout;
002: import java.awt.GridLayout;
003: import java.awt.event.ActionEvent;
004: import java.awt.event.ActionListener;
005: import javax.swing.JButton;
006: import javax.swing.JPanel;
007: import javax.swing.JTextField;
008:
009: /**
010: A component that lets the user enter a number, using
011: a button pad labeled with digits
012: */
013: public class KeyPad extends JPanel
014: {
015: /**
016:     Constructs the keypad panel.
017: */
018: public KeyPad()
019: {
020:   setLayout(new BorderLayout());
021:
022:   // add display field
023:
024:   display = new JTextField();
025:   add(display, "North");
026:
027:   // make button panel
028:
029:   buttonPanel = new JPanel();
030:   buttonPanel.setLayout(new GridLayout(4, 3));
031:
032:   // add digit buttons
033:
034:   addButton("7");
035:   addButton("8");
036:   addButton("9");
037:   addButton("4");
038:   addButton("5");
039:   addButton("6");
040:   addButton("1");
041:   addButton("2");
042:   addButton("3");
043:   addButton("0");
044:   addButton(".");
045:
046:   // add clear entry button
047:
048:   clearButton = new JButton("CE");
049:   buttonPanel.add(clearButton);
050:
051:   class ClearButtonListener implements ActionListener
052:   {
053:     public void actionPerformed(ActionEvent event)
054:     {
055:       display.setText("");
056:     }
057:   }
058:       ActionListener listener = new ClearButtonListener();
059:
060:       clearButton.addActionListener(new
061:         ClearButtonListener());
062:
063:       add(buttonPanel, "Center");
064:   }
065:
066:   /**
067:     Adds a button to the button panel
068:     @param label the button label
069:   */
070:   private void addButton(final String label)
071:   {
072:     class DigitButtonListener implements ActionListener
073:     {
074:       public void actionPerformed(ActionEvent event)
075:       {
076:
077:          // don't add two decimal points
078:               if (label.equals(".")
079:                  && display.getText().indexOf(".") != -1)
080:                  return;
081:
082:               // append label text to button
083:               display.setText(display.getText() + label);
084:           }
085:       }
086:
087:       JButton button = new JButton(label);
088:       buttonPanel.add(button);
089:       ActionListener listener = new DigitButtonListener();
090:       button.addActionListener(listener);
091:   }
092:
093:   /**
094:     Gets the value that the user entered.
095:     @return the value in the text field of the keypad
096:   */
097:   public double getValue()
098: {
099:   return Double.parseDouble(display.getText());
100: }
101:
102: /**
103:   Clears the dislay.
104: */
105: public void clear()
106: {
107:   display.setText("");
108: }
109:
110: private JPanel buttonPanel;
111: private JButton clearButton;
112: private JTextField display;
113: }
                      File Bank.java
01: import java.io.BufferedReader;
02: import java.io.FileReader;
03: import java.io.IOException;
04: import java.util.ArrayList;
05: import java.util.StringTokenizer;
06:
07: /**
08: A bank contains customers with bank accounts.
09: */
10: public class Bank
11: {
12: /**
13:     Constructs a bank with no customers.
14: */
15: public Bank()
16: {
17:     customers = new ArrayList();
18:   }
19:
20:   /**
21:     Reads the customer numbers and pins
22:     and initializes the bank accounts.
23:     @param filename the name of the customer file
24:   */
25:   public void readCustomers(String filename)
26:     throws IOException
27:   {
28:     BufferedReader in = new BufferedReader
29:       (new FileReader(filename));
30:     boolean done = false;
31:     while (!done)
32:     {
33:       String inputLine = in.readLine();
34:       if (inputLine == null) done = true;
35:       else
36:       {
37:          StringTokenizer tokenizer
38:               = new StringTokenizer(inputLine);
39:             int number
40:               = Integer.parseInt(tokenizer.nextToken());
41:             int pin
42:               = Integer.parseInt(tokenizer.nextToken());
43:
44:             Customer c = new Customer(number, pin);
45:             addCustomer(c);
46:         }
47:       }
48:       in.close();
49:   }
50:
51:   /**
52:     Adds a customer to the bank.
53:     @param c the customer to add
54:   */
55:   public void addCustomer(Customer c)
56:   {
57:     customers.add(c);
58: }
59:
60: /**
61:   Finds a customer in the bank.
62:   @param aNumber a customer number
63:   @param aPin a personal identification number
64:   @return the matching customer, or null if no customer
65:   matches
66: */
67: public Customer findCustomer(int aNumber, int aPin)
68: {
69:   for (int i = 0; i < customers.size(); i++)
70:   {
71:     Customer c = (Customer)customers.get(i);
72:     if (c.match(aNumber, aPin))
73:        return c;
74:   }
75:   return null;
76: }
77:
78: private ArrayList customers;
79: }
01: /**
               File Customer.java
02: A bank customer with a checking and savings account.
03: */
04: public class Customer
05: {
06: /**
07:     Constructs a customer with a given number and PIN.
08:     @param aNumber the customer number
09:     @param PIN the personal identification number
10: */
11: public Customer(int aNumber, int aPin)
12: {
13:     customerNumber = aNumber;
14:     pin = aPin;
15:     checkingAccount = new BankAccount();
16:     savingsAccount = new BankAccount();
17: }
18:
19:   /**
20:     Tests if this customer matches a customer number
21:     and PIN.
22:     @param aNumber a customer number
23:     @param aPin a personal identification number
24:     @return true if the customer number and PIN match
25:   */
26:   public boolean match(int aNumber, int aPin)
27:   {
28:     return customerNumber == aNumber && pin == aPin;
29:   }
30:
31:   /**
32:     Gets the checking account of this customer.
33:     @return the checking account
34:   */
35:   public BankAccount getCheckingAccount()
36:   {
37:     return checkingAccount;
38: }
39:
40: /**
41:   Gets the savings account of this customer.
42:   @return the checking account
43: */
44: public BankAccount getSavingsAccount()
45: {
46:   return savingsAccount;
47: }
48:
49: private int customerNumber;
50: private int pin;
51: private BankAccount checkingAccount;
52: private BankAccount savingsAccount;
53: }

				
DOCUMENT INFO
Description: Printing Invoice document sample