Java is a programming language and a platform. Java is a high level, robust, object-oriented and secure programming language. Java is an object-oriented, class-based, concurrent, secured and general-purpose computer-programming language. It is a widely used robust technology. if i think what we can do with java, we can make so many Web Application, Desktop GUI and many more as you guys can see below. so, i thought to make a ATM(Automated teller machine) concept . Java is a Collection of Mind map like:
OOPs, Collection, data structure, MISC etcetera.
First of all, to make ATM We should have JAVA installed in local System as well as JDK version.
ATM program Java:
In Java, we can create an ATM program for representing ATM transection. In the ATM program, the user has to select an option from the options displayed on the screen. The options are related to withdraw the money, deposit the money, check the balance, and exit.
To withdraw the money, we simply get the withdrawal amount from the user and remove that amount from the total balance and print the successful message.
To deposit the money, we simply get the deposit amount from the user, add it to the total balance and print the successful message.
To check balance, we simply print the total balance of the user.
We use the exit(0) method to exit from the current Transaction mode and return the user to the home page or initial screen.
//import required classes and packages  Â
import java.util.Scanner; Â
 Â
//create ATMExample class to implement the ATM functionality Â
public class ATMExample Â
{Â Â
    //main method starts  Â
    public static void main(String args[] ) Â
    { Â
        //declare and initialize balance, withdraw, and deposit Â
        int balance = 100000, withdraw, deposit; Â
         Â
        //create scanner class object to get choice of user Â
        Scanner sc = new Scanner(System.in); Â
         Â
        while(true) Â
        { Â
            System.out.println("Automated Teller Machine"); Â
            System.out.println("Choose 1 for Withdraw"); Â
            System.out.println("Choose 2 for Deposit"); Â
            System.out.println("Choose 3 for Check Balance"); Â
            System.out.println("Choose 4 for EXIT"); Â
            System.out.print("Choose the operation you want to perform:"); Â
             Â
            //get choice from user Â
            int choice = sc.nextInt(); Â
            switch(choice) Â
            { Â
                case 1: Â
        System.out.print("Enter money to be withdrawn:"); Â
                     Â
        //get the withdrawl money from user Â
        withdraw = sc.nextInt(); Â
                     Â
        //check whether the balance is greater than or equal to the withdrawal amount Â
        if(balance >= withdraw) Â
        { Â
            //remove the withdrawl amount from the total balance Â
            balance = balance - withdraw; Â
            System.out.println("Please collect your money"); Â
        } Â
        else Â
        { Â
            //show custom error message  Â
            System.out.println("Insufficient Balance"); Â
        } Â
        System.out.println(""); Â
        break; Â
  Â
                case 2: Â
                     Â
        System.out.print("Enter money to be deposited:"); Â
                     Â
        //get deposite amount from te user Â
        deposit = sc.nextInt(); Â
                     Â
        //add the deposit amount to the total balanace Â
        balance = balance + deposit; Â
        System.out.println("Your Money has been successfully depsited"); Â
        System.out.println(""); Â
        break; Â
  Â
                case 3: Â
        //displaying the total balance of the user Â
        System.out.println("Balance : "+balance); Â
        System.out.println(""); Â
        break; Â
  Â
                case 4: Â
        //exit from the menu Â
        System.exit(0); Â
            } Â
        } Â
    } Â
}Â Â
Output:
The Output will be like that....
Let's Move further to Exception Handling:
The Exceptional Handling in java is one of the powerful mechanism to handle the runtime errors so that the normal flow of the application can be maintained.
Exception is abnormal condition .
IN java, an exception is an event that disrupts the normal flow of the program.
Exception Handling: Exception handling is the mechanism to handle runtime errors such as ClassNotFoundException, IO Exception, SQLException, RemoteException, etc.
Advantage of Exception Handling:
The core advantage of Exception handling is to maintain the normal flow of the application.
An Exception normally disrupts the normal flow of the application that is why we use Exception Handling.
Let's see the scenario:
statement 1; Â
statement 2; Â
statement 3; Â
statement 4; Â
statement 5;//exception occurs Â
statement 6; Â
statement 7; Â
statement 8; Â
statement 9; Â
statement 10; Â
Suppose there are 10 statements in a Java program and an exception occurs at statement 5; the rest of the code will not be executed, i.e., statements 6 to 10 will not be executed. However, when we perform exception handling, the rest of the statements will be executed. That is why we use exception handling in Java.
Hierarchy of Java Exception classes:
The java.lang.Throwable class is the root class of Java Exception hierarchy inherited by two subclasses: Exception and Error. The hierarchy of Java Exception classes is given below:
Apart From that,
Last but not least is Error, our third java Exception.
Error is irrecoverable. Some example of errors are OutOfMemoryError, VirtualMachineError, AssertionError etc.
Moreover, we have 5 java Exception Keywords.
Java Exception Handling Example:
1. TRY and CATCH:
The try statement allows you to define a block of code to be tested for errors while it is being executed.
The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.
The try and catch keywords come in pairs:
Let's see an example of Java Exception Handling in which we are using a try-catch statement to handle the exception.
public class JavaExceptionExample{ Â
  public static void main(String args[]){ Â
   try{ Â
      //code that may raise exception Â
      int data=100/0; Â
   }catch(ArithmeticException e){System.out.println(e);} Â
   //rest code of the program  Â
   System.out.println("rest of the code..."); Â
  } Â
}Â Â
Output will be like that
Exception in thread main java.lang.ArithmeticException:/ by zero
rest of the code...
Common Scenarios of Java Exceptions
There are given some scenarios where unchecked exceptions may occur. They are as follows:
1) A scenario where ArithmeticException occurs
If we divide any number by zero, there occurs an ArithmeticException.
int a=50/0;//ArithmeticExceptionÂ
2) A scenario where NullPointerException occurs
If we have a null value in any variable, performing any operation on the variable throws a NullPointerException.
String s=null; Â
System.out.println(s.length());//NullPointerException Â
3) A scenario where NumberFormatException occurs
If the formatting of any variable or number is mismatched, it may result into NumberFormatException. Suppose we have a string variable that has characters; converting this variable into digit will cause NumberFormatException.
String s="abc"; Â
int i=Integer.parseInt(s);//NumberFormatException Â
4) A scenario where ArrayIndexOutOfBoundsException occurs
When an array exceeds to it's size, the ArrayIndexOutOfBoundsException occurs. there may be other reasons to occur ArrayIndexOutOfBoundsException. Consider the following statements.
int a[]=new int[5]; Â
a[10]=50; //ArrayIndexOutOfBoundsException Â
2. Finally:
The finally statement lets you execute code, after try...catch, regardless of the result:
Example
public class Main {
public static void main(String[] args) {
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
} finally {
System.out.println("The 'try catch' is finished.");
}
}
}
The output will be:
Something went wrong.
The 'try catch' is finished.
3. The throw keyword:
The throw statement allows you to create a custom error.
The throw statement is used together with an exception type.
All methods use the throw statement to throw an exception. The throw statement requires a single argument: a throwable object. Throwable objects are instances of any subclass of the Throwable class. Here's an example of a throw statement.
throw someThrowableObject;
Example
Throw an exception if age is below 18 (print "Access denied"). If age is 18 or older, print "Access granted":
public class Main {
static void checkAge(int age) {
if (age < 18) {
throw new ArithmeticException("Access denied - You must be at least 18 years old.");
}
else {
System.out.println("Access granted - You are old enough!");
}
}
public static void main(String[] args) {
checkAge(15);Â // Set age to 15 (which is below 18...)
}
}
he output will be:
Exception in thread "main" java.lang.ArithmeticException: Access denied - You must be at least 18 years old.
at Main.checkAge(Main.java:4)
at Main. Main(Main.java:12)
I hope You guys enjoy this tutorial.....
Thanks for Reading...
References: