Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Design

Similar presentations


Presentation on theme: "Object Oriented Design"— Presentation transcript:

1 Object Oriented Design
Chapter 14 Object Oriented Design ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

2 Horstmann/Java Essentials, 2/e
Figure 1 The Waterfall Model ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

3 Horstmann/Java Essentials, 2/e
Figure 2 The Waterfall Model with Iter- ations ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

4 Horstmann/Java Essentials, 2/e
Figure 3 A Software Development Model with Iterations and Limited Forward- Looking Steps ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

5 Horstmann/Java Essentials, 2/e
Figure 4 An Invoice ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

6 Horstmann/Java Essentials, 2/e
Figure 5 A CRC Card ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

7 Horstmann/Java Essentials, 2/e
Figure 6 UML Notation for Aggregation and Inheritance ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

8 Horstmann/Java Essentials, 2/e
Figure 7 UML Notation for Depen- dency ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

9 Horstmann/Java Essentials, 2/e
Figure 8 Low and High Coupling ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

10 Horstmann/Java Essentials, 2/e
Figure 9 UML Notation for Interfaces ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

11 Horstmann/Java Essentials, 2/e
Figure 10 Compartments for Attributes and Methods ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

12 Horstmann/Java Essentials, 2/e
Figure 11 The Relationships between the Invoice Classes ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

13 Horstmann/Java Essentials, 2/e
Figure 12 The Invoice Classes and Their Methods ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

14 Horstmann/Java Essentials, 2/e
Figure 13 Attributes and Methods of the Invoice Classes ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

15 Horstmann/Java Essentials, 2/e
Figure 14 The Class Documentation in HTML Format ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

16 Horstmann/Java Essentials, 2/e
Program InvoiceTest.java import java.util.Vector; public class InvoiceTest { public static void main(String[] args) { Customer sam = new Customer("Sam's Small Appliances", "100 Main Street\nAnytown, CA 98765"); Invoice samsInvoice = new Invoice(sam); samsInvoice.addItem(new Item("Toaster", 3, 29.95)); samsInvoice.addItem(new Item("Hair dryer", 1, 24.95)); samsInvoice.addItem(new Item("Car vacuum", 2, 19.99)); samsInvoice.print(); } ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

17 Horstmann/Java Essentials, 2/e
/** Describes an invoice for a set of purchased items. */ class Invoice { /** Constructs an invoice for a customer. @param c the customer public Invoice(Customer c) { theCustomer = c; items = new Vector(); } ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

18 Horstmann/Java Essentials, 2/e
/** Adds an item to this invoice. @param anItem the item to add */ public void addItem(Item anItem) { items.add(anItem); } Prints the invoice. public void print() { System.out.println(” I N V O I C E"); System.out.println(); theCustomer.print(); System.out.print("Item "); System.out.println("Qty Price Total"); ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

19 Horstmann/Java Essentials, 2/e
for (int i = 0; i < items.size(); i++) { Item nextItem = (Item)items.get(i); nextItem.print(); } System.out.println(); System.out.println("AMOUNT DUE: $” + getAmountDue()); /** Computes the total amount due. @return the amount due */ public double getAmountDue() { double amountDue = 0; amountDue = amountDue + nextItem.getTotalPrice(); return amountDue; ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

20 Horstmann/Java Essentials, 2/e
private Customer theCustomer; private Vector items; } /** Describes a customer with a mailing address. */ class Customer { /** Constructs a customer from the name and mailing address. @param aName the customer name @param anAddress the customer address */ public Customer(String aName, String anAddress) { name = aName; address = anAddress; } ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

21 Horstmann/Java Essentials, 2/e
/** Prints the customer name and address. */ public void print() { System.out.println(name); System.out.println(address); } private String name; private String address; Describes a quantity of an article to purchase and its price. ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

22 Horstmann/Java Essentials, 2/e
class Item { /** Constructs an item from the name, quantity, and price. @param aName the item name @param aQuantity the item quantity @param aPrice the unit price */ public Item(String aName, int aQuantity, double aPrice) { name = aName; quantity = aQuantity; price = aPrice; } /** Computes the total price of this item. @return the total price (unit price  quantity) ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

23 Horstmann/Java Essentials, 2/e
public double getTotalPrice() { return price * quantity; } /** Prints the item name, quantity, unit price and total. */ public void print() { final int COLUMN_WIDTH = 30; System.out.print(name); ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

24 Horstmann/Java Essentials, 2/e
// pad with spaces to fill column int pad = COLUMN_WIDTH - name.length(); for (int i = 1; i <= pad; i++) System.out.print(” "); System.out.println(quantity + ” ” + price + ” ” + getTotal()); } private String name; private int quantity; private double price; ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

25 Horstmann/Java Essentials, 2/e
Figure 15 The User Interface of the ATM ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

26 Horstmann/Java Essentials, 2/e
Figure 16 The States of the ATM ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

27 Horstmann/Java Essentials, 2/e
Figure 17 A UML Diagram for the ATM Classes ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

28 Horstmann/Java Essentials, 2/e
Program ATMSimulation.java import java.awt.Container; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowAdapter; import java.io.IOException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextArea; /** A simulation of an automatic teller machine */ ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

29 Horstmann/Java Essentials, 2/e
public class ATMSimulation { public static void main(String[] args) { ATM frame = new ATM(); frame.setTitle("First National Bank of Java"); frame.show(); } class ATM extends JFrame { /** Constructs the user interface of the ATM application. */ public ATM() { final int FRAME_WIDTH = 500; final int FRAME_HEIGHT = 200; setSize(FRAME_WIDTH, FRAME_HEIGHT); addWindowListener(new WindowCloser()); ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

30 Horstmann/Java Essentials, 2/e
// initialize bank and customers theBank = new Bank(); try { theBank.readCustomers("customers.txt"); } catch(IOException e) { JOptionPane.showMessageDialog (null, "Error opening accounts file."); // construct components pad = new KeyPad(); display = new JTextArea(4, 20); aButton = new JButton(” A "); aButton.addActionListener(new AButtonListener()); ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

31 Horstmann/Java Essentials, 2/e
bButton = new JButton(” B "); bButton.addActionListener(new BButtonListener()); cButton = new JButton(” C "); cButton.addActionListener(new CButtonListener()); // add components to content pane JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(3, 1)); buttonPanel.add(aButton); buttonPanel.add(bButton); buttonPanel.add(cButton); ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

32 Horstmann/Java Essentials, 2/e
Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); contentPane.add(pad); contentPane.add(display); contentPane.add(buttonPanel); setState(START_STATE); } /** Sets the current customer number to the keypad value and sets state to PIN */ public void setCustomerNumber() { customerNumber = (int)pad.getValue(); setState(PIN_STATE); ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

33 Horstmann/Java Essentials, 2/e
/** Gets PIN from keypad, finds customer in bank. If found, sets state to ACCOUNT, else to START. */ public void selectCustomer() { int pin = (int)pad.getValue(); currentCustomer = theBank.findCustomer(customerNumber, pin); if (currentCustomer == null) setState(START_STATE); else setState(ACCOUNT_STATE); } Sets current account to checking or savings. Sets state to TRANSACT. @param account one of Customer.CHECKING_ACCOUNT or Customer.SAVINGS_ACCOUNT ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

34 Horstmann/Java Essentials, 2/e
public void selectAccount(int account) { currentAccount = currentCustomer.getAccount(account); setState(TRANSACT_STATE); } /** Withdraws amount typed in keypad from current account. Sets state to ACCOUNT. */ public void withdraw() { currentAccount.withdraw(pad.getValue()); setState(ACCOUNT_STATE); } ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

35 Horstmann/Java Essentials, 2/e
/** Deposits amount typed in keypad to current account. Sets state to ACCOUNT. */ public void deposit() { currentAccount.deposit(pad.getValue()); setState(ACCOUNT_STATE); } Sets state and updates display message. @param state the next state public void setState(int newState) { state = newState; pad.clear(); if (state == START_STATE) display.setText("Enter customer number\nA = OK"); ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

36 Horstmann/Java Essentials, 2/e
else if (state == PIN_STATE) display.setText("Enter PIN\nA = OK"); else if (state == ACCOUNT_STATE) display.setText("Select Account\n" + "A = Checking\nB = Savings\nC = Exit"); else if (state == TRANSACT_STATE) display.setText("Balance = " + currentAccount.getBalance() + "\nEnter amount and select transaction\n" + "A = Withdraw\nB = Deposit\nC = Cancel"); } private int state; private int customerNumber; private Customer currentCustomer; private BankAccount currentAccount; private Bank theBank; ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

37 Horstmann/Java Essentials, 2/e
private JButton aButton; private JButton bButton; private JButton cButton; private KeyPad pad; private JTextArea display; private static final int START_STATE = 1; private static final int PIN_STATE = 2; private static final int ACCOUNT_STATE = 3; private static final int TRANSACT_STATE = 4; ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

38 Horstmann/Java Essentials, 2/e
private class AButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { if (state == START_STATE) setCustomerNumber(); else if (state == PIN_STATE) selectCustomer(); else if (state == ACCOUNT_STATE) selectAccount(Customer.CHECKING_ACCOUNT); else if (state == TRANSACT_STATE) withdraw(); } private class BButtonListener implements ActionListener { if (state == ACCOUNT_STATE) selectAccount(Customer.SAVINGS_ACCOUNT); deposit(); ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

39 Horstmann/Java Essentials, 2/e
private class CButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { if (state == ACCOUNT_STATE) setState(START_STATE); else if (state == TRANSACT_STATE) setState(ACCOUNT_STATE); } private class WindowCloser extends WindowAdapter { public void windowClosing(WindowEvent event) { System.exit(0); ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

40 Horstmann/Java Essentials, 2/e
Class KeyPad.java import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JTextField; /** A component that lets the user enter a number, using a button pad labeled with digits */ public class KeyPad extends JPanel { public KeyPad() { setLayout(new BorderLayout()); ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

41 Horstmann/Java Essentials, 2/e
// add display field display = new JTextField(); add(display, "North"); // make button panel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(4, 3)); // add digit buttons ActionListener listener = new DigitButtonListener(); addButton("7", listener); addButton("8", listener); addButton("9", listener); addButton("4", listener); addButton("5", listener); addButton("6", listener); addButton("1", listener); ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

42 Horstmann/Java Essentials, 2/e
addButton("2", listener); addButton("3", listener); addButton("0", listener); addButton(".", listener); // add clear entry button clearButton = new JButton("CE"); buttonPanel.add(clearButton); clearButton.addActionListener (new ClearButtonListener()); add(buttonPanel, "Center"); } ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

43 Horstmann/Java Essentials, 2/e
/** Gets the value that the user entered. @return the value in the text field of the keypad */ public double getValue() { return Double.parseDouble(display.getText()); } Clears the display. public void clear() { display.setText(""); Adds a button to the button panel @param label the button label @param listener the button listener ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

44 Horstmann/Java Essentials, 2/e
public void addButton(String label, ActionListener listener) { JButton button = new JButton(label); buttonPanel.add(button); button.addActionListener(listener); } private JPanel buttonPanel; private JButton clearButton; private JTextField display; private class DigitButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { // Get the button label // it is a digit or decimal point JButton source = (JButton)event.getSource(); String label = source.getText(); ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

45 Horstmann/Java Essentials, 2/e
// don’t add two decimal points if (label.equals(".") &&display.getText().indexOf(".") != -1) return; display.setText(display.getText() + label); } private class ClearButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { clear(); } ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

46 Horstmann/Java Essentials, 2/e
Class Bank.java import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.StringTokenizer; import java.util.Vector; public class Bank { /** Constructs a bank with no customers */ public Bank() { customers = new Vector(); } /** Reads the customer numbers and PINs and initializes the bank accounts. @param filename the name of the customer file ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

47 Horstmann/Java Essentials, 2/e
public void readCustomers(String filename) throws IOException { BufferedReader in = new BufferedReader (new FileReader(filename)); boolean done = false; while (!done) { String inputLine = in.readLine(); if (inputLine == null) done = true; else { StringTokenizer tokenizer = new StringTokenizer(inputLine); int number = Integer.parseInt(tokenizer.nextToken()); int pin Customer c = new Customer(number, pin); addCustomer(c); } ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

48 Horstmann/Java Essentials, 2/e
in.close(); } /** Adds a customer to the bank. @param c the customer to add */ public void addCustomer(Customer c) { customers.add(c); } ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

49 Horstmann/Java Essentials, 2/e
/** Finds a customer in the bank. @param aNumber a customer number @param aPin a personal identification number @return the matching customer, or null if no customer matches */ public Customer findCustomer(int aNumber, int aPin) { for (int i = 0; i < customers.size(); i++) { Customer c = (Customer)customers.get(i); if (c.match(aNumber, aPin)) return c; } return null; private Vector customers; ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

50 Horstmann/Java Essentials, 2/e
Class Customer.java /** A bank customer */ public class Customer { /** Constructs a customer with a given number and PIN. @param aNumber the customer number @param PIN the personal identification number public Customer(int aNumber, int aPin) { customerNumber = aNumber; pin = aPin; accounts = new BankAccount[2]; accounts[CHECKING_ACCOUNT] = new BankAccount(); accounts[SAVINGS_ACCOUNT] = new BankAccount(); } ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

51 Horstmann/Java Essentials, 2/e
/** Tests whether this customer matches a customer number and PIN. @param aNumber a customer number @param aPin a personal identification number @return true if the customer number and PIN match */ public boolean match(int aNumber, int aPin) { return customerNumber == aNumber && pin == aPin; } /** Gets an account of this customer. @param account one of Customer.CHECKING_ACCOUNT or Customer.SAVINGS_ACCOUNT @return the selected account, or null if account number not valid. ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

52 Horstmann/Java Essentials, 2/e
public BankAccount getAccount(int a) { if (0 <= a && a < accounts.length) return accounts[a]; else return null; } public static final int CHECKING_ACCOUNT = 0; public static final int SAVINGS_ACCOUNT = 1; private int customerNumber; private int pin; private BankAccount[] accounts; ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e

53 Horstmann/Java Essentials, 2/e
Figure 18 An Analog Clock ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e


Download ppt "Object Oriented Design"

Similar presentations


Ads by Google