0% found this document useful (0 votes)
14 views3 pages

Java ATM Simulation Program Guide

The document outlines a Java program simulating an ATM machine with functionalities such as checking balance, depositing, and withdrawing money. It includes exception handling for insufficient funds and invalid inputs using try, catch, and finally blocks to ensure smooth operation. The program prompts users for choices and handles errors gracefully while allowing continuous interaction.
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)
14 views3 pages

Java ATM Simulation Program Guide

The document outlines a Java program simulating an ATM machine with functionalities such as checking balance, depositing, and withdrawing money. It includes exception handling for insufficient funds and invalid inputs using try, catch, and finally blocks to ensure smooth operation. The program prompts users for choices and handles errors gracefully while allowing continuous interaction.
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

Practical no: 04

Develop a Java program for simulation of any real time application with required functionalities.
For eg. ATM machine with functionalities like checking account balance, withdrawing, and
depositing money. Use try, catch, and finally blocks to handle potential exceptions such as
insufficient funds (throwing ArithmeticException) and invalid input (throwing
IllegalArgumentException). Ensure that the application continues to run smoothly after handling
exceptions

import [Link]; }

class ATM { public class SimpleATMSim {


private double bal; public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
public ATM(double initial) { ATM atm = new ATM(5000);
if (initial < 0) throw new boolean run = true;
IllegalArgumentException("Initial balance
negative"); [Link]("=== Simple ATM ===");
[Link] = initial;
} while (run) {
try {
public double getBalance() { [Link]("\n1. Check Balance");
return bal; [Link]("2. Deposit");
} [Link]("3. Withdraw");
[Link]("4. Exit");
public void deposit(double amt) { [Link]("Enter choice: ");
if (amt <= 0) throw new
IllegalArgumentException("Deposit must be > String line = [Link]().trim();
0"); int choice;
bal += amt; try {
[Link]("Deposited: " + amt); choice = [Link](line);
} } catch (NumberFormatException e) {
throw new IllegalArgumentException("Enter
public void withdraw(double amt) { number 1-4");
if (amt <= 0) throw new }
IllegalArgumentException("Withdraw must be >
0"); switch (choice) {
if (amt > bal) throw new case 1:
ArithmeticException("Insufficient funds"); [Link]("Balance: " +
bal -= amt; [Link]());
[Link]("Withdrawn: " + amt); break;
} case 2:
[Link]("Enter amount to deposit: "); }
double dAmt = readAmount(sc);
[Link](dAmt);
break;
case 3:
[Link]("Enter amount to withdraw:
");
double wAmt = readAmount(sc);
[Link](wAmt);
break;
case 4:
run = false;
[Link]("Goodbye!");
break;
default:
throw new IllegalArgumentException("Choice
must be 1-4");
}
} catch (ArithmeticException ae) {
[Link]("Error: " + [Link]());
} catch (IllegalArgumentException ie) {
[Link]("Invalid: " +
[Link]());
} catch (Exception e) {
[Link]("Unexpected error: " +
[Link]());
} finally {
if (run) [Link]("Back to menu...");
}
}
[Link]();
}

private static double readAmount(Scanner sc) {


String s = [Link]().trim();
try {
return [Link](s);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Enter
valid amount");
}
}
Output:

You might also like