In: Computer Science
I need to create a program that will simulate an ATM. The program has to be written in JAVA that uses JOptionaPane to pop up the messages. It will need to simulate to get
** balance
**withdraw
**Deposit
**exit the atm
** need to have a method that shows a receipt of everything that was done during the transaction
If you have any problem with the code feel free to comment.
Program
import javax.swing.JFrame;
import javax.swing.JOptionPane;
class ATM {
//instance variables
private int balance, withdraw, deposit;
//setters
public void setBalance(int balance) {
this.balance = balance;
}
public void setWithdraw(int withdraw) {
this.withdraw = withdraw;
}
public void setDeposit(int deposit) {
this.deposit = deposit;
}
public void performDeposit() {
if(deposit>0) {//if value is
less than zero balance remain unchanged
balance +=
deposit;
}
}
public void performWithdraw() {
if(withdraw<balance) {//if
withdraw is greater than balance, balance remain unchanged
balance -=
withdraw;
}
}
//for showing reciept
public void showReciept() {
System.out.println("**********Reciept**********");
System.out.println("Initial
Balance: $"+balance);
System.out.println("Withdraw
Amount: $"+withdraw);
performWithdraw();
System.out.println("Balance after
withdraw: $"+balance);
System.out.println("Deposit Amount:
$"+deposit);
performDeposit();
System.out.println("Balance after
deposit: $"+balance);
}
}
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
ATM obj = new ATM();
//getting user input using
JOptionPane
String balance =
JOptionPane.showInputDialog(frame,"Enter Balance");
String withdraw =
JOptionPane.showInputDialog(frame,"Enter Withdraw");
String deposit =
JOptionPane.showInputDialog(frame,"Enter Deposit");
//setting user input
obj.setBalance(Integer.parseInt(balance));
obj.setWithdraw(Integer.parseInt(withdraw));
obj.setDeposit(Integer.parseInt(deposit));
obj.showReciept();//showing
reciept
System.exit(0);
}
}
Output