package Main;
This line declares that the class belongs to the Main package. Packages are used in Java to
organize related classes and avoid naming conflicts.
import AtmAccount.*;
This imports all classes from the AtmAccount package. Because of this import statement, the
program can use classes such as AccountName and ATMsimulation without writing their full
package names.
public class ATMmain
This creates a class named ATMmain. It serves as the main controller of the ATM program
because it contains the main() method.
public static void main(String[] args)
This is the main method, which is the starting point of every Java application. When the
program runs, Java executes the code inside this method first.
AccountName account = new AccountName("Group 4", "1234");
This line creates an object named account from the AccountName class.
"Group 4" is the username.
"1234" is the password.
These values are passed to the constructor of the AccountName class and become the account's
login credentials.
boolean loggedIn = [Link]();
This line calls the login() method from the AccountName object.
The method checks whether the user entered the correct username and password. The result is
stored in the variable loggedIn.
true = login successful
false = login failed
if (loggedIn)
This is an if statement that checks the value of loggedIn.
If the value is true, the code inside the curly braces {} will execute. If it is false, the program
skips the block and ends.
ATMsimulation simulation = new ATMsimulation();
If login is successful, this line creates an object named simulation from the ATMsimulation
class.
This object contains the ATM functions such as:
Check Balance
Deposit Money
Withdraw Money
Exit Transaction
[Link]();
This line calls the start() method of the ATMsimulation class.
The start() method launches the ATM menu and allows the user to perform ATM transactions.
Explanation Per Line
package AtmAccount;
This declares that the class belongs to the AtmAccount package. Packages help organize Java
classes into groups.
import [Link];
This imports the Scanner class, which is used to receive input from the keyboard.
public class AccountName
This creates a class named AccountName. It is responsible for storing account information and
handling the login process.
String name, pin;
These are instance variables:
name stores the account holder's name.
pin stores the account PIN.
Example:
name = "Group 4";
pin = "1234";
Constructor
public AccountName(String name, String pin) {
[Link] = name;
[Link] = pin;
}
The constructor initializes the account information when an object is created.
Example:
AccountName account = new AccountName("Group 4", "1234");
After execution:
name = "Group 4";
pin = "1234";
The keyword this refers to the current object's variables.
Login Method
public boolean login()
This method checks whether the entered PIN is correct.
It returns:
true if login is successful
false if login fails
Scanner Creation
Scanner sc = new Scanner([Link]);
Creates a Scanner object to read user input from the keyboard.
Attempts Counter
int attempts = 0;
Tracks how many incorrect PIN attempts have been made.
While Loop
while (attempts < 3)
The user is allowed a maximum of 3 attempts to enter the correct PIN.
The loop continues as long as attempts are less than 3.
Ask for PIN
[Link]("Enter PIN: ");
Displays:
Enter PIN:
and waits for user input.
Check PIN
if ([Link]().equals(pin))
[Link]() reads the entered PIN.
.equals(pin) compares the entered PIN with the stored PIN.
If they are the same:
[Link]("Welcome, " + name + "!");
return true;
Displays:
Welcome, Group 4!
and returns true, meaning login is successful.
Wrong PIN
If the PIN is incorrect:
attempts++;
Adds 1 to the number of failed attempts.
[Link]("Wrong PIN! " + (3 - attempts) + " attempt(s) left.");
Shows the remaining attempts.
Example:
Wrong PIN! 2 attempt(s) left.
Account Locked
If the user enters the wrong PIN three times:
[Link]("Account locked!");
return false;
Displays:
Account locked!
and returns false, meaning login failed.
Detailed Explanation of the ATMsimulation Class
package AtmAccount;
import [Link];
public class ATMsimulation {
package AtmAccount;
This places the class inside the AtmAccount package.
import [Link];
Imports the Scanner class, which allows the program to receive input from the keyboard.
public class ATMsimulation
This creates the ATMsimulation class, which contains all ATM transactions such as checking
balance, depositing money, withdrawing money, and exiting the system.
Start Method
public void start() {
This method starts the ATM simulation after the user successfully logs in.
Scanner Object
Scanner scanner = new Scanner([Link]);
Creates a Scanner object to read user input.
Initial Balance
double balance = 1500;
Sets the initial account balance to PHP 1,500.
Example:
Current Balance = PHP 1500
Choice Variable
int choice;
Stores the menu option selected by the user.
ATM Header
[Link]("ATM MACHINE");
Displays:
ATM MACHINE
Do-While Loop
do {
The ATM menu repeats continuously until the user chooses Exit (4).
Display Menu
[Link]("\n[1] Check Balance");
[Link]("[2] Deposit");
[Link]("[3] Withdraw");
[Link]("[4] Exit");
Displays:
[1] Check Balance
[2] Deposit
[3] Withdraw
[4] Exit
Get User Choice
[Link]("Enter choice: ");
choice = [Link]();
The program asks the user to select an option and stores it in the choice variable.
Option 1: Check Balance
if (choice == 1) {
[Link]("Your balance is: PHP " + balance);
}
If the user enters 1, the program displays the current balance.
Example:
Your balance is: PHP 1500.0
Option 2: Deposit
else if (choice == 2)
If the user enters 2, they can deposit money.
Ask for Deposit Amount
[Link]("Enter deposit amount: PHP ");
double amount = [Link]();
Example:
Enter deposit amount: PHP 500
Validate Deposit
if (amount > 0)
Checks if the deposit amount is positive.
Successful Deposit
balance += amount;
Adds the deposited amount to the current balance.
Example:
Balance = 1500
Deposit = 500
New Balance = 2000
Displays:
Deposit successful!
New balance: PHP 2000.0
Invalid Deposit
If the amount is zero or negative:
[Link]("Invalid amount.");
Option 3: Withdraw
else if (choice == 3)
Allows the user to withdraw money.
Ask Withdrawal Amount
[Link]("Enter withdrawal amount: PHP ");
double amount = [Link]();
Example:
Enter withdrawal amount: PHP 300
Check for Insufficient Funds
if (amount > balance)
If the withdrawal amount exceeds the current balance:
Insufficient funds.
Example:
Balance = PHP 1500
Withdraw = PHP 2000
Result:
Insufficient funds.
Check Invalid Amount
else if (amount <= 0)
If the amount is zero or negative:
Invalid amount.
Successful Withdrawal
balance -= amount;
Subtracts the withdrawal amount from the balance.
Example:
Balance = PHP 1500
Withdraw = PHP 300
New Balance = PHP 1200
Displays:
Withdrawal successful!
New balance: PHP 1200.0
Option 4: Exit
else if (choice == 4)
If the user enters 4, the program exits.
Displays:
[Link]("Thank you for using JAM'S BANK. Goodbye!");
Output:
Thank you for using JAM'S BANK. Goodbye!
Invalid Menu Choice
else {
[Link]("Invalid choice. Please enter 1-4.");
}
If the user enters a number other than 1, 2, 3, or 4:
Invalid choice. Please enter 1-4.
Loop Condition
} while (choice != 4);
The menu continues to appear until the user selects 4 (Exit).
Close Scanner
[Link]();
Closes the Scanner object and frees system resources.