SYSTEM DESIGN - PowerPoint PPT Presentation

1 / 121
About This Presentation
Title:

SYSTEM DESIGN

Description:

... the use of CRC cards for class discovery ... Use CRC cards to find classes, responsibilities, and ... CRC Cards for Printing Invoice. Printing an Invoice ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 122
Provided by: facul56
Category:
Tags: design | system

less

Transcript and Presenter's Notes

Title: SYSTEM DESIGN


1
CHAPTER 16
  • SYSTEM DESIGN

2
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

3
Software Life Cycle
  • Encompasses all activities from initial analysis
    until obsolescence

4
Development Process
  • Analysis
  • Design
  • Implementation
  • Testing  
  • Deployment

5
Analysis
  • Decide what the project is suppose to do
  • Output requirements document

6
Design
  • Plan how to implement the system
  • Output
  • description of classes and methods
  • diagrams showing the relationships among the
    classes

7
Implementation
  • Write and compile the code
  • Output completed code

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

9
Deployment
  • Install the program and begin using it

10
The Waterfall Model
11
The Spiral Model
  • Describes an iterative process in which design
    and implementation are repeated

12
The Spiral Model
13
Extreme Programming
  • Development methodology that strives for
    simplicity by removing formal structure and
    focusing on best practices

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

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

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

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

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

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

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

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

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

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

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

25
Extreme Programming
  • Coding standards
  • Follow standards that emphasize self-documenting
    code

26
Object-Oriented Design
  • Discover classes
  • Determine responsibilities of each class
  • Describe the relationships among the classes

27
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

28
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

29
A CRC Card
30
Relationships Between Classes
  • Inheritance
  • Association
  • Dependency

31
"Is-a" Relationship
  • The "is-a" relationship is inheritance use
    extends
  • a circle is an ellipse
  • a car is a vehicle

32
"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

33
Uses Relationship
  • The "uses" relationship is dependency
  • an Applet uses a Graphics object

34
"is-a" and "has-a"Example
  • class Car extends Vehicle //inheritance "is-a"
  • ...
  • private Tire tires //association "has-a"

35
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

36
UML Notation for Inheritance and Association
37
UML Relationship Symbols
38
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

39
Printing an Invoice - Requirements
  • Print billing address, all line items, amount due
  • Line item contains description, unit price,
    quantity ordered, total price

40
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  

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

42
Printing an Invoice - CRC Cards
  • Classes after a process of elimination
  • Invoice
  • Address
  • Item
  • Product

43
CRC Cards for Printing Invoice
44
CRC Cards for Printing Invoice
45
CRC Cards for Printing Invoice
46
CRC Cards for Printing Invoice
47
Printing an Invoice - UML Diagrams
  • The relationship between Invoice classes

48
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

49
Method Documentation Invoice class
  • /
  • Describes an invoice for a set of purchased
    products.
  • /
  • class Invoice
  • /
  • Adds a charge for a product to this
    invoice.
  • _at_param aProduct the product that the
    customer ordered
  • _at_param quantity the quantity of the product
  • /
  • public void add(Product aProduct, int
    quantity)
  • /
  • Formats the invoice.
  • _at_return the formatted invoice

50
  • /
  • public String format()
  • /
  • Computes the total amount due.
  • _at_Return the amount due
  • /
  • public double getAmountDue()

51
Method Documentation - Item class
  • /
  • Describes a quantity an article to purchase
    and its price.
  • /
  • Class Item
  • /
  • Computes the total cost of this item.
  • _at_Return the total price
  • /
  • public double getTotalPrice()
  • /
  • Formats this item.
  • _at_Return a formatted string of this item
  • /

52
  • public String format()

53
Method Documentation - Product class
  • /
  • Describes a product with a description and a
    price
  • /
  • class Product
  • /
  • Gets the product description.
  • _at_Return the description
  • /
  • public String getDescription()
  • /
  • Gets the product price.
  • _at_Return the unit price
  • /
  • public double getPrice()

54
Method Documentation - Address class
  • /
  • Describes a mailing address.
  • /
  • Class Address
  • /
  • Formats the address.
  • _at_Return the address as a string with 3
    lines
  • /
  • public String format()

55
The Class Documentation in the HTML Format
56
Printing an Invoice - Implementation
  • The UML diagram will give instance variables
  • Look for associated classes
  • They yield instance variables

57
Implementation
  • Invoice is associated with Address and Item
  • It will have Address and Item instance variables

58
Implementation
  • An Invoice has one Address.
  • Declare one Address instance variable.
  • private Address billingAddress

59
Implementation
  • An Invoice has multiple Items.
  • Use an ArrayList to hold Items.
  • private ArrayList items

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

61
  • 18 samsInvoice.add(new Product("Car
    vacuum", 19.99), 2)
  • 19
  • 20 System.out.println(samsInvoice.format())
  • 21
  • 22

62
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 _at_param anAddress the billing address
  • 11 /
  • 12 public Invoice(Address anAddress)
  • 13
  • 14 items new ArrayList()
  • 15 billingAddress anAddress
  • 16
  • 17

63
  • 18 /
  • 19 Adds a charge for a product to this
    invoice.
  • 20 _at_param aProduct the product that the
    customer ordered
  • 21 _at_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 _at_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()

64
  • 38 for (int i 0 i lt 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 _at_return the amount due
  • 52 /
  • 53 public double getAmountDue()
  • 54
  • 55 double amountDue 0
  • 56 for (int i 0 i lt items.size() i)

65
  • 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

66
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 _at_param aProduct the product
  • 09 _at_param aQuantity the item quantity
  • 10 /
  • 11 public Item(Product aProduct, int
    aQuantity)
  • 12
  • 13 theProduct aProduct
  • 14 quantity aQuantity
  • 15
  • 16
  • 17 /

67
  • 18 Computes the total cost of this item.
  • 19 _at_return the total price
  • 20 /
  • 21 public double getTotalPrice()
  • 22
  • 23 return theProduct.getPrice()
    quantity
  • 24
  • 25
  • 26 /
  • 27 Formats this item.
  • 28 _at_return a formatted string of this item
  • 29 /
  • 30 public String format()
  • 31
  • 32 final int COLUMN_WIDTH 30
  • 33 String description theProduct.getDescr
    iption()
  • 34
  • 35 String r description
  • 36

68
  • 38
  • 39 int pad COLUMN_WIDTH -
    description.length()
  • 40 for (int i 1 i lt 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

69
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 _at_param aDescription the product
    description
  • 09 _at_param aPrice the product price
  • 10 /
  • 11 public Product(String aDescription, double
    aPrice)
  • 12
  • 13 description aDescription
  • 14 price aPrice
  • 15
  • 16
  • 17 /

70
  • 18 Gets the product description.
  • 19 _at_return the description
  • 20 /
  • 21 public String getDescription()
  • 22
  • 23 return description
  • 24
  • 25
  • 26 /
  • 27 Gets the product price.
  • 28 _at_return the unit price
  • 29 /
  • 30 public double getPrice()
  • 31
  • 32 return price
  • 33
  • 34
  • 35 private String description
  • 36 private double price

71
File Address.java
  • 01 /
  • 02 Describes a mailing address.
  • 03 /
  • 04 class Address
  • 05
  • 06 /
  • 07 Constructs a mailing address.
  • 08 _at_param aName the recipient name
  • 09 _at_param aStreet the street
  • 10 _at_param aCity the city
  • 11 _at_param aState the 2-letter state code
  • 12 _at_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

72
  • 19 city aCity
  • 20 state aState
  • 21 zip aZip
  • 22
  • 23
  • 24 /
  • 25 Formats the address.
  • 26 _at_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

73
An Automatic Teller Machine - Requirements
  • ATM has
  • Keypad
  • Display
  • Buttons A,B,C
  • The button function depends on the state of the
    machine.

74
Requirements
  • ATM is used by bank customers. A customer has a
  • Checking account
  • Savings account
  • Customer number
  • Pin

75
Requirements
  • At start up the customer is expected to
  • Enter customer number
  • Press the A button.
  • The display showsEnter Customer Number A OK

76
Requirements
  • The customer is expected to
  • Enter a PIN
  • Press A button.
  • The display showsEnter PINA OK

77
Requirements
  • Search for the customer number and PIN
  • If it matches a bank customer, proceed
  • Else return to start up screen

78
Requirements
  • If the customer is authorized
  • The display shows Select AccountA Checking
  • B SavingsC Exit

79
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

80
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.

81
User Interface of the Automatic Teller Machine
82
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

83
CRC Cards for Automatic Teller Machine
84
CRC Cards for Automatic Teller Machine
85
CRC Cards for Automatic Teller Machine
86
CRC Cards for Automatic Teller Machine
87
ATM States
  • START Enter customer ID
  • PIN Enter PIN
  • ACCOUNT Select account
  • TRANSACT Select transaction

88
State Diagram for ATM Class
89
The Relationships Between the ATM Classes
90
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

91
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
  • _at_param account one of CHECKING_ACCOUNT or
    SAVINGS_ACCOUNT
  • /
  • public void selectAccount(int account)

92
  • /
  • 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()
  • /

93
  • _at_param state the next state
  • /
  • public void setState(int newState)

94
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

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

96
Implementation for ATM class
  • Other instance variables needed
  • private int state
  • private Customer currentCustomer
  • private BankAccount currentAccount

97
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.EX
    IT_ON_CLOSE)
  • 13 frame.pack()
  • 14 frame.show()
  • 15
  • 16

98
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

99
  • 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

100
  • 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)

101
  • 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

102
  • 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(customerNumbe
    r, 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 _at_param account one of CHECKING_ACCOUNT
    or SAVINGS_ACCOUNT

103
  • 098 public void selectAccount(int account)
  • 099
  • 100 if (account CHECKING_ACCOUNT)
  • 101 currentAccount currentCustomer.get
    CheckingAccount()
  • 102 else
  • 103 currentAccount currentCustomer.get
    SavingsAccount()
  • 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

104
  • 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 _at_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")

105
  • 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()

106
  • 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

107
  • 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

108
  • 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

109
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 /

110
  • 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")

111
  • 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(ActionEv
    ent event)
  • 054
  • 055 display.setText("")
  • 056

112
  • 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 _at_param label the button label
  • 069 /
  • 070 private void addButton(final String
    label)
  • 071
  • 072 class DigitButtonListener implements
    ActionListener
  • 073
  • 074 public void actionPerformed(ActionEv
    ent event)
  • 075
  • 076

113
  • 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 _at_return the value in the text field of
    the keypad
  • 096 /

114
  • 098
  • 099 return Double.parseDouble(display.getTe
    xt())
  • 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

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

116
  • 18
  • 19
  • 20 /
  • 21 Reads the customer numbers and pins
  • 22 and initializes the bank accounts.
  • 23 _at_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

117
  • 38 new StringTokenizer(inputLine
    )
  • 39 int number
  • 40 Integer.parseInt(tokenizer.ne
    xtToken())
  • 41 int pin
  • 42 Integer.parseInt(tokenizer.ne
    xtToken())
  • 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 _at_param c the customer to add
  • 54 /
  • 55 public void addCustomer(Customer c)
  • 56

118
  • 58
  • 59
  • 60 /
  • 61 Finds a customer in the bank.
  • 62 _at_param aNumber a customer number
  • 63 _at_param aPin a personal identification
    number
  • 64 _at_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 lt 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

119
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 _at_param aNumber the customer number
  • 09 _at_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

120
  • 18
  • 19 /
  • 20 Tests if this customer matches a
    customer number
  • 21 and PIN.
  • 22 _at_param aNumber a customer number
  • 23 _at_param aPin a personal identification
    number
  • 24 _at_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
Write a Comment
User Comments (0)
About PowerShow.com