Title: Case Study:
1- Case Study
- Programming An ATM
2HW 4
- 3 x 3 array
- For player computer generate randoem variable
between 1 and 3 - import java.util.Random
- Random rand new Random()
- int i rand.nextInt(3)
- i between 0-2 uniformly distributed random
integer
3HW 4
- Wining condition
- If
- Two states
- Current
- Previous
- do
- when enrted to loop make curent state privous
- After a play make new stae current
- Check wining conditton
- If someone wins record previous
- Record previous state
- Eles
- While(stoping condition)
4The ArrayList Class
- Array store objects but
- Once an Array is created its size is fixed
- Java ArrayList class to store unlimited number of
objects
5Some Methods of the ArrayList Class
- ArrayList()
- Create an empty list
- void add(Object o)
- Append a new element o at the end of the list
- void add(int index, Object o)
- Add a new element o at the specifed index to the
list - void clear()
- Removes all the elements from the list
- boolean contains(Object o)
- Returns true if the lsit contains Object o
6Some Methods of the ArrayList Class (cont.)
- Object get(int index)
- Returns the element from the list at the
specified index - int indexOf(Object o)
- Retuns the index of the first matching element in
the list - boolean isEmpty()
- Returns true if the list is empty contains no
elements - int lastIndexOf(Object o)
- Returns the index of the last matching element
- boolean remove(Object o)
- Removes the element from the list
7Some Methods of the ArrayList Class (cont.)
- int size()
- Returns the number of elements in the list
- boolean remove(int index)
- Removes the element at the specifed index from
the list - Object set(int index, Object o)
- Sets the element at the specified index
8Comparing Array and ArrayList
- Creatng an Array/ArrayList
- Object a new Object10
- ArrayList list new ArrayList()
- Accesing an element
- aindex
- list.get(index)
- Updating an element
- aindex London
- list.set(index,London)
9Comparing Array and ArrayList (cont.)
- Returning size
- a.length
- list.size()
- Adding a new element
- -
- list.add(London)
- Inserting a new element
- -
- list. add(index,London)
10Comparing Array and ArrayList (cont.)
- Removing an element
- -
- list.remove(index)
- list.remove ( London)
- Removing all elements
- -
- list.clear()
11An Example Program
- import java.util.ArrayList
- public class ArrayListTest
- public static void main(String args)
-
- ArrayList list new ArrayList()
- list.add("Ankara")
- list.add("Istanbul")
- list.add("Izmir")
- System.out.println("size"list.size())
- int i list.indexOf("Ankara")
- System.out.println("index of Ankara"i)
- System.out.println("index of Ankara"list.indexOf
("kjk")) - Integer j new Integer(2)
-
12An Example Program (cont.)
- list.add(j)
- System.out.println("size"list.size())
- int k 2
- list.add(k)
- System.out.println("size"list.size())
- X x new X()
- list.add(x)
- System.out.println("size"list.size())
- System.out.println("index of
x"list.indexOf(x)) - X y (X)list.get(5)
- System.out.println("value of y.a"y.a)
-
-
13An Example Program (cont.)
- class X
- public double a 10
-
14An ATM Example
- The customer of a bank connects to her bank
accounts - Select an account a customer may have more then
one account - Prform opperations
- see balance
- Deposit money
- Withdraw money
- Transfer money to another account
- Cancel the oppertations
- Using GUIs and database connecti
15What are classss?
- Classes
- ATM, customer, Account, Connection, GUI
- Design principle
- Seperate clases
- Control classes ATM
- Entities customer, account, connection
- Boundry CUI
16What are classss?
Account
Frame
Atm
Connection
Customer
17User 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 information to the GUI and the database
18Starting 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
- Inserting the card is represented as entering the
card number from the keypad - Then enter the PIN (Personel Indentification
Number) from the keypad
19Starting 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
20Selection 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
21Withdraw 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 gt amount
- withdraw method of the account
- Update the database
- Send a message to the user
- if not
- Send a message to the user
22Deposit 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
23Transder 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 - If not
- Send a message to the user indicating that
transfer in not succesul
24Cancel button
- When pressed cancel the opperations ar cenceled
the ATM returns to the first state
25GUI
- 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
26AtmTest class
- public class AtmTest
- public static void main(String args)
-
- Atm atm new Atm()
- // end method main
- // end class AtmTest
27The 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
28The 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
29The control class - Atm
- class Atm
-
- DBConnection dbConnection
- Account account
- Account account2
-
- boolean tF
- double balance
- String accountNo
- String name
30constructor
- /
- 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
- /
31constructor
- public Control ()
-
- Frame framenew Frame()
- frame.setControl(this)
-
- frame.setDefaultCloseOperation(JFrame.EXIT_O
N_CLOSE) - frame.setSize(500,400)
- frame.setVisible(true)
- dbConnection new DBConnection()
-
32Method 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
- /
33Method getPassword
- public boolean getPassword(String sifre)
-
- tFdbConnection.getPassword(sifre)
- if (tF)
-
- getDbBalance()
- getDbAccountNo()
- getDbName()
-
- createAccount()
-
- return tF
-
34Method getPassword
- /
- these methods get information from the database
about the account
35- public double getDbBalance()
-
- balanceDouble.parseDouble(dbConnection.getInfo(
"miktar")) - return balance
-
-
- public String getDbAccountNo()
-
- accountNodbConnection.getInfo("Noo")
- return accountNo
-
-
- public String getDbName()
-
- namedbConnection.getInfo("soyad")
- return name
-
36Methods 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
37- 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()
38Withdraw 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
39- public boolean withdraw(double amount)
-
- if(account.withdraw(amount))
-
- dbConnection.dbBalanceUpdate(account.getAccount
No(), account.getBalance()) - return true
-
- return false
-
-
- public void deposit(double amount)
-
- account.deposit(amount)
- dbConnection.dbBalanceUpdate(account.getAccountN
o(),account.getBalance()) -
40The Account class
- An Account class having
- Instance variables
- Balnce
- Name
- Account No
- Constructor to set these variables
- Withdraw and deposit methods
- get methods
- set methods are not wtitten yet
-
41- class Account
- private String name
- private String accountNo
- private double balance
-
- Account(String name,String accountNo, double
money) - this.namename
- this.accountNoaccountNo
- balancemoney
-
42- public void deposit( double amount )
-
- balance amount
-
- public boolean withdraw( double amount )
-
- if (balance gt amount)
-
- balance - amount
- return true
-
- return false
-
43- public String getAccountNo()
-
- return accountNo
-
- public String getAccountName()
-
- return name
-
- public double getBalance()
- return balance
-
- // end class Account
44Connection Class - DBConnection
- Methods
- public boolean getPassword(String sifre)
- If 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
45The 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
- Implements ActionListener as an inner class
- actionPerformed method - actionEvent
46- 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
47(No Transcript)
48(No Transcript)
49(No Transcript)
50(No Transcript)
51(No Transcript)
52(No Transcript)
53(No Transcript)
54Registering Action event
- For all the buttons JButton objects
- An inner class implementing ActionListener
intrface is registeed using the addActionListener
method
55Registering 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
56ActionPerformed method
- All the events are action events
- The method cheks the source of the event
- Array of
57When one number buttons is clicked on the keypad
- for (int i 0 i lt keyPadButton.length i)
-
- if (event.getSource() keyPadButtoni)
-
- password password i
- passwordd passwordd ""
-
-
- txtEnterPassword.setText(passwordd)
- //txtwithdraw.setText(password)
-
-
58When 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
-
-
59Withdraw 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)
-
60Withdrawing 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
61- 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
62- if (event.getSource() rightOther)
-
- passwordd ""
- state "withdraw"
- withdrawPanel.setVisible(true)
- keyPadPane.setVisible(true)
- confirmationPanel.setVisible(true)
63Depositing 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
64- if (event.getSource() deposit)
-
- password ""
- state "deposit"
- withdrawPanel.setVisible(true)
- keyPadPane.setVisible(true)
- confirmationPanel.setVisible(true)
-
65Ok button
- When the ok button is clicked
- The resutlts depends on the state
- Whether state is deposit or withdraw
66- 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)
67- 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)
-
-
-
68cancel
- if (event.getSource() cancelButton)
-
- password ""
- state ""
- accountPanel.setVisible(false)
- withdrawPanel.setVisible(false)
- keyPadPane.setVisible(false)
- confirmationPanel.setVisible(false)
-
69Exit
- Return to the first screen
- Change the state to
- Adjust the visibility of panels
- Set the String and numerical variables to or
zero
70- 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)
-
71- password ""
- passwordd ""
-
- txtEnterPassword.setText(passwordd)
- txtwithdraw.setText(password)
-
- state ""
- balanceamount 0
-
- intAmount 0
- strAmount ""
- // end if for exit