Skip to content

added command lines to ATM #474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions ATM/ATM.java
Original file line number Diff line number Diff line change
@@ -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
}
}
}
30 changes: 22 additions & 8 deletions ATM/Account.java
Original file line number Diff line number Diff line change
@@ -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: ");
Expand All @@ -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: ");
Expand All @@ -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: ");
Expand All @@ -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: ");
Expand Down
77 changes: 45 additions & 32 deletions ATM/OptionMenu.java
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> data = new HashMap<>();
Scanner menuInput = new Scanner(System.in); // Scanner object for reading user input
DecimalFormat moneyFormat = new DecimalFormat("'$'###,##0.00"); // Currency formatter

HashMap<Integer, Integer> 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");
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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)
}
}
}
}
}