Top Banner
1 1992-2007 Pearson Education, Inc. All rights rese Case Studies
94

1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

Jan 01, 2016

Download

Documents

Alexina Daniels
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

1

1992-2007 Pearson Education, Inc. All rights reserved.

Case Studies

Page 2: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

2

1992-2007 Pearson Education, Inc. All rights reserved.

Cases Covered

Design methodology Simple aritmetic opperations

– boundary-control– Without database connection

Pizza order– boundary – control – entity– Without database connection

Human resource monitoring– boundary – control – entity– With database conection

ATM – boundary – control – entity– With database conection

Page 3: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

3

1992-2007 Pearson Education, Inc. All rights reserved.

Design Methodology

Three classes– Bounary – get information from the user and present

information

– Entity – internal representations of entities- Emloyees

- Students

- Accounts…

– Control – execute main opperaiions - All other classes comunicate with control class

- Sent messages get permisions

Page 4: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

4

1992-2007 Pearson Education, Inc. All rights reserved.

Relationb etween classes

Boundary

frames

Boundary

database

Entity

contro

Entity

Page 5: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

5

1992-2007 Pearson Education, Inc. All rights reserved.

Boundary Classes

Boundary classes get information from the user or external sources such as databeses

Or present information to external sources – Users, databases…

GUIs– Form Frames– Insert components on frames– Register events– When an event occurs – invoke relevant methods of handler objects

such as- actionPerformed, itemStateChanged

– invokes control classes- Sent infromation in components to control class- Let control class get the information from components

Page 6: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

6

1992-2007 Pearson Education, Inc. All rights reserved.

Boundary Classes

The control class process the information– Makes some calculations– Communicates with other entity or boundary classes– Register the changes in entities or store in databases– Finally send back information to the GUIs

Examples:– Student registration: when a corse is seleced after the

actionEvent - the list of selected courses are send to the control class - Or the control class is invoked to get the selected courses

from JLists

Page 7: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

7

1992-2007 Pearson Education, Inc. All rights reserved.

Boundary Classes

Depositing money– When an amount of meney to be deposited is entered into a

JTextField

– And an deposit button is pressed – generate an actionEvent

– The actionPerformed method of the event handler

– Send the money together with the deposit mesage to the contol

– Or invokes the control class that a deposiing money shold be processed so that the control class get the money depositted from the relevant JTextField

Page 8: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

8

1992-2007 Pearson Education, Inc. All rights reserved.

Control Class

Get and send infromation to boundry classes Communicate with entity classes

– Objects or list of objects

internal processing of information Example

– When a request comes about withdrawing money from an account

- Get the infromation – amount of money, account no, userID from the GUI boundary class

- Get infromation from the relevent account or user status – database connection

- Determine whether withdraw is possible – process information

Page 9: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

9

1992-2007 Pearson Education, Inc. All rights reserved.

Entity Classes

Internal representation of entities such as – Employee

– Student

– Account

In some applications list or ArrayList of objects are hold in memory

Mirror image of database Otherwise java is a database manipulation program

– Get information from the user send it to databse or visa versa

Page 10: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

10

1992-2007 Pearson Education, Inc. All rights reserved.

Example: taking square root of a real number

Get a real mumber from the user, take its square root and display the results in a label

Control object– Creats the bounday object

- Boundary class communicates with user When the user enters a number to the text field and press

the square rootbutton – The contrl class is invoked

Control object gets the numer in the textField prform processing by taking the square root

and send the result back to the bounday object to be displayed in a label

Page 11: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

11

1992-2007 Pearson Education, Inc. All rights reserved.

Output of program

Page 12: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

12

1992-2007 Pearson Education, Inc. All rights reserved.

Test class

import javax.swing.*;import java.awt.*;import java.awt.event.*;public class BoundaryControl { public static void main(String args[]) {

ControlClass control = new ControlClass();

} // end method main} // end class DoundaryControl

Page 13: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

13

1992-2007 Pearson Education, Inc. All rights reserved.

The Control class

class ControlClass { JFrameBoundary boundary; double value;

public ControlClass() {boundary = new JFrameBoundary(this);

} // end constructor

public void buttonPressed() {

value = boundary.getValue();value = Math.sqrt(value) ;boundary.setResult(value);

} // end method buttonPressed} // end class ControlClass

Page 14: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

14

1992-2007 Pearson Education, Inc. All rights reserved.

The Boundary class

class JFrameBoundary extends JFrame {

JTextField enterValue;JLabel result;JButton squareRoot;ControlClass controlObj;

public JFrameBounary(ControlClass ctrlObj) {controlObj = ctrlObj;setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(400, 200);

Page 15: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

15

1992-2007 Pearson Education, Inc. All rights reserved.

The boundary class cont.

setLayout( new FlowLayout());enterValue = new JTextField(10);result = new JLabel(“”);squareRoot = new JButton(“Square Root");

add(enterValue);add(result);add(squareRoot);

Handler handler = new Handler();squareRoot.addActionListener(handler);setVisible(true);

} // end constructor

Page 16: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

16

1992-2007 Pearson Education, Inc. All rights reserved.

İnner class Handler

private class Handler implements ActionListener { public void actionPerformed(ActionEvent e) { controlObj.buttonPressed(); } // end method actionPerformed} // end inner class Handler

public double getValue() { return Double.parseDouble(enterValue.getText());} // end method getValue

public void setResult(double x) { String s = String.format(“square root: %10.3f”,x); result.setText(s);} // end method setResult

} // end class

Page 17: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

17

1992-2007 Pearson Education, Inc. All rights reserved.

Notes

Boundary object calls control class so the control object has to be registered to the badudaryobjec

– When a boundary is created the current control object is sent to the xFraem0 object as well so that the xFrame0 object also knows the control object.

Page 18: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

18

1992-2007 Pearson Education, Inc. All rights reserved.

Another Approach

class ControlClass { JFrameBoundary boundary; double value;

public ControlClass() { boundary = new JFrameBoundary(this); } // end constructor

public void getValue(double x) {

value = x; value = Math.sqrt(value) ; boundary.setResult(value);

} // end method buttonPressed} // end class ControlClass

Page 19: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

19

1992-2007 Pearson Education, Inc. All rights reserved.

The Boundary class

class JFrameBoundary extends JFrame {

JTextField enterValue;JLabel result;JButton squareRoot;ControlClass controlObj;

public JFrameBounary(ControlClass ctrlObj) {controlObj = ctrlObj;setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(400, 200);

Page 20: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

20

1992-2007 Pearson Education, Inc. All rights reserved.

The boundary class cont.

setLayout( new FlowLayout());enterValue = new JTextField(10);result = new JLabel(“”);squareRoot = new JButton(“Square Root");

add(enterValue);add(result);add(squareRoot);

Handler handler = new Handler();squareRoot.addActionListener(handler);setVisible(true);

} // end constructor

Page 21: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

21

1992-2007 Pearson Education, Inc. All rights reserved.

İnner class Handler

private class Handler implements ActionListener { public void actionPerformed(ActionEvent e) {

double x = Double.parseDouble(.getText());

contrlObj.getValue(x); } // end method actionPerformed} // end inner class Handler

public void setResult(doube x) { String s = String.format(“square root: %10.3f”,x); jTextFieldResult.setText(s);} // end method setResult

} // end class

Page 22: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

22

1992-2007 Pearson Education, Inc. All rights reserved.

Example

Product selectin

Page 23: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

23

1992-2007 Pearson Education, Inc. All rights reserved.

Page 24: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

24

1992-2007 Pearson Education, Inc. All rights reserved.

import javax.swing.*;import java.awt.*;import java.awt.event.*;public class BoundaryControl1 {public static void main(String args[]) {

ControlClass cntrlObj= new ControlClass();

} // end method main} // end class BoundaryContro

Page 25: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

25

1992-2007 Pearson Education, Inc. All rights reserved.

Control Class

class ControlClass { private String products[] = {"cheese","salami","tomata","ovel"}; private double prices[] = {10.5,12.25,20.75,15};

JFrameBoundary boundary;public ControlClass() {

boundary = new JFrameBoundary(this,products);

boundary.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

boundary.setSize(400, 200); boundary.setVisible(true);

} // end constructor

Page 26: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

26

1992-2007 Pearson Education, Inc. All rights reserved.

public void itemsSelected() {double total = 5.0;int i;int selectedIng[] =

boundary.getSelectedItems();for(i=0;i<selectedIng.length;i++)

total += prices[selectedIng[i]];

boundary.setResult(total);} // end method buttonPressed

} // end class ControClass

Page 27: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

27

1992-2007 Pearson Education, Inc. All rights reserved.

class JFrameBoundary extends JFrame {

ControlClass controlObj;

private JList list;private JButton button;private JLabel totalPrice;

public JFrameBoundary(ControlClass ctrlObj, String[] ingrad) {

controlObj = ctrlObj; ; setLayout( new FlowLayout());

Page 28: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

28

1992-2007 Pearson Education, Inc. All rights reserved.

list = new JList(ingrad);list.setVisibleRowCount(2);

list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);button = new JButton("Selected");totalPrice = new JLabel("Total price:");

add(new JScrollPane(list));add(button);add(totalPrice);

Handler handler = new Handler();button.addActionListener(handler);

} // end constructor

Page 29: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

29

1992-2007 Pearson Education, Inc. All rights reserved.

private class Handler implements ActionListener {public void actionPerformed(ActionEvent e) {

controlObj.itemsSelected(); } // end method actionPerformed

} // end inner class Handler

public int[] getSelectedItems() {return list.getSelectedIndices();

} // end method getSelectedItems

public void setResult(double x) {String s = String.format("Total price:%10.3f“,x); totalPrice.setText(s);

} // end method setResult

} // end class JFrameBoundary

Page 30: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

30

1992-2007 Pearson Education, Inc. All rights reserved.

Notes

When items are selected and button is pressed The actionPerformend method’s only task is to

inform the control object that the user seleced some items and want price to be calculated.

Conrol class’s itemsSelected methd get information from the boundary wia the getSelecedItems method, calcualte total and send total to bounday class so as to be displayed in a text Field

Page 31: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

31

1992-2007 Pearson Education, Inc. All rights reserved.

Second Approach: Control class

class ControlClass { private String products[] = {"cheese","salami","tomata","ovel"}; private double prices[] = {10.5,12.25,20.75,15};

JFrameBoundary boundary; public ControlClass() {

boundary = new JFrameBoundary(this,products);

boundary.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

boundary.setSize(400, 200); boundary.setVisible(true);

} // end constructor

Page 32: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

32

1992-2007 Pearson Education, Inc. All rights reserved.

public void itemsSelected(int selectedIng[]) {

double total = 5.0;

for(int i=0;i<selectedIng.length;i++)

total += prices[selectedIng[i]];

boundary.setResult(total);

} // end method buttonPressed

} // end class ControlClass

Page 33: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

33

1992-2007 Pearson Education, Inc. All rights reserved.

Boundary Class

class JFrameBoundary extends JFrame {

ControlClass controlObj;

private JList list;private JButton button;private JLabel totalPrice;

public JFrameBoundary(ControlClass ctrlObj, String[] ingrad) {

controlObj = ctrlObj; ; setLayout( new FlowLayout());

Page 34: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

34

1992-2007 Pearson Education, Inc. All rights reserved.

list = new JList(ingrad);list.setVisibleRowCount(2);

list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);button = new JButton("Selected");totalPrice = new JLabel("Total price:");

add(new JScrollPane(list));add(button);add(totalPrice);

Handler handler = new Handler();button.addActionListener(handler);

} // end constructor

Page 35: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

35

1992-2007 Pearson Education, Inc. All rights reserved.

private class Handler implements ActionListener {public void actionPerformed(ActionEvent e) { controlObj.itemsSelected(list.getSelectedIndices()); } // end method actionPerformed

} // end inner class Handler

public void setResult(double x) {String s = String.format("Total price:

%10.3f“,x); totalPrice.setText(s);

} // end method setResult

} // end class

Page 36: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

36

1992-2007 Pearson Education, Inc. All rights reserved.

Notes

In the actionPerformed mehod of the boundary class seleced information is directly send to the control object.

Seleced item iist is extracted from the list and send as an argument ot the itemsSelected method of the control class

Page 37: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

37

1992-2007 Pearson Education, Inc. All rights reserved.

An ATM Example

• The customer of a bank• Connects to her bank accounts• Select an account – a customer may have more then one

account• Perform opperations

• see balance• Deposit money• Withdraw money• Transfer money to another account• Cancel the oppertations

Using GUIs and database connections

Page 38: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

38

1992-2007 Pearson Education, Inc. All rights reserved.

What are classss?

Classes:– ATM, customer, Account, Connection, GUI

Design principle Seperate clases

– Control classes – ATM

– Entities – customer, account, connection

– Boundry – CUI

Page 39: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

39

1992-2007 Pearson Education, Inc. All rights reserved.

What are classss?

Frame

Connection

Account

Atm

Customer

Page 40: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

40

1992-2007 Pearson Education, Inc. All rights reserved.

User to Atm class

The user has some request from the system– Enter PIN– Select an account– Select opperation– Enter or select amount of money to withdraw

The user should get messages from the system– Not enough oney to withdraw– The account not fount (in case of transfer)

User request taken from the GUI are sent to Atm the control class

Atm class calls the withdraw method of the account class– Send the ınformation to the GUI and the database

Page 41: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

41

1992-2007 Pearson Education, Inc. All rights reserved.

Starting the program

When the program starts– A start frame of the ATM is presented to the user

In reality the user inserts her card – The card is identified

– Read the card number

– Search the database of customers

This is a ATM simulation – İnserting the card is represented as entering the card number

from the keypad

Then enter the PIN (Personel Indentification Number) from the keypad

Page 42: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

42

1992-2007 Pearson Education, Inc. All rights reserved.

Starting the program (cont.)

These information is taken from the GUI is send to the Atm class

The Atm class– Connect to the database– To check whther there is such a customer– if true

- Creates a customer object – name ,…- Get the accounts information from the database- Send this to the GUI

– if false- Ask PIN several times

Page 43: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

43

1992-2007 Pearson Education, Inc. All rights reserved.

Selection of the account

The user selects an account Account number is sent to the Atm class

– Creats the account class

– There are both saving or chacking accounts

After selecting an account – GUI changes

- On the left and right sides show the opperations

- Balance, withdraw, deposit, transfer, cancel

Page 44: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

44

1992-2007 Pearson Education, Inc. All rights reserved.

Withdraw opperation

When the user selects withdraw– There are predetermined amounts

- 20,50,200,300, and others- Deermine the amount and send to Atm

– if the other button is clicked- Enter the amount from the keypad then press Enter button

GUI send the amout of money to be withdrawn trom the accont to the Atm class

The Atm calls – Check the balance of the account – if balacne > amount

- withdraw method of the account- Update the database- Send a message to the user

– if not - Send a message to the user

Page 45: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

45

1992-2007 Pearson Education, Inc. All rights reserved.

Deposit opperation

Similar to withdraw Amount of money deposited is entered from the

keypad and press the Enter button No special choises are presented to the user Note this is just a simulation of the ATM

– In reality the physical amont of money is put to an envelop and inserted into the machine

– Here it is entered from the keypad

Page 46: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

46

1992-2007 Pearson Education, Inc. All rights reserved.

Transfer of Money

Asks the user – the account number to which money will be trnsfered– And amount of money to be transferd

The prohlems are– Account mey not be found– The balnce may not be enough to meke this transfer + commussions if any

So the Atm class– Checks whether there is such an account– And balnce is greater then the money transfered– if so

- Withdraw moeny from the account- Updates the transfer account- Send a mesage to the user that tranfer is made succesfully

– İf not- Send a message to the user indicating that transfer in not succesul

Page 47: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

47

1992-2007 Pearson Education, Inc. All rights reserved.

Cancel button

When pressed cancel the opperations ar cenceled the ATM returns to the first state

Page 48: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

48

1992-2007 Pearson Education, Inc. All rights reserved.

GUI

A class that extends from JFrame Border layout

– North – infromation present information to the user– South – keypad– Cener – textfield and a label

- Amount of moeny, PIN.,,

– East and West – opperations of choises- Withdraw, deposit, transfer, cancel- 50,100,200, others

Keypad– All numbers are enterd from the keypad and appears in a

textField

Page 49: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

49

1992-2007 Pearson Education, Inc. All rights reserved.

AtmTest class

public class AtmTest {

public static void main(String[] args) {

Atm atm = new Atm();

} // end method main

} // end class AtmTest

Page 50: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

50

1992-2007 Pearson Education, Inc. All rights reserved.

The control class

Main task:– Create other objcect from other classes

- Account account, DbConnection dbconnection

- Frame frame

– interract with the boundary object – frame

– Updat the database

– Ubdate the temporary account object

Page 51: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

51

1992-2007 Pearson Education, Inc. All rights reserved.

The control class

In its constructor:– Create the frame object – boundary class –

- Get information from teh user

- Present information to the user

– Create the database connection

Create an account

Page 52: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

52

1992-2007 Pearson Education, Inc. All rights reserved.

The control class - Atm

class Atm {

DBConnection dbConnection; Account account; Account account2;

boolean tF; double balance; String accountNo; String name;

Page 53: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

53

1992-2007 Pearson Education, Inc. All rights reserved.

constructor

/*

Create the frame

And database connection objects

Note in the frame object since it repeatedly calls the Atm (control) class methods there should be a reference to –atm-

setContrl(Atm a) method does this

*/

Page 54: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

54

1992-2007 Pearson Education, Inc. All rights reserved.

constructor

public Control (){ Frame frame=new Frame();

frame.setControl(this); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500,400); frame.setVisible(true); dbConnection = new DBConnection();}

Page 55: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

55

1992-2007 Pearson Education, Inc. All rights reserved.

Method getPassword

/* when the user enters a PIN from the GUI

call the method to send this information to the control class which chacks the database to see whether there is such a customer

if found returns true and

get the account infrmation from the database create an internal account object else return false*/

Page 56: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

56

1992-2007 Pearson Education, Inc. All rights reserved.

Method getPassword

public boolean getPassword(String sifre){ tF=dbConnection.getPassword(sifre); if (tF) {

getDbBalance(); getDbAccountNo(); getDbName();}

createAccount();

return tF;}

Page 57: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

57

1992-2007 Pearson Education, Inc. All rights reserved.

Method getPassword

/*

these methods get information from the database about the account

Page 58: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

58

1992-2007 Pearson Education, Inc. All rights reserved.

public double getDbBalance(){

balance=Double.parseDouble(dbConnection.getInfo("miktar"));return balance;

}

public String getDbAccountNo(){

accountNo=dbConnection.getInfo("Noo");return accountNo;

}

public String getDbName(){

name=dbConnection.getInfo("soyad");return name;

}

Page 59: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

59

1992-2007 Pearson Education, Inc. All rights reserved.

Methods getting infromation from athe internal accout object

• These methods• Creatre teh account object

• Get its instance variables• AccountNo

• Account name

• And balance

• And store in contol object to be used

Page 60: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

60

1992-2007 Pearson Education, Inc. All rights reserved.

public void createAccount(){

account = new Account (name,accountNo,balance);

}

public double getBalance(){

return account.getBalance();}

public String getAccountNo(){

return account.getAccountNo();}

public String getName(){

return account.getAccountName();}

Page 61: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

61

1992-2007 Pearson Education, Inc. All rights reserved.

Withdraw and deposit methods

First updata accont object in memory For withdraw

– if true – enough money in the account – – updata the database– return true to the caller– else – not enough money in the account– no connection to the database is needed– return false

For deposit– Updata the account object and database respectively

Page 62: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

62

1992-2007 Pearson Education, Inc. All rights reserved.

public boolean withdraw(double amount){

if(account.withdraw(amount)){

dbConnection.dbBalanceUpdate(account.getAccountNo(), account.getBalance());

return true;}return false;

}

public void deposit(double amount){

account.deposit(amount);

dbConnection.dbBalanceUpdate(account.getAccountNo(),account.getBalance());}

Page 63: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

63

1992-2007 Pearson Education, Inc. All rights reserved.

The Account class

• An Account class having• İnstance variables

• Balnce

• Name

• Account No

• Constructor to set these variables

• Withdraw and deposit methods

• get methods

• set methods are not wtitten yet

Page 64: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

64

1992-2007 Pearson Education, Inc. All rights reserved.

class Account { private String name; private String accountNo; private double balance; Account(String name,String accountNo, double money){ this.name=name;

this.accountNo=accountNo; balance=money; }

Page 65: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

65

1992-2007 Pearson Education, Inc. All rights reserved.

public void deposit( double amount ) { balance += amount;

}

public boolean withdraw( double amount ){ if (balance >= amount) {

balance -= amount;return true;

} return false;

}

Page 66: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

66

1992-2007 Pearson Education, Inc. All rights reserved.

public String getAccountNo() { return accountNo; } public String getAccountName() { return name; } public double getBalance(){

return balance; }

} // end class Account

Page 67: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

67

1992-2007 Pearson Education, Inc. All rights reserved.

Connection Class - DBConnection

Methods– public boolean getPassword(String sifre)

- İf the sifre is found in the databses return true

– public String getInfo(String variable)- After sifre is found - customer is found in the databse

- Get information and return to the control class

- Variable can be: miktar, Noo, soyad

– public void dbBalanceUpdate(String AccountNo,double balance)

- When money is deposited or withdrawn update the account in the database

Page 68: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

68

1992-2007 Pearson Education, Inc. All rights reserved.

The Frame class

Define – Define a state variable

- “”, deposit, withdraw,…

– Panels– Buttons– No layout – coordinates of components are given– Add compontets to panels– Adjust visibility of panels– Register components to listeners– İmplements ActionListener as an inner class

- actionPerformed method - actionEvent

Page 69: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

69

1992-2007 Pearson Education, Inc. All rights reserved.

Setcontrol method Called from the atm – control class Sets the referance of the control object in Frame

class as frame object calls atm object very often– a referenc is needed

Page 70: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

70

1992-2007 Pearson Education, Inc. All rights reserved.

Page 71: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

71

1992-2007 Pearson Education, Inc. All rights reserved.

Page 72: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

72

1992-2007 Pearson Education, Inc. All rights reserved.

Page 73: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

73

1992-2007 Pearson Education, Inc. All rights reserved.

Page 74: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

74

1992-2007 Pearson Education, Inc. All rights reserved.

Page 75: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

75

1992-2007 Pearson Education, Inc. All rights reserved.

Page 76: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

76

1992-2007 Pearson Education, Inc. All rights reserved.

Page 77: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

77

1992-2007 Pearson Education, Inc. All rights reserved.

Registering Action event

For all the buttons – JButton objects

An inner class implementing ActionListener intrface is registeed using the addActionListener method

Page 78: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

78

1992-2007 Pearson Education, Inc. All rights reserved.

Registering Action event

login.addActionListener(handler);cancel.addActionListener(handler);showBalance.addActionListener(handler);withdraw.addActionListener(handler);deposit.addActionListener(handler);exit.addActionListener(handler);button5.addActionListener(handler);button10.addActionListener(handler);button20.addActionListener(handler);button50.addActionListener(handler);button100.addActionListener(handler);button250.addActionListener(handler);leftcancel.addActionListener(handler);rightOther.addActionListener(handler);okButton.addActionListener(handler);cancelButton.addActionListener(h

Page 79: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

79

1992-2007 Pearson Education, Inc. All rights reserved.

ActionPerformed method

•All the events are action events•The method cheks the source of the event • Array of

Page 80: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

80

1992-2007 Pearson Education, Inc. All rights reserved.

When one number buttons is clicked on the keypad

for (int i = 0; i < keyPadButton.length; i++)

{if (event.getSource() ==

keyPadButton[i]){

password = password + i;passwordd = passwordd + "*";

}

txtEnterPassword.setText(passwordd);//txtwithdraw.setText(password);

}

Page 81: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

81

1992-2007 Pearson Education, Inc. All rights reserved.

When right pannel is

if (event.getSource() == leftcancel){

balancePanel.setVisible(false);leftPanel.setVisible(true);rightPanel.setVisible(true);withdrawLeftPanel.setVisible(false);

withdrawPanel.setVisible(false);keyPadPane.setVisible(false);confirmationPanel.setVisible(false);

withdrawRightPanel.setVisible(false);strAmount = "";txtwithdraw.setText(strAmount);intAmount = 0;

}

Page 82: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

82

1992-2007 Pearson Education, Inc. All rights reserved.

Withdraw is clicked

if (event.getSource() == withdraw){

balancePanel.setVisible(false);leftPanel.setVisible(false);rightPanel.setVisible(false);withdrawLeftPanel.setVisible(true);

withdrawRightPanel.setVisible(true);

moneyMessage.setVisible(false);withdrawPanel.setVisible(false);

confirmationPanel.setVisible(false);keyPadPane.setVisible(false);

}

Page 83: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

83

1992-2007 Pearson Education, Inc. All rights reserved.

Withdrawing money

The user can select one of the butons– 5,10,20,50,100,250

– Call the withdraw method of the control object

– if true – oppertation is succesful

– Write an appropriate message to the user

– No physical money is given to the user

Or select other– The amount of money to be witdrawn is asked

– Prepare the screen so that the user can enter any amount from the keypad

Page 84: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

84

1992-2007 Pearson Education, Inc. All rights reserved.

if (event.getSource() == button5) {

if (control.withdraw(5))

lblmoneyMessage.setText("Please Get Your Money"); else

lblmoneyMessage.setText("You do not have enough money in your account");

moneyMessage.setVisible(true);} // end if 5

Page 85: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

85

1992-2007 Pearson Education, Inc. All rights reserved.

if (event.getSource() == rightOther)

{

passwordd = "";

state = "withdraw";

withdrawPanel.setVisible(true);

keyPadPane.setVisible(true);

confirmationPanel.setVisible(true);

}

Page 86: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

86

1992-2007 Pearson Education, Inc. All rights reserved.

Depositing money

•If the user click deposit•The screen is prepaired for dopositnig money• Keypad • Texfield for entering moeny is shown to the user

• Stare is set to deposit

Page 87: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

87

1992-2007 Pearson Education, Inc. All rights reserved.

if (event.getSource() == deposit){

password = "";state = "deposit";withdrawPanel.setVisible(true);keyPadPane.setVisible(true);

confirmationPanel.setVisible(true);}

Page 88: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

88

1992-2007 Pearson Education, Inc. All rights reserved.

Ok button

When the ok button is clicked

The resutlts depends on the state

Whether state is deposit or withdraw

Page 89: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

89

1992-2007 Pearson Education, Inc. All rights reserved.

if (event.getSource() == okButton){

if (password != "")

if (state.equals("withdraw")){

intAmount = Integer.parseInt(password);

if (control.withdraw(intAmount))

lblmoneyMessage.setText("Please Get Your Money...");else

lblmoneyMessage.setText("You do not have enough money in your account");moneyMessage.setVisible(true);

withdrawPanel.setVisible(false);keyPadPane.setVisible(false);

confirmationPanel.setVisible(false);}

Page 90: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

90

1992-2007 Pearson Education, Inc. All rights reserved.

if (state.equals("deposit")){

if (password != ""){

intAmount = Integer.parseInt(password);

control.deposit(intAmount);lblmoneyMessage.setText("Your

Money Has Been Deposited");

moneyMessage.setVisible(true);

withdrawPanel.setVisible(false);keyPadPane.setVisible(false);

confirmationPanel.setVisible(false);}

}}

Page 91: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

91

1992-2007 Pearson Education, Inc. All rights reserved.

cancel

if (event.getSource() == cancelButton)

{password = "";state = "";accountPanel.setVisible(false);

withdrawPanel.setVisible(false);keyPadPane.setVisible(false);

confirmationPanel.setVisible(false);

}

Page 92: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

92

1992-2007 Pearson Education, Inc. All rights reserved.

Exit

Return to the first screen Change the state to “” Adjust the visibility of panels Set the String and numerical variables to ** or

zero

Page 93: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

93

1992-2007 Pearson Education, Inc. All rights reserved.

if (event.getSource() == exit){

keyPadPane.setVisible(true);enterPane.setVisible(true);leftPanel.setVisible(false);rightPanel.setVisible(false);accountPanel.setVisible(false);balancePanel.setVisible(false);

withdrawLeftPanel.setVisible(false);

withdrawRightPanel.setVisible(false);withdrawPanel.setVisible(false);

confirmationPanel.setVisible(false);

Page 94: 1992-2007 Pearson Education, Inc. All rights reserved. 1 Case Studies.

94

1992-2007 Pearson Education, Inc. All rights reserved.

password = ""; passwordd = "";

txtEnterPassword.setText(passwordd);

txtwithdraw.setText(password);

state = ""; balanceamount = 0;

intAmount = 0; strAmount = "";} // end if for exit