From f3e0d6928f41799970c37f5d77b27df28c9adf00 Mon Sep 17 00:00:00 2001 From: SR PRAWIN RAJA Date: Tue, 8 Jul 2025 12:15:43 +0530 Subject: [PATCH] added command lines to ATM --- ATM/ATM.java | 11 +++---- ATM/Account.java | 30 +++++++++++++----- ATM/OptionMenu.java | 77 ++++++++++++++++++++++++++------------------- 3 files changed, 72 insertions(+), 46 deletions(-) diff --git a/ATM/ATM.java b/ATM/ATM.java index fdffd1ca..0bcb5418 100644 --- a/ATM/ATM.java +++ b/ATM/ATM.java @@ -1,7 +1,6 @@ - -public class ATM extends OptionMenu { - public static void main(String[] args) { - OptionMenu options = new OptionMenu(); - options.getLogin(); +public class ATM extends OptionMenu { // Declares ATM class that inherits from OptionMenu + public static void main(String[] args) { // Main method: entry point of the program + OptionMenu options = new OptionMenu(); // Creates an object of OptionMenu class + options.getLogin(); // Calls the getLogin method to prompt user login } -} \ No newline at end of file +} diff --git a/ATM/Account.java b/ATM/Account.java index 04f5dd0e..950f609c 100644 --- a/ATM/Account.java +++ b/ATM/Account.java @@ -1,56 +1,67 @@ -import java.text.DecimalFormat; -import java.util.Scanner; +import java.text.DecimalFormat; // Import for formatting numbers into currency format +import java.util.Scanner; // Import for taking user input from the console public class Account { - private int customerNumber; - private int pinNumber; - private double checkingBalance = 0; - private double savingBalance = 0; + private int customerNumber; // Stores customer number + private int pinNumber; // Stores customer's PIN + private double checkingBalance = 0; // Stores checking account balance + private double savingBalance = 0; // Stores saving account balance - Scanner input = new Scanner(System.in); - DecimalFormat moneyFormat = new DecimalFormat("'$'###,##0.00"); + Scanner input = new Scanner(System.in); // Scanner object for user input + DecimalFormat moneyFormat = new DecimalFormat("'$'###,##0.00"); // Format to display currency values + // Sets the customer number public void setCustomerNumber(int customerNumber){ this.customerNumber = customerNumber; } + // Returns the customer number public int getCustomerNumber(){ return customerNumber; } + // Sets the PIN number public void setPinNumber(int pinNumber){ this.pinNumber = pinNumber; } + // Returns the PIN number public int getPinNumber(){ return pinNumber; } + // Returns the checking account balance public double getCheckingBalance() { return checkingBalance; } + // Returns the saving account balance public double getSavingBalance(){ return savingBalance; } + // Deducts the amount from checking account public void calcCheckingWithdraw(double amount){ checkingBalance = (checkingBalance - amount); } + // Deducts the amount from saving account public void calcSavingWithdraw(double amount){ savingBalance = (savingBalance - amount); } + // Adds the amount to checking account public void calcCheckingDeposit(double amount){ checkingBalance = (checkingBalance + amount); } + // Adds the amount to saving account public void calcSavingDeposit(double amount){ savingBalance = (savingBalance + amount); } + // Prompts user to withdraw money from checking account public void getCheckingWithdrawInput() { System.out.println("Checking Account balance: " + moneyFormat.format(checkingBalance)); System.out.print("Amount you want to withdraw from Checking Account: "); @@ -65,6 +76,7 @@ public void getCheckingWithdrawInput() { } } + // Prompts user to withdraw money from saving account public void getSavingWithdrawInput() { System.out.println("Saving Account balance: " + moneyFormat.format(savingBalance)); System.out.print("Amount you want to withdraw from Saving Account: "); @@ -79,6 +91,7 @@ public void getSavingWithdrawInput() { } } + // Prompts user to deposit money into checking account public void getCheckingDepositInput(){ System.out.println("Checking Account Balance: " + moneyFormat.format(checkingBalance)); System.out.print("Amount you want to deposit to Checking Account: "); @@ -93,6 +106,7 @@ public void getCheckingDepositInput(){ } } + // Prompts user to deposit money into saving account public void getSavingDepositInput(){ System.out.println("Saving Account Balance: " + moneyFormat.format(savingBalance)); System.out.print("Amount you want to deposit to Saving Account: "); diff --git a/ATM/OptionMenu.java b/ATM/OptionMenu.java index 5658dda9..09f18db0 100644 --- a/ATM/OptionMenu.java +++ b/ATM/OptionMenu.java @@ -1,44 +1,50 @@ -import java.text.DecimalFormat; -import java.util.HashMap; -import java.util.Scanner; +import java.text.DecimalFormat; // Import for formatting balance outputs as currency +import java.util.HashMap; // Import for storing customer number and PIN pairs +import java.util.Scanner; // Import for capturing user input from the console public class OptionMenu extends Account { - - Scanner menuInput = new Scanner(System.in); - DecimalFormat moneyFormat = new DecimalFormat("'$'###,##0.00"); - HashMap data = new HashMap<>(); + Scanner menuInput = new Scanner(System.in); // Scanner object for reading user input + DecimalFormat moneyFormat = new DecimalFormat("'$'###,##0.00"); // Currency formatter + HashMap data = new HashMap<>(); // Stores customer number as key, PIN as value + + // Handles login process public void getLogin() { int x = 1; - do{ - try{ + do { + try { + // Predefined dummy accounts data.put(952141, 191904); data.put(989947, 717976); System.out.println("Welcome to ATM"); System.out.println("Enter your Customer Number"); - setCustomerNumber(menuInput.nextInt()); + setCustomerNumber(menuInput.nextInt()); // Reads and sets customer number System.out.println("Enter your PIN Number"); - setPinNumber(menuInput.nextInt()); + setPinNumber(menuInput.nextInt()); // Reads and sets PIN } - catch(Exception e){ + catch(Exception e) { + // Handles non-numeric input System.out.println("\nInvalid Characters Only Numbers Allowed\n" + e); - x = 2; + x = 2; // Prevents loop continuation on error } int cn = getCustomerNumber(); int pn = getPinNumber(); - if(data.containsKey(cn) && data.get(cn) == pn){ - getAccountType(); + + // Validates login credentials + if (data.containsKey(cn) && data.get(cn) == pn) { + getAccountType(); // Proceed to account menu } - else{ + else { System.out.println("\nWrong Customer Number or Wrong PIN Number\n\n"); } - }while(x == 1); + } while (x == 1); // Loop continues until valid login or exception } + // Displays account type options public void getAccountType() { System.out.println("Select Account Type you want to Access"); System.out.println("Type 1 - Checking Account"); @@ -47,14 +53,16 @@ public void getAccountType() { int selection = menuInput.nextInt(); + // Handles user selection switch (selection) { - case 1 -> getChecking(); - case 2 -> getSaving(); - case 3 -> System.out.println("Thank you for using ATM, BYE\n"); - default -> System.out.println("\n Invalid Choice \n"); + case 1 -> getChecking(); // Redirects to checking account menu + case 2 -> getSaving(); // Redirects to saving account menu + case 3 -> System.out.println("Thank you for using ATM, BYE\n"); // Exit message + default -> System.out.println("\n Invalid Choice \n"); // Handles invalid input } } + // Menu for checking account options public void getChecking() { System.out.println("Checking Account"); System.out.println("Type 1 - View Balance"); @@ -64,27 +72,30 @@ public void getChecking() { int selection = menuInput.nextInt(); + // Handles user selection switch (selection) { case 1 -> { + // Display balance System.out.println("Checking Account Balance: " + moneyFormat.format(getCheckingBalance())); - getAccountType(); + getAccountType(); // Return to main menu } case 2 -> { - getCheckingWithdrawInput(); + getCheckingWithdrawInput(); // Perform withdrawal getAccountType(); } case 3 -> { - getCheckingDepositInput(); + getCheckingDepositInput(); // Perform deposit getAccountType(); } - case 4 -> System.out.println("Thank you for using ATM, Bye"); + case 4 -> System.out.println("Thank you for using ATM, Bye"); // Exit message default -> { System.out.println("\nInvalid Choice\n"); - getChecking(); + getChecking(); // Re-prompt on invalid input } } } + // Menu for saving account options public void getSaving() { System.out.println("Saving Account"); System.out.println("Type 1 - View Balance"); @@ -95,24 +106,26 @@ public void getSaving() { int selection = menuInput.nextInt(); + // Handles user selection switch (selection) { case 1 -> { + // Display balance System.out.println("Saving Account Balance: " + moneyFormat.format(getSavingBalance())); - getAccountType(); + getAccountType(); // Return to main menu } case 2 -> { - getSavingWithdrawInput(); + getSavingWithdrawInput(); // Perform withdrawal getAccountType(); } case 3 -> { - getSavingDepositInput(); + getSavingDepositInput(); // Perform deposit getAccountType(); } - case 4 -> System.out.println("Thank you for using ATM, Bye\n"); + case 4 -> System.out.println("Thank you for using ATM, Bye\n"); // Exit message default -> { System.out.println("\nInvalid Choice\n"); - getChecking(); + getChecking(); // Re-prompt (note: this may be intended to redirect to saving, consider reviewing) } } } -} \ No newline at end of file +}