In: Computer Science
Your objective is to write a well-documented simple program using classes, a loop, and nested ifs to simulate an ATM using JAVA.
1. Create an ATM class with class variables name, pin, and balance, a constructor with parameters to assign values to the three instance variables, methods to get the name, pin, and balance, and methods to handle validated deposits and withdrawals ( deposit and withdrawal amounts cannot be negative, and withdrawal amount must not be greater than the existing balance).
2. In the ATMTest class, read the names, 4 digit pin numbers, and account balances of two customers into two instances of the ATM class. Display the two customers names, pins, and balances formatted.
3. Now that you have all your customers’ information start your ATM to accomplish the following within an infinite loop,
a). Display a welcome screen with your bank’s information and prompt for and read the customer entered pin.
b). Use a nested if to match the entered pin with one of the two customers’ pins. If the entered pin number matches that of one of the customers, then:
i. Welcome the customer by name and display the balance.
ii. Display option to 1. DEPOSIT, 2. WITHDRAW or 3. EXIT.
iii. If option 1 is selected, then use the instance deposit method to prompt for deposit amount, read, and add a valid deposit amount to the customer’s balance
iv. If option 2 is selected, then use the instance withdrawal method to subtract a valid withdrawal amount from the customers balance
v. If option 3 is selected, go to step a.
4. Should the entered pin number not match either of the two customers, notify the customer that the entered pin is not valid and go to step a.
5. Selection of the EXIT option must display welcome/login screen (step a).
6. Should an incorrect option be entered, notify the user and display the original welcome/login screen (step a).
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// ATM.java
public class ATM {
//Declaring instance variables
private String name;
private int pin;
private double balance;
//Parameterized constructor
public ATM(String name, int pin, double balance)
{
this.name = name;
this.pin = pin;
this.balance = balance;
}
// getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPin() {
return pin;
}
public void setPin(int pin) {
this.pin = pin;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
//This method will perform the deposit
operation
public void deposit(double amt)
{
this.balance+=amt;
}
//This method will perform the withdrawl
operation
public void withdrawl(double amt)
{
if(amt<=balance)
{
this.balance-=amt;
}
else
{
System.out.println("** Your balance is low **");
}
}
}
_________________________
// Test.java
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
/*
* Creating an Scanner class object which is used to
get the inputs
* entered by the user
*/
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
String name;
int pin,choice,flag=0,custIndx =
0;
double balance;
ArrayList<ATM> arl=new
ArrayList<ATM>();
for(int i=0;i<2;i++)
{
//Getting the
input entered by the user
System.out.print("Enter User#"+(i+1)+" name :");
name=sc.nextLine();
System.out.print("Enter Pin :");
pin=sc.nextInt();
System.out.print("Enter balance $:");
balance=sc.nextDouble();
ATM atm=new ATM(name, pin, balance);
arl.add(atm);
sc.nextLine();
}
while(true)
{
System.out.println("\n\n:: WELCOME TO THE LUCKY BANK ::");
while(true)
{
flag=0;
System.out.print("Enter PIN :");
pin=sc.nextInt();
for(int i=0;i<arl.size();i++)
{
if(arl.get(i).getPin()==pin)
{
flag=1;
custIndx=i;
}
}
if(flag==0)
{
System.out.println("**
Invalid Pin **");
}
else
{
break;
}
}
while(true)
{
System.out.println("1.DEPOSIT\n2. WITHDRAW\n3.
EXIT.");
System.out.print("Enter Choice :");
choice=sc.nextInt();
switch(choice)
{
case 1:{
System.out.print("Enter Amount to Deposit
:$");
double amt=getValidAmount();
arl.get(custIndx).deposit(amt);
System.out.println("Account balance after
deposit :$"+arl.get(custIndx).getBalance());
continue;
}
case 2:{
System.out.print("Enter Amount to Withdrawl
:$");
double amt=getValidAmount();
arl.get(custIndx).withdrawl(amt);
System.out.println("Account balance after
withdrawl :$"+arl.get(custIndx).getBalance());
continue;
}
case 3:{
break;
}
default :{
System.out.println("** Invalid Choice
**");
}
}
break;
}
break;
}
}
private static double getValidAmount() {
double amt;
while(true)
{
amt=sc.nextDouble();
if(amt<0)
{
System.out.println("** Invalid.Must be positive
**");
}
else
break;
}
return amt;
}
}
____________________________
Output:
Enter User#1 name :James
Enter Pin :1234
Enter balance $:45000
Enter User#2 name :Sachin
Enter Pin :3434
Enter balance $:56000
:: WELCOME TO THE LUCKY BANK ::
Enter PIN :4543
** Invalid Pin **
Enter PIN :3434
1.DEPOSIT
2. WITHDRAW
3. EXIT.
Enter Choice :1
Enter Amount to Deposit :$12000
Account balance after deposit :$68000.0
1.DEPOSIT
2. WITHDRAW
3. EXIT.
Enter Choice :2
Enter Amount to Withdrawl :$5000
Account balance after withdrawl :$63000.0
1.DEPOSIT
2. WITHDRAW
3. EXIT.
Enter Choice :3
_______________Could you plz rate me well.Thank
You