950 likes | 1.21k Views
Case Studies. 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
E N D
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
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
Relationb etween classes Entity Boundary frames contro Boundary database Entity
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
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
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
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
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
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
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
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
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);
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
İ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
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.
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
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);
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
İ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
Example • Product selectin
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
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
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
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());
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
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
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
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
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
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());
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
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
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
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
What are classss? • Classes: • ATM, customer, Account, Connection, GUI • Design principle • Seperate clases • Control classes – ATM • Entities – customer, account, connection • Boundry – CUI
What are classss? Account Frame Atm Connection Customer
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
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
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
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
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
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
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
Cancel button • When pressed cancel the opperations ar cenceled the ATM returns to the first state
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
AtmTest class public class AtmTest { public static void main(String[] args) { Atm atm = new Atm(); } // end method main } // end class AtmTest
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