Using ***JAVA***, write a program that implements an ATM machine. The interface
ID: 3774515 • Letter: U
Question
Using ***JAVA***, 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 two Account 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, utilizing comments and avoiding the duplication of code. Furthermore you must select enough scenarios to completely test the program.
ATM Machine Withdraw Deposit Transfer to Balance Checking Savings OExplanation / Answer
GUI Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ATM {
private JFrame frame;
private JButton withdrawl;
private JButton deposit;
private JButton transfer;
private JButton balance;
private JRadioButton checkingButton;
private JRadioButton savingsButton;
private JTextField input;
public static void main(String[] args) {
ATM frame = new ATM();
frame.createFrame();
}
public void Frame(){
createFrame();
}
private void createFrame(){
frame = new JFrame("ATM Machine");
frame.setLocationRelativeTo(null);
frame.setSize(300,220);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
withdrawl = new JButton("Withdrawl");
deposit = new JButton("Deposit");
transfer = new JButton("Transfer");
balance = new JButton("Balance");
checkingButton = new JRadioButton("Checking");
savingsButton = new JRadioButton("Savings");
input = new JTextField();
ButtonGroup group = new ButtonGroup();
group.add(checkingButton);
group.add(savingsButton);
controls(contentPane);
frame.setVisible(true);
}
private void controls(Container contentPane) {
GroupLayout layout = new GroupLayout(contentPane);
contentPane.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(withdrawl)
.addComponent(transfer)
.addComponent(checkingButton)
.addComponent(input))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(deposit)
.addComponent(balance)
.addComponent(savingsButton)
));
layout.setVerticalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(withdrawl)
.addComponent(deposit))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(transfer)
.addComponent(balance))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(checkingButton)
.addComponent(savingsButton))
.addComponent(input));
contentPane.add(withdrawl);
contentPane.add(deposit);
contentPane.add(transfer);
contentPane.add(balance);
contentPane.add(checkingButton);
contentPane.add(savingsButton);
}}
Business Logic:
import java.util.*;
public class ATM {
public static Scanner kbd = new Scanner(System.in);
// The checkID method determines if acctNum is a valid account number
// and pwd is the correct password for the account. If the account information
// is valid, the method returns the current account balance, as a string.
// If the account information is invalid, the method returns the string "error".
public static String checkID(String acctNum, String pwd)
{
String result = "error";
// Strings a, b, and c contain the valid account numbers and passwords.
// For each string, the account number is listed first, followed by
// a space, followed by the password for the account, followed by a space,
// followed by the current balance.
String a = "44567-5 mypassword 520.36";
String b = "1234567-6 anotherpassword 48.20";
String c = "4321-0 betterpassword 96.74";
if (acctNum.equals(a.substring(0, a.indexOf(" "))) &&
pwd.equals(a.substring(a.indexOf(" ")+1,a.lastIndexOf(" "))))
return result = a.substring(a.lastIndexOf(" ") + 1);
if (acctNum.equals(b.substring(0, b.indexOf(" "))) &&
pwd.equals(b.substring(b.indexOf(" ")+1,b.lastIndexOf(" "))))
return result = b.substring(b.lastIndexOf(" ") + 1);
if (acctNum.equals(c.substring(0, c.indexOf(" "))) &&
pwd.equals(c.substring(c.indexOf(" ") + 1,c.lastIndexOf(" "))))
return result = c.substring(c.lastIndexOf(" ") + 1);
return result;
}
public static int menu()
{
int menuChoice;
do
{
System.out.print(" Please Choose From the Following Options:"
+ " 1. Display Balance 2. Deposit"
+ " 3. Withdraw 4. Log Out ");
menuChoice = kbd.nextInt();
if (menuChoice < 1 || menuChoice > 4){
System.out.println("error");
}
}while (menuChoice < 1 || menuChoice > 4);
return menuChoice;
}
public static void displayBalance(double x)
{
System.out.printf(" Your Current Balance is $%.2f ", x);
}
public static double deposit(double x, double y)
{
double depositAmt = y, currentBal = x;
double newBalance = depositAmt + currentBal;
System.out.printf("Your New Balance is $%.2f ", newBalance);
return newBalance;
}
public static double withdraw(double x, double y)
{
double withdrawAmt = y, currentBal = x, newBalance;
newBalance = currentBal - withdrawAmt;
System.out.printf("Your New Balance is %.2f ",newBalance);
return newBalance;
}
public static void main(String[] args) {
String accNum, pass, origBal = "error";
int count = 0, menuOption = 0;
double depositAmt = 0, withdrawAmt = 0, currentBal=0;
boolean foundNonDigit;
//loop that will count the number of login attempts
//you make and will exit program if it is more than 3.
//as long as oriBal equals an error.
do{
foundNonDigit = false;
System.out.println("Please Enter Your Account Number: ");
accNum = kbd.next();
System.out.println("Enter Your Password: ");
pass = kbd.next();
origBal = checkID(accNum, pass);
count++;
if (count >= 3 && origBal.equals("error")){
System.out.print("Maximum Login Attempts Reached.");
System.exit(0);
}
if (!(origBal.equals("error"))){
System.out.println(" Your New Balance is: $ "+ origBal);
}
else
System.out.println(origBal);
}while(origBal.equals("error"));
currentBal=Double.parseDouble(origBal);
//this loop will keep track of the options that
//the user inputs in for the menu. and will
//give the option of deposit, withdraw, or logout.
while (menuOption != 4)
{
menuOption=menu();
switch (menuOption)
{
case 1:
displayBalance(currentBal);
break;
case 2:
System.out.print(" Enter Amount You Wish to Deposit: $ ");
depositAmt = kbd.nextDouble();
currentBal=deposit(depositAmt, currentBal);
break;
case 3:
System.out.print(" Enter Amount You Wish to Withdrawl: $ ");
withdrawAmt = kbd.nextDouble();
while(withdrawAmt>currentBal){
System.out.print("ERROR: INSUFFICIENT FUNDS!! "
+ "PLEASE ENTER A DIFFERENT AMOUNT: $");
withdrawAmt = kbd.nextDouble();
}
currentBal = withdraw(currentBal, withdrawAmt);
break;
case 4:
System.out.print(" Thank For Using My ATM. Have a Nice Day. Good-Bye!");
System.exit(0);
break;
}
}
}
}
Output:
Please Enter Your Account Number:
44567-5
Enter Your Password:
mypassword
Your New Balance is: $ 520.36
Please Choose From the Following Options:
1. Display Balance
2. Deposit
3. Withdraw
4. Log Out
2
Enter Amount You Wish to Deposit: $ 400
Your New Balance is $920.36
Please Choose From the Following Options:
1. Display Balance
2. Deposit
3. Withdraw
4. Log Out
3
Enter Amount You Wish to Withdrawl: $ 1000
ERROR: INSUFFICIENT FUNDS!! PLEASE ENTER A DIFFERENT AMOUNT: $600
Your New Balance is 320.36
Please Choose From the Following Options:
1. Display Balance
2. Deposit
3. Withdraw
4. Log Out
1
Your Current Balance is $320.36
Please Choose From the Following Options:
1. Display Balance
2. Deposit
3. Withdraw
4. Log Out
4
Thank For Using My ATM. Have a Nice Day. Good-Bye!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.