Write a program that implements an ATM machine. The interface to the program sho
ID: 667344 • Letter: W
Question
Write a program that implements an ATM machine. The interface to the program should be a GUI that looks similar to the following:
The program should consist of three classes.
The first class should define the GUI and should be hand-coded and not generated by a GUI generator. In addition to the main method and a constructor to build the GUI, event handlers will be needed to handle each of the four buttons shown above.
When the Withdraw button is clicked, several checks must be made. The first check is to ensure the value in the text field is numeric. Next a check must be made to ensure the amount is in increments of $20. At that point an attempt to withdraw the funds is made from the account selected by the radio buttons. The attempt might result in an exception being thrown for insufficient funds, If any of those three errors occur a JOptionPane window should be displayed explaining the error. Otherwise a window should be displayed confirming that the withdrawal has succeeded. When the Deposit button is clicked the only necessary check is to ensure that the amount input in the textfield is numeric. Clicking the Transfer button signifies transferring funds to the selected account from the other account. The checks needed are to confirm that the amount supplied is numeric and that there are sufficient funds in the account from which the funds are being transferred. Clicking the Balance button will cause a JOptionPane window to be displayed showing the current balance in the selected account.
The main class must contain twoAccount objects, one for the checking account and another for the savings account.
The second class is Account. It must have a constructor plus a method that corresponds to each of the four buttons in the GUI. It must also incorporate logic to deduct a service charge of $1.50 when more than four total withdrawals are made from either account. Note that this means, for example, if two withdrawals are made from the checking and two from the savings, any withdrawal from either account thereafter incurs the service charge. The method that performs the withdrawals must throw an InsufficientFunds exception whenever an attempt is made to withdraw more funds than are available in the account. Note that when service charges apply, there must also be sufficient funds to pay for that charge.
The third class is InsufficientFunds, which is a user defined checked exception.
Be sure to follow good programming style, which means making all instance and class variables private, naming all constants and avoiding the duplication of code. Furthermore you must select enough scenarios to completely test the program.
Explanation / Answer
import java.io.IOException;
/** An ATM that accesses a bank. */
public class ATM
{
/**
Constructs an ATM for a given bank.
@naveen aBank the bank to which this ATM connects
*/
public ATM(Bank aBank)
{
theBank = aBank;
reset();
}
/**
Resets the ATM to the initial state.
*/
public void reset()
{
customerNumber = -1;
currentAccount = null;
state = START;
}
/**Sets the current customer number and sets state to PIN.(Precondition: state is START)@naveen number the customer number.*/
public void setCustomerNumber(int number)
{
assert state == START;
customerNumber = number;
state = PIN;
}
/**Finds customer in bank.If found sets state to ACCOUNT, else to START.(Precondition: state is PIN)@naveen pin the PIN of the current customer*/
public void selectCustomer(int pin)
{
assert state == PIN;
currentCustomer= theBank.findCustomer(customerNumber, pin);
if (currentCustomer == null)
state = START;
else
state = ACCOUNT;
}
/**Sets current account to checking or savings. Sets state to TRANSACT.(Precondition: state is ACCOUNT or TRANSACT)@naveen account one of CHECKING or SAVINGS*/
public void selectAccount(int account)
{
assert state == ACCOUNT || state == TRANSACT;
if (account == CHECKING)
currentAccount = currentCustomer.getCheckingAccount();
else
currentAccount = currentCustomer.getSavingsAccount();
state = TRANSACT;
}
/**Withdraws amount from current account.(Precondition: state is TRANSACT)@naveen value the amount to withdraw*/
public void withdraw(double value)
{
assert state == TRANSACT;
currentAccount.withdraw(value);
}
/**Deposits amount to current account.(Precondition: state is TRANSACT)@naveen value the amount to deposit*/
public void deposit(double value)
{
assert state == TRANSACT;
currentAccount.deposit(value);
}
/**Gets the balance of the current account.(Precondition: state is TRANSACT)@return the balance*/
public double getBalance()
{
assert state == TRANSACT;
return currentAccount.getBalance();
}
/**Moves back to the previous state.*/
public void back()
{
if (state == TRANSACT)
state = ACCOUNT;
else if (state == ACCOUNT)
state = PIN;
else if (state == PIN)
state = START;
}
/**Gets the current state of this ATM.@return the current state*/
public int getState()
{
return state;
}
private int state;
private int customerNumber;
private Customer currentCustomer;
private BankAccount currentAccount;
private Bank theBank;
public static final int START = 1;
public static final int PIN = 2;
public static final int ACCOUNT = 3;
public static final int TRANSACT = 4;
public static final int CHECKING = 1;
public static final int SAVINGS = 2;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.