950 likes | 1.1k 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 multiplying a number with 2 • Get an mumber from the user and multiply it with 2 display the results to the TextField • Control object • Creats the xFrame object • Boundary class communicates with user • When the user enters a number to first field and press multiply button • The contrl class is invoked • Control object gets the numer in the textField prform processing – multiply by two – send to boundary (xFrame)
Test class import javax.swing.*; import java.awt.*; import java.awt.event.*; public class BounCont0 { public static void main(String args[]) { XCont0 xCont0 = new XCont0(); } // end method main } // end class BounCont0
The Control class class XCont0 { XFrame0 xFrame0; int number; public XCont0() { xFrame0 = new XFrame0(this); } // end constructor public void buttonPressed() { number = xFrame0.getVal1(); number *=2; xFrame0.setResult(number); } // end method buttonPressed } // end class XContFr0
The Boundary class class XFrame0 extends JFrame { JTextField jTextFieldVal1; JTextField jTextFieldResult; JButton jMultButton; XCont0 xC; public XFrame0(XCont0 x) { xC =x; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 200);
The boundary class cont. setLayout( new FlowLayout()); jTextFieldVal1 = new JTextField(10); jTextFieldResult = new JTextField(10); jMultButton = new JButton("Multiply"); add(jTextFieldVal1); add(jTextFieldResult); add(jMultButton); Handler handler = new Handler(); jMultButton.addActionListener(handler); setVisible(true); } // end constructor
İnner class Handler private class Handler implements ActionListener { public void actionPerformed(ActionEvent e) { xC.buttonPressed(); } // end method actionPerformed } // end inner class Handler public int getVal1() { return Integer.parseInt(jTextFieldVal1.getText()); } // end method getVal1 public void setResult(int x) { String s = ""+x; jTextFieldResult.setText(s); } // end method setResult } // end class
Notes • xFrame object calls control class so the control object has to be registered to the xFrame object • When an xFrame0 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 XCont0 { XFrame0 xFrame0; int number; public XCont0() { xFrame0 = new XFrame0(this); } // end constructor public void getNumber(int n) { number =n; number *=2; xFrame0.setResult(number); } // end method buttonPressed } // end class XContFr0
The Boundary class class XFrame0 extends JFrame { JTextField jTextFieldVal1; JTextField jTextFieldResult; JButton jMultButton; XCont0 xC; public XFrame0(XCont0 x) { xC =x; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 200);
The boundary class cont. setLayout( new FlowLayout()); jTextFieldVal1 = new JTextField(10); jTextFieldResult = new JTextField(10); jMultButton = new JButton("Multiply"); add(jTextFieldVal1); add(jTextFieldResult); add(jMultButton); Handler handler = new Handler(); jMultButton.addActionListener(handler); setVisible(true); } // end constructor
İnner class Handler private class Handler implements ActionListener { public void actionPerformed(ActionEvent e) { int n = Integer.parseInt(jTextFieldVal1.getText()); xC. getNumber(n); } // end method actionPerformed } // end inner class Handler public void setResult(int x) { String s = ""+x; jTextFieldResult.setText(s); } // end method setResult } // end class
Example • Product selectin
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class BounCnt1 { public static void main(String args[]) { XCont1 xCont1 = new XCont1(); } // end method main } // end class BounCmt1
Control Class class XCont1 { private String products[] = {"cheese","salami","tomata","ovel"}; private int prices[] = {10,12,20,15}; XFrame1 xFrame1; public XCont1() { xFrame1 = new XFrame1(this,products); xFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); xFrame1.setSize(400, 200); xFrame1.setVisible(true); }
public void itemsSelected() { int total = 5; int i; int selectedIng[] = xFrame1.getSelectedItems(); for(i=0;i<selectedIng.length;i++) total += prices[selectedIng[i]]; xFrame1.setResult(total); } // end method buttonPressed } // end class XCount1
class XFrame1 extends JFrame { XCont1 xC; private JList list; private JButton button; private JTextField totalPrice; public XFrame1(XCont1 x,String[] ingrad) { xC=x; setLayout( new FlowLayout());
list = new JList(ingrad); list.setVisibleRowCount(2); list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); button = new JButton("Selected"); totalPrice = new JTextField(10); add(list); 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) { xC.itemsSelected(); } // end method actionPerformed } // end inner class Handler public int[] getSelectedItems() { return list.getSelectedIndices(); } // end method getSelectedItems public void setResult(int x) { String s = "Total price:"+x; totalPrice.setText(s); } // end method setResult } // end class
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 XCont1 { private String products[] = {"cheese","salami","tomata","ovel"}; private int prices[] = {10,12,20,15}; XFrame1 xFrame1; public XCont1() { xFrame1 = new XFrame1(this,products); xFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); xFrame1.setSize(400, 200); xFrame1.setVisible(true); }
public void itemsSelected(int selectedIng[]) { int total = 5; for(int i=0;i<selectedIng.length;i++) total += prices[selectedIng[i]]; xFrame1.setResult(total); } // end method buttonPressed } // end class XCount1
Boundary Class class XFrame1 extends JFrame { XCont1 xC; private JList list; private JButton button; private JTextField totalPrice; public XFrame1(XCont1 x,String[] ingrad) { xC=x; setLayout( new FlowLayout());
list = new JList(ingrad); list.setVisibleRowCount(2); list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); button = new JButton("Selected"); totalPrice = new JTextField(10); add(list); 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) { xC.itemsSelected(list.getSelectedIndices()); } // end method actionPerformed } // end inner class Handler public void setResult(int x) { String s = "Total price:"+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