Create a program called MyAtm. This program will have a minimum of three classes
ID: 3761853 • Letter: C
Question
Create a program called MyAtm. This program will have a minimum of three classes.
The driver class: This class will control the flow of the program.
The ATM class: This class will provide the menu and the methods required to authenticate the user's PIN, display the account balance, do a withdrawal and do a deposit.
The Account class: This class will store the user's PIN and account balance. This class will also provide the methods necessary to update the account balance.
The program will instantiate two account class instances when the ATM class is instantiated. The account information for the two accounts will be:
First Name: Brenda
Last Name: Thompson
PIN: 1234
Account Balance: $100.00
First Name: Tim
Last Name: Hansen
PIN: 1111
Account Balance: $25.20
When an instance of an account is instantiated, the PIN will be immutable. The first and last names along with the account balance will be mutable.
Only allow withdrawals to be done in increments of $20.00. Display to the user a message if the value is not in an increment of $20.00. Also, if the user exceeds the available funds, display another message. Allow the user to cancel or try again.
Only one transaction can be completed each time a user enters their PIN. After a transaction has been completed or canceled, prompt the user for their PIN. If the PIN does not match either of the accounts, display a message stating the PIN is not recognized then return to the PIN prompt.
Explanation / Answer
//Account.java
public class Account
{
//instance variables of class
private String firstName;
private String lastName;
private int pin;
private double balance;
//Constructor of Account
public Account(String firstName,
String lastName,
int pin,
double balance)
{
this.firstName=firstName;
this.lastName=lastName;
this.pin=pin;
this.balance=balance;
}
//Sets first name
public void setFName(String firstName)
{
this.firstName=firstName;
}
//Sets last name
public void setLName(String lastName)
{
this.lastName=lastName;
}
//Sets balance
public void setBalance(double balance)
{
this.balance=balance;
}
//Returns first name
public String getFName()
{
return firstName;
}
//Returns last name
public String getLName()
{
return lastName;
}
//Returns pin
public int getPIN()
{
return pin;
}
//Returns balance
public double getBalance()
{
return balance;
}
//Returns string represetnation of accout object
@Override
public String toString() {
return "First Name:"+firstName+
"Last Name:"+lastName+
"Balance:"+balance;
}
}
------------------------------------------------------------------------------------------------------------
//ATM.java
import java.util.Scanner;
public class ATM
{
//declare two variables of Account
Account account1;
Account account2;
private Scanner scanner=new Scanner(System.in);
public ATM()
{
//declare two objects of ATM class
account1=new Account("Brenda",
"Thomson", 1234, 100);
account2=new Account("Tim",
"Hansen", 1111,25.20);
}
//Menu choices
public int menu()
{
int choice;
System.out.println("1.Accoount Balance");
System.out.println("2.Withdrawl");
System.out.println("3.Deposit");
System.out.println("Enter your choice");
choice=scanner.nextInt();
return choice;
}
//Authenticate pin
public boolean authenticatePIN(int pin)
{
return pin==account1.getPIN() ||
pin==account2.getPIN() ;
}
//Withdrawl method using pin
public void withdrawl(int pin,double amount)
{
if(pin==account1.getPIN())
{
account1.setBalance(account1.getBalance()-amount);
}
else if(pin==account2.getPIN())
{
account2.setBalance(account2.getBalance()-amount);
}
}
//Deposit method using pin
public void deposit(int pin, double amount)
{
if(pin==account1.getPIN())
{
account1.setBalance(account1.getBalance()+amount);
}
else if(pin==account2.getPIN())
{
account2.setBalance(account2.getBalance()+amount);
}
}
//Returns balance using pin
public double getBalance(int pin)
{
double balance=0;
if(pin==account1.getPIN())
{
balance= account1.getBalance();
}
else if(pin==account2.getPIN())
{
balance=account2.getBalance();
}
return balance;
}
}
------------------------------------------------------------------------------------------------------------
//ATMDriver.java
import java.util.Scanner;
public class ATMDriver
{
public static void main(String[] args)
{
//Create an instance of Scanner class
Scanner scanner=new Scanner(System.in);
//Create an instance of ATM class
ATM atm=new ATM();
int pin = 0;
double amount;
int choice;
while(true)
{
//call menu choice
choice=atm.menu();
switch (choice)
{
case 1:
System.out.println("Enter pin number");
pin=scanner.nextInt();
if(atm.authenticatePIN(pin))
System.out.println("Success");
else
System.out.println("Invalid pin");
System.out.println("Balace : "+atm.getBalance(pin));
break;
case 2:
System.out.println("Enter withdrawl amount");
amount=scanner.nextDouble();
if(amount/20==0)
System.out.println("Not increment of $20");
else
atm.withdrawl(pin, amount);
break;
case 3:
System.out.println("Enter deposit amount");
amount=scanner.nextDouble();
atm.deposit(pin, amount);
break;
default:
System.out.println("Invalid choice entered");
}
}
}
}
------------------------------------------------------------------------------------------------------------
Sample output:
1.Accoount Balance
2.Withdrawl
3.Deposit
Enter your choice
1
Enter pin number
1111
Success
Balace : 25.2
1.Accoount Balance
2.Withdrawl
3.Deposit
Enter your choice
2
Enter withdrawl amount
20
1.Accoount Balance
2.Withdrawl
3.Deposit
Enter your choice
3
Enter deposit amount
500
1.Accoount Balance
2.Withdrawl
3.Deposit
Enter your choice
1
Enter pin number
1111
Success
Balace : 505.2
1.Accoount Balance
2.Withdrawl
3.Deposit
Enter your choice
2
Enter withdrawl amount
10
1.Accoount Balance
2.Withdrawl
3.Deposit
Enter your choice
1
Enter pin number
1111
Success
Balace : 495.2
1.Accoount Balance
2.Withdrawl
3.Deposit
Enter your choice
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.