ATM Test w/Array Lists

/*

  • Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
  • Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
  • Name: Carter Hendrick
  • Course Name: IT_205_Object Oriented Programming w/ Java
  • Date: 10/14/2025
  • Pledge: This program is entirely written by me (Carter Hendrick) with the exception of the starting point provided by the professor
    / /

/ package atmtest; import java.util.*;

public class ATMTest {
public static void main(String[] args) {

    //declare some variables
    Scanner input = new Scanner(System.in);

    //declare an array list of customers
    ArrayList<ATM> customers = new ArrayList<>();

    String firstName;
    double balance;
    int pin;

    for(int count = 0; count<3; count++){
        System.out.println("CUSTOMER #"+count);

        System.out.print("Enter one word name of customer: ");
        firstName = input.next();

        System.out.print("Enter your customers pin: ");
        pin = input.nextInt();

        System.out.print("Enter your customers balance: ");
        balance = input.nextDouble(); 
        System.out.println("END OF CUSTOMER #"+count+"\n");

    //create an instance of ATM
    ATM customer1 = new ATM(firstName, pin, balance);

    customers.add(customer1);  //added an instance of ATM to the arraylist

    }//end of for loop creating arraylist

    //output list of customers

    for(int count = 0; count<customers.size(); count++){

    System.out.printf("%10s%5d%10.2f%n",
            customers.get(count).getName(),
            customers.get(count).getPin(),
            customers.get(count).getBalance());
    }//end of customer output for loop



    //SAY THAT THE INDEX OF THE ELEMENT WHOSE PIN MATCHED WAS 
   // int index = 2;
    //how to call the deposit method
    //customers.get(index).deposit(103);
   //  System.out.println("Your new balance is "+
           //  customers.get(index).getBalance());






    //start infinite while loop
     while(true){
      //prompt and read somepin
      System.out.println("Welcome to my Bank");
      System.out.print("Enter your pin: ");
      //read the pin
      int somePin = input.nextInt();
      System.out.println("You entered "+somePin);
   /* USE A FOR LOOP AND DETERMINE THE INDEX OF THE CUSTOMER WHOSE PIN MATCHES
    THE INPUT PIN
    INSIDE THIS LOOP HAVE A BOOL VARIABLE THAT TURNS TRUE IF THERE IS A MATCH
    IF NO MATCH GO TO TOP OF LOOP -CONTINUE
   */
   int Index = 0;
   boolean pinMatch = false;
   for (int i = 0; i < customers.size(); i++)
   {
       if (customers.get(i).equals(somePin))
       {
           Index = i;
           pinMatch = true;
           break;
       }else{
           continue;
       }
   }
    if (pinMatch = true) {
        System.out.println("PIN matched! Customer found at index: " + Index);
        System.out.println("Customer Name: " + customers.get(Index).getName());
        System.out.println("Customer Balance: " + customers.get(Index).getBalance());
    } else {
        System.out.println("No customer found with the given PIN.");
    }

    //output menu -deposit, withdraw,  or exit
    if(pinMatch = true){
     System.out.printf(" %s%s%n", "Welcome ",
             customers.get(Index).getName());
     System.out.printf(" %s%s%n"," balance: $",
             customers.get(Index).getBalance());
     balance = customers.get(Index).getBalance();
     boolean exit = false;
     while (!exit) {
         System.out.println("1. Withdraw");
         System.out.println("2. Deposit");
         System.out.println("3. Exit");
         System.out.println("Please select an option: ");
         int option = input.nextInt();
         switch (option) {
             case 1 -> { 
                 //withdraw
                 System.out.print("Enter amount to withdraw: ");
                 double withdrawAmount = input.nextDouble();
                 customers.get(Index).withdraw(withdrawAmount);
                 System.out.printf(" %s%s%n"," balance: $",
                        customers.get(Index).getBalance());
             }
             case 2 -> { 
                 //Deposit
                 System.out.print("Enter amount to deposit: ");
                 double depositAmount = input.nextDouble();
                 customers.get(Index).deposit(depositAmount);
                 System.out.printf(" %s%s%n"," balance: $",
                         customers.get(Index).getBalance());
             }
             case 3 -> {
                 exit = true;
                break;
             }
             default -> {
                 System.out.print("invalid choice, please try again");
             }
         }
     }
    }
     }
}
public static void main(String[] args) {

    //declare some variables
    Scanner input = new Scanner(System.in);

    //declare an array list of customers
    ArrayList<ATM> customers = new ArrayList<>();

    String firstName;
    double balance;
    int pin;

    for(int count = 0; count<3; count++){
        System.out.println("CUSTOMER #"+count);

        System.out.print("Enter one word name of customer: ");
        firstName = input.next();

        System.out.print("Enter your customers pin: ");
        pin = input.nextInt();

        System.out.print("Enter your customers balance: ");
        balance = input.nextDouble(); 
        System.out.println("END OF CUSTOMER #"+count+"\n");

    //create an instance of ATM
    ATM customer1 = new ATM(firstName, pin, balance);

    customers.add(customer1);  //added an instance of ATM to the arraylist

    }//end of for loop creating arraylist

    //output list of customers

    for(int count = 0; count<customers.size(); count++){

    System.out.printf("%10s%5d%10.2f%n",
            customers.get(count).getName(),
            customers.get(count).getPin(),
            customers.get(count).getBalance());
    }//end of customer output for loop



    //SAY THAT THE INDEX OF THE ELEMENT WHOSE PIN MATCHED WAS 
   // int index = 2;
    //how to call the deposit method
    //customers.get(index).deposit(103);
   //  System.out.println("Your new balance is "+
           //  customers.get(index).getBalance());






    //start infinite while loop
     while(true){
      //prompt and read somepin
      System.out.println("Welcome to my Bank");
      System.out.print("Enter your pin: ");
      //read the pin
      int somePin = input.nextInt();
      System.out.println("You entered "+somePin);
   /* USE A FOR LOOP AND DETERMINE THE INDEX OF THE CUSTOMER WHOSE PIN MATCHES
    THE INPUT PIN
    INSIDE THIS LOOP HAVE A BOOL VARIABLE THAT TURNS TRUE IF THERE IS A MATCH
    IF NO MATCH GO TO TOP OF LOOP -CONTINUE
   */
   int Index = 0;
   boolean pinMatch = false;
   for (int i = 0; i < customers.size(); i++)
   {
       if (customers.get(i).equals(somePin))
       {
           Index = i;
           pinMatch = true;
           break;
       }else{
           continue;
       }
   }
    if (pinMatch = true) {
        System.out.println("PIN matched! Customer found at index: " + Index);
        System.out.println("Customer Name: " + customers.get(Index).getName());
        System.out.println("Customer Balance: " + customers.get(Index).getBalance());
    } else {
        System.out.println("No customer found with the given PIN.");
    }

    //output menu -deposit, withdraw,  or exit
    if(pinMatch = true){
     System.out.printf(" %s%s%n", "Welcome ",
             customers.get(Index).getName());
     System.out.printf(" %s%s%n"," balance: $",
             customers.get(Index).getBalance());
     balance = customers.get(Index).getBalance();
     boolean exit = false;
     while (!exit) {
         System.out.println("1. Withdraw");
         System.out.println("2. Deposit");
         System.out.println("3. Exit");
         System.out.println("Please select an option: ");
         int option = input.nextInt();
         switch (option) {
             case 1 -> { 
                 //withdraw
                 System.out.print("Enter amount to withdraw: ");
                 double withdrawAmount = input.nextDouble();
                 customers.get(Index).withdraw(withdrawAmount);
                 System.out.printf(" %s%s%n"," balance: $",
                        customers.get(Index).getBalance());
             }
             case 2 -> { 
                 //Deposit
                 System.out.print("Enter amount to deposit: ");
                 double depositAmount = input.nextDouble();
                 customers.get(Index).deposit(depositAmount);
                 System.out.printf(" %s%s%n"," balance: $",
                         customers.get(Index).getBalance());
             }
             case 3 -> {
                 exit = true;
                break;
             }
             default -> {
                 System.out.print("invalid choice, please try again");
             }
         }
     }
    }
     }
}
}

ATM methods

/*

  • Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
  • Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
  • Name: Carter Hendrick
  • Course Name: IT_205_Object Oriented Programming w/ Java
  • Date: 10/14/2025
  • Pledge: This program is entirely written by me (Carter Hendrick) without help from anyone
    */

/**
*

  • @author hendr
    / /
    ATM class with instance members and methods
    */
package atmtest;
public class ATM {
//declare the instance variables
String firstName;
//others such as pin and balance
int pin;
double balance;

//construtor with parameters
public ATM(String someFirstName, int somePin, double someBalance){
    this.firstName = someFirstName;
    this.pin = somePin;
    this.balance = someBalance;
}//end of constructor

//get and set methods

//get method for the name
public String getName(){
    return this.firstName;
}//end of getName

public double getBalance(){
    return this.balance;
}

public int getPin(){
    return this.pin;
}

//create a public method to deposit

public void deposit(double depAmt){
    if(depAmt>=0){
        this.balance+=depAmt;
    }//end of if

    }

//create a public method to deposit

public void withdraw(double depAmt){
    if(depAmt>=0){
        this.balance-=depAmt;
    }//end of if
}
}//end of ATM

Leave a Reply

Your email address will not be published.