chapter 16
Download
Skip this Video
Download Presentation
CHAPTER 16

Loading in 2 Seconds...

play fullscreen
1 / 121

SYSTEM DESIGN - PowerPoint PPT Presentation


  • 74 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'SYSTEM DESIGN' - huong


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 16

CHAPTER 16

SYSTEM DESIGN

chapter goals
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
Software Life Cycle
  • Encompasses all activities from initial analysis until obsolescence
development process
Development Process
  • Analysis
  • Design
  • Implementation
  • Testing
  • Deployment
analysis
Analysis
  • Decide what the project is suppose to do
  • Output: requirements document
design
Design
  • Plan how to implement the system
  • Output:
    • description of classes and methods
    • diagrams showing the relationships among the classes
implementation
Implementation
  • Write and compile the code
  • Output: completed code
testing
Testing
  • Run tests to verify the program works correctly
  • Output: a report of the tests and their results
deployment
Deployment
  • Install the program and begin using it
the spiral model
The Spiral Model
  • Describes an iterative process in which design and implementation are repeated
extreme programming
Extreme Programming
  • Development methodology that strives for simplicity by removing formal structure and focusing on best practices
extreme programming1
Extreme Programming
  • Realistic planning
    • Customers make business decisions
    • Programmers make technical decisions
extreme programming2
Extreme Programming
  • Small releases
    • Release a useful system quickly
    • Release updates on a short cycle
extreme programming3
Extreme Programming
  • Metaphor
    • A shared story to explain the system
extreme programming4
Extreme Programming
  • Simplicity
    • Design as simply as possible instead of preparing for future complexities
extreme programming5
Extreme Programming
  • Testing
    • Programmers and customers write test cases
    • Test continuously
extreme programming6
Extreme Programming
  • Refactoring
    • Restructure the system continuously to improve code and eliminate duplication
extreme programming7
Extreme Programming
  • Pair programming
    • Two programmers write code on the same computer
extreme programming8
Extreme Programming
  • Collective ownership
    • All programmers change all code as needed
extreme programming9
Extreme Programming
  • Continuous integration
    • Build the entire system and test it whenever a task is complete
extreme programming10
Extreme Programming
  • 40-hour week
    • Don\'t cover up unrealistic schedules with heroic effort
extreme programming11
Extreme Programming
  • On-site customer
    • A customer is accessible to the programming team at all times
extreme programming12
Extreme Programming
  • Coding standards
    • Follow standards that emphasize self-documenting code
object oriented design
Object-Oriented Design
  • Discover classes
  • Determine responsibilities of each class
  • Describe the relationships among the classes
discovering 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
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
relationships between classes
Relationships Between Classes
  • Inheritance
  • Association
  • Dependency
is a relationship
"Is-a" Relationship
  • The "is-a" relationship is inheritance; use extends
    • a circle is an ellipse
    • a car is a vehicle
has a relationship
"Has-a" Relationship
  • The "has-a" relationship is association; use an instance variable
    • a tire has a circle as its boundary
    • a car has a set of tire
uses relationship
Uses Relationship
  • The "uses" relationship is dependency
    • an Applet uses a Graphics object
is a and has a example
"is-a" and "has-a"Example

class Car extends Vehicle //inheritance "is-a"

{

...

private Tire[] tires; //association "has-a"

}

association and dependency
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
five part development process
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
Printing an Invoice - Requirements
  • Print billing address, all line items, amount due
  • Line item contains description, unit price, quantity ordered, total price
sample invoice
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
Printing an Invoice - CRC Cards
  • Discover classes
  • Nouns are possible classes

Invoice Price

Address Quantity

Item Total

Product AmountDue

Description

printing an invoice crc cards1
Printing an Invoice - CRC Cards
  • Classes after a process of elimination

Invoice

Address

Item

Product

printing an invoice uml diagrams
Printing an Invoice - UML Diagrams
  • The relationship between Invoice classes
printing an invoice method documentation
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
Method Documentation – Invoiceclass

/**

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

slide50
*/

public String format()

{

}

/**

Computes the total amount due.

@Return the amount due

*/

public double getAmountDue()

{

}

}

method documentation item class
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

*/

method documentation product class
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
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()

{

}

}

printing an invoice implementation
Printing an Invoice - Implementation
  • The UML diagram will give instance variables
    • Look for associated classes
    • They yield instance variables
implementation1
Implementation
  • Invoice is associated with Address and Item
  • It will have Address and Item instance variables
implementation2
Implementation
  • An Invoice has one Address.
    • Declare one Address instance variable.

private Address billingAddress;

implementation3
Implementation
  • An Invoice has multiple Items.
    • Use an ArrayList to hold Items.

private ArrayList items

file invoicetest java
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);

slide61
18: samsInvoice.add(new Product("Car vacuum", 19.99), 2);

19:

20: System.out.println(samsInvoice.format());

21: }

22: }

file invoice java
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:

slide63
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";

slide64
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: {

slide65
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: }

file item java
File Item.java

01: /**

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: /**

slide67
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

slide68
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: }

file product java
File Product.java

01: /**

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: /**

slide70
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: }

file address java
File Address.java

01: /**

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;

slide72
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
An Automatic Teller Machine - Requirements
  • ATM has
      • Keypad
      • Display
      • Buttons A,B,C
      • The button function depends on the state of the machine.
requirements
Requirements
  • ATM is used by bank customers. A customer has a
    • Checking account
    • Savings account
    • Customer number
    • Pin
requirements1
Requirements
  • At start up the customer is expected to
    • Enter customer number
    • Press the A button.
    • The display shows:Enter Customer Number A = OK
requirements2
Requirements
  • The customer is expected to
    • Enter a PIN
    • Press A button.
    • The display shows:Enter PINA = OK
requirements3
Requirements
  • Search for the customer number and PIN
    • If it matches a bank customer, proceed
    • Else return to start up screen
requirements4
Requirements
  • If the customer is authorized
    • The display shows: Select AccountA = Checking

B = SavingsC = Exit

requirements5
Requirements
  • If the user presses C
    • The Atm returns to the start up screen
  • If the user presses A or B
    • The ATM remembers which
    • The display shows: Balance = balance in selected accountEnter amount and select transactionA = WithdrawB = DepositC = Cancel
requirements6
Requirements
  • If the user presses A or B
    • The value entered is withdrawn or deposited.
    • The ATM reverts to previous state.
  • If the user presses C
    • The ATM reverts to previous state.
an automatic teller machine crc
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

atm states
ATM States
  • START: Enter customer ID
  • PIN: Enter PIN
  • ACCOUNT: Select account
  • TRANSACT: Select transaction
an automatic teller machine method documentation
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
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)

{

slide92
}

/**

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.

slide93
@param state the next state

*/

public void setState(int newState)

{

}

}

an automatic teller machine implementation
An Automatic Teller Machine - Implementation
  • Start implementation with classes that don\'t depend on others
    • Keypad
    • BankAccount
  • Then implement Customer which depends only on BankAccount
  • This bottom-up approach allows you to test your classes individually
implementation for atm class
Implementation for ATM class
  • Associated classes in UML diagram give instance variables
    • private Bank theBank;
    • private KeyPad pad;
implementation for atm class1
Implementation for ATM class
  • Other instance variables needed
    • private int state;
    • private Customer currentCustomer;
    • private BankAccount currentAccount;
file atmsimulation java
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
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: {

slide99
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:

slide100
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);

slide101
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:

slide102
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: */

slide103
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: /**

slide104
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)

slide105
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)

slide106
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)

slide107
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;

slide108
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
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: */

slide110
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");

slide111
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: }

slide112
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

slide113
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()

slide114
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
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();

slide116
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

slide117
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);

slide118
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: }

file customer java
File Customer.java

01: /**

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: }

slide120
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;

slide121
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: }

ad