In: Computer Science
Java
Requirements: The ATM system will require users to enter a valid ID . If the entered ID matches to one of the Accounts’ ID, the system will present the user an ATM menu. The ATM menu asks the user to enter a choice, and depending on the user’s choice it may ask for additional input (i.e., withdraw or deposit amount). The code should show for each user choice as shown in the sample-run figure at the end. Description of the sample-run figure (shown at the end): In the main method, create an Array of ten accounts with account IDs as 1, . . . , 10, and an initial balance of $100 for each account. You will use this Array to simulate an ATM machine experience in the main method. To do that, you need a Scanner to capture the user’s input and define a few variables. You should also define a few methods in the main class, to be used inside the main method, to make the main method looks lean. Additional simulation requirements: After creating the 10-element array, the system should prompt the user to enter an ID and it will verify if the ID is a valid one. An ID is valid if it matches to one of the accounts of the 10-element Array. If the ID is valid, the ATM menu is displayed; otherwise the system will ask the user to enter a correct ID. That is, the system will not present the ATM main menu until the user enters a correct ID. Based on the above ATM simulation description, once the system starts it will not stop because after a user chooses option 4 to exit the ATM system will ask user to enter an ID again. To give users an option exiting the system after a few rounds of ATM simulation, inform users that when entering an ID, enter 0 to exit the ATM run (since valid ids are from 1 to 10). That is, if users enter 0 as ID, instead of showing the menu, the system will terminate the project, but before that it will print out every ATM transactions for each account in the Array that actually incurred transactions.
I have written the java code for the atm machine. I have created just 3 users and have given passwords for their accounts and initial limit of $100. There are options added for the user to add and retireve money from the bank accounts.
working java code:
import java.util.*;
class ATM {
public static Scanner sc = new Scanner(System.in);
public static String user_account(String acctNum, String pwd)
{
String result = "error";
// this is to store account information and password with
initial amount of $100
String a = "1 pass 100";
String b = "2 pass 100";
String c = "3 pass 100";
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 user_choice;
do
{
System.out.print("\nPlease Choose From the Following
Options:"
+ "\n 1. Check Balance \n 2. Add Money"
+ "\n 3. Get Money\n 4. Log Out\n\n");
user_choice = sc.nextInt();
if (user_choice < 1 || user_choice > 4){
System.out.println("error");
}
}while (user_choice < 1 || user_choice > 4);
return user_choice;
}
public static void check_balance(double x)
{
System.out.printf("\nYour Current Balance is $%.2f\n", x);
}
public static double add_money(double x, double y)
{
double add_moneyAmt = y, account_balance = x;
double newBalance = add_moneyAmt + account_balance;
System.out.printf("Your New Balance is $%.2f\n", newBalance);
return newBalance;
}
public static double get_money(double x, double y)
{
double get_moneyAmt = y, account_balance = x, newBalance;
newBalance = account_balance - get_moneyAmt;
System.out.printf("Your New Balance is %.2f\n",newBalance);
return newBalance;
}
public static void main(String[] args) {
String accNum, pass, origBal = "error";
int count = 0, menuOption = 0;
double add_moneyAmt = 0, get_moneyAmt = 0, account_balance=0;
boolean foundNonDigit;
do{
foundNonDigit = false;
System.out.println("Please Enter Your Account Number: ");
accNum = sc.next();
System.out.println("Enter Your Password: ");
pass = sc.next();
origBal = user_account(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("\nYour New Balance is: $ "+ origBal);
}
else
System.out.println(origBal);
}while(origBal.equals("error"));
account_balance=Double.parseDouble(origBal);
while (menuOption != 4)
{
menuOption=menu();
switch (menuOption)
{
case 1:
check_balance(account_balance);
break;
case 2:
System.out.print("\nEnter Amount You Wish to add_money: $ ");
add_moneyAmt = sc.nextDouble();
account_balance=add_money(add_moneyAmt, account_balance);
break;
case 3:
System.out.print("\nEnter Amount You Wish to get_moneyl: $
");
get_moneyAmt = sc.nextDouble();
while(get_moneyAmt>account_balance){
System.out.print("ERROR: INSUFFICIENT FUNDS!! "
+ "PLEASE ENTER A DIFFERENT AMOUNT: $");
get_moneyAmt = sc.nextDouble();
}
account_balance = get_money(account_balance,
get_moneyAmt);
break;
case 4:
System.out.print("\nThanks for visiting. Enjoy ur day");
System.exit(0);
break;
}
}
}
}
Input given
1
pass
4
Output: