0% found this document useful (0 votes)
11 views7 pages

PF Assin 03

The document presents a Java program for an ATM simulation that includes PIN verification, balance checking, and money withdrawal functionalities. It handles errors such as invalid PIN entries, insufficient balance, and hardware/network issues. The program provides a user-friendly menu for interaction and displays appropriate messages based on user actions.

Uploaded by

dostmuhammad9990
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

PF Assin 03

The document presents a Java program for an ATM simulation that includes PIN verification, balance checking, and money withdrawal functionalities. It handles errors such as invalid PIN entries, insufficient balance, and hardware/network issues. The program provides a user-friendly menu for interaction and displays appropriate messages based on user actions.

Uploaded by

dostmuhammad9990
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

NAME:ABDULLAH REG:NO:SP24_BCS_065

ASSIGNMENT:03 SUBMITTED TO:DR.M.TAHIR

DATE:09/12/2024

CODE:
import java.util.Scanner;

public class pf_assign_3_sp24_bcs_065 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int correctPIN = 1234;


double balance = 10000.0;

boolean isHardwareWorking = true;


boolean isNetworkAvailable = true;

int pinAttempts = 0;
boolean accessGranted = false;

while (pinAttempts < 3) {


System.out.print("Enter your PIN: ");
int enteredPIN = scanner.nextInt();
if (enteredPIN == correctPIN) {
accessGranted = true;
break;
} else {
System.out.println("Invalid PIN. Try again.");
pinAttempts++;
}
}

if (!accessGranted) {
System.out.println("Too many invalid attempts. Access blocked.");
return;
}

while (true) {
System.out.println("\nATM Menu:");
System.out.println("1. Check Balance");
System.out.println("2. Withdraw Money");
System.out.println("3. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();

try {
if (!isHardwareWorking) {
throw new Exception("Hardware Error: Card reader or cash dispenser malfunction.");
}

if (!isNetworkAvailable) {
throw new Exception("Network Error: Unable to connect to the bank's central
system.");
}

switch (choice) {
case 1:

System.out.println("Your current balance is: $" + balance);


break;

case 2:

System.out.print("Enter the amount to withdraw: ");


double amount = scanner.nextDouble();

if (amount > balance) {


throw new Exception("Insufficient Balance: You cannot withdraw more than
your available balance.");
} else if (amount <= 0) {
System.out.println("Invalid amount entered.");
} else {
balance -= amount;
System.out.println("Transaction successful. Please collect your cash.");
System.out.println("Remaining balance: $" + balance);
}
break;

case 3:

System.out.println("Thank you for using the ATM. Goodbye!");


return;

default:
System.out.println("Invalid option. Please choose again.");
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
}
OUTPUT:
Enter your PIN: 2345
Invalid PIN. Try again.
Enter your PIN: 1234

ATM Menu:
1. Check Balance
2. Withdraw Money
3. Exit
Choose an option: 1
Your current balance is: $10000.0

ATM Menu:
1. Check Balance
2. Withdraw Money
3. Exit
Choose an option: 2
Enter the amount to withdraw: 2000
Transaction successful. Please collect your cash.

ATM Menu:
1. Check Balance
3. Exit
Choose an option: 3
Thank you for using the ATM. Goodbye!
PS C:\Users\hp\Desktop\javaCodes\2Darrays.java> ^C
PS C:\Users\hp\Desktop\javaCodes\2Darrays.java>
PS C:\Users\hp\Desktop\javaCodes\2Darrays.java> c:; cd 'c:\Users\hp\Desktop\javaCodes\
2Darrays.java'; & 'C:\Program Files\Java\jdk-22\bin\java.exe' '-
agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:59648' '-XX:
+ShowCodeDetailsInExceptionMessages' '-cp' 'C:\Users\hp\AppData\Roaming\Code\User\
workspaceStorage\8786173d550aeed683bee960273cc8ee\redhat.java\jdt_ws\
2Darrays.java_6ae26f3e\bin' 'pf_assign_3_sp24_bcs_065'
Enter your PIN: 1234

ATM Menu:
1. Check Balance
2. Withdraw Money
3. Exit
Choose an option: 1
Your current balance is: $10000.0

ATM Menu:
1. Check Balance
2. Withdraw Money
3. Exit
Choose an option: 2
Enter the amount to withdraw: 11000
Error: Insufficient Balance: You cannot withdraw more than your available balance.

ATM Menu:
1. Check Balance
2. Withdraw Money
3. Exit
Choose an option: 3
Thank you for using the ATM. Goodbye!
PS C:\Users\hp\Desktop\javaCodes\2Darrays.java>

You might also like