Friday, October 16, 2015

Latest Banking Application (All except Persistence)

Account.java

import java.util.*;
import java.io.*;
public class Account implements Serializable{
private int accountId;
private String holderName;
private double balance;
private Date creationDate;
public static final String TRA_DEPOSIT = "deposit";
public static final String TRA_WITHDRAW = "withdraw";

ArrayList<Transaction> passbook = new ArrayList<Transaction>();

Account(String holderName, int accountId, double balance){
this.holderName = holderName;
this.accountId = accountId;
this.balance = balance;
this.creationDate = new Date();
}
Account(String holderName, int accountId){
this(holderName, accountId, 0);
}

public void setAccountId(int id){
this.accountId = id;
}
public int getAccountId(){
return this.accountId;
}
public void setName(String name){
this.holderName = name;
}
public String getName(){
return this.holderName;
}
public double getBalance(){
return this.balance;
}
public String toString(){
return "\nAccount No. :"+this.accountId+"\nHolder Name: "+this.holderName+"\nCurrent Balance: "+this.balance+"\n";
}
public boolean deposit(double amt){
if(amt<=0){
return false;
}
else{
this.balance += amt;
Account.Transaction t1 = this.new Transaction(amt, Account.TRA_DEPOSIT);
this.passbook.add(t1);
return true;
}
}
public boolean withdraw(double amt){
if(amt<=0){
return false;
}
else if(amt>balance){
return false;
}
else{
this.balance -= amt;
Account.Transaction t1 = this.new Transaction(amt, Account.TRA_WITHDRAW);
this.passbook.add(t1);
return true;
}
}

public class Transaction{
double amt;
String type;
Date transactionDate;

public Transaction(double amt,String type){
this.amt = amt;
this.type = type;
this.transactionDate = new Date();
}
public String toString(){
return "Amount: "+ ((this.type.equals(Account.TRA_DEPOSIT))?"+":"-") + this.amt+" \nTransaction Date: "+ transactionDate;
}

}

boolean printPassbook(){
System.out.println(this+"\n----------------------------------\n");
for(Transaction t : this.passbook){
System.out.println(t+"\n\n");
}
return true;
}
}



Bank.java

import java.util.*;
public class Bank{
String name;
int nextAccountId = 1001;
ArrayList<Account> accounts = new ArrayList<Account>();

Bank(String name){
this.name = name;
}

Account createAccount(String holderName, double balance){
Account a1 = new Account(holderName, nextAccountId++, balance);
this.accounts.add(a1);
return a1;
}

double getAccountBalance(int accountId){
Account a = findAccount(accountId);
if(a==null){
return -1;
}
else{
return a.getBalance();
}
}
boolean removeAccount(int accountId){
for(int i = 0; i < accounts.size(); i++){
if(accounts.get(i).getAccountId() == accountId){
accounts.remove(i);
return true;
}
}
return false;
}
boolean deposit(int accountId, double amt){
Account a = findAccount(accountId);
if(a==null){
return false;
}
else{
return a.deposit(amt);
}
}
boolean withdraw(int accountId, double amt){
Account a = findAccount(accountId);
if(a==null){
return false;
}
else{
return a.withdraw(amt);
}
}
Account findAccount(int accountId){
for(Account a: accounts){
if(a.getAccountId() == accountId){
return a;
}
}
return null;
}
boolean printPassbook(int accountId){
Account a = findAccount(accountId);
if(a==null){
return false;
}
return a.printPassbook();
}


}


TestBank.java

import java.util.*;
class TestBank{
static Bank b1;
static Scanner sc;
public static void main(String[] args){
if(!findAndLoadBank()){
createBank();
}
sc = new Scanner(System.in);
while(true){
int selection = showMenu();
if(selection==6){
System.out.println("Thank you !!");
saveBank();
break;
}
if(selection >0 && selection<6){
handleSelection(selection);
}
}
}
public static int showMenu(){
System.out.println("Select an option:\n-------------------------\n1. Create Accoun\n2. Deposit\n3. Withdraw\n4. Check Balance\n5. Print Passbook\n6. QUIT\n");
try{
int selection = Integer.parseInt(sc.nextLine());
return selection;
}catch(Exception e){
System.out.println("Invalid Selection\n");
return 0;
}
}
public static void handleSelection(int selection){
System.out.println("I am currently handling : "+ selection);
switch(selection){
case 1:

{
System.out.println("Enter your name: ");

String name = sc.nextLine();

System.out.println("Enter initial balance: ");
String balance = sc.nextLine();
try{
Account a = b1.createAccount(name, Double.parseDouble(balance));
System.out.println(a);
}catch(Exception e){
System.out.println("Invalid Data, Please try again !!");
}
}

break;
case 2:

{
System.out.println("Enter your account no: ");
String acno = sc.nextLine();
System.out.println("Enter deposit amount: ");
String amount = sc.nextLine();
try{
boolean confirm = b1.deposit(Integer.parseInt(acno), Double.parseDouble(amount));
System.out.println(confirm?"Deposit Successful":"Deposit Failed" );
}catch(Exception e){
System.out.println("Invalid Data, Please try again !!");
}

}
break;
case 3:

{
System.out.println("Enter your account no: ");
String acno = sc.nextLine();
System.out.println("Enter withdraw amount: ");
String amount = sc.nextLine();
try{
boolean confirm = b1.withdraw(Integer.parseInt(acno), Double.parseDouble(amount));
System.out.println(confirm?"Withdraw Successful":"Withdraw Failed" );
}catch(Exception e){
System.out.println("Invalid Data, Please try again !!");
}

}
break;
case 4:
{
System.out.println("Enter your account no: ");
String acno = sc.nextLine();
double balance = b1.getAccountBalance(Integer.parseInt(acno));
System.out.println("Balance is: "+ balance);
}
break;
case 5:
{
System.out.println("Enter your account no: ");
String acno = sc.nextLine();
b1.printPassbook(Integer.parseInt(acno));
}
break;
default:


}



}
public static void createBank(){
b1 = new Bank("SBI Alkapuri");
}

}

Thread Synchronization & Inter Thread Communication

When two or more threads need access to a shared resource, they need some way to ensure that the resource will be used by only one thread at a time.
The process by which this synchronization is achieved is called thread synchronization.
The synchronized keyword in Java creates a block of code referred to as a critical section. Every Java object with a critical section of code gets a lock associated with the object. To enter a critical section, a thread needs to obtain the corresponding object's lock.
This is the general form of the synchronized block statement:
synchronized(object) {
   // statements to be synchronized
}
  
We also have synchronized methods which are used as below:

synchronized void m1() {
   // method body to be synchronized
}
  
A synchronized block/method ensures that a call to a method that is a member of object occurs only after the current thread has successfully entered object's monitor.

Thread synchronization also includes a topic related to

INTER THREAD COMMUNICATION
Consider the classic queuing problem, where one thread is producing some data and another is consuming it. To make the problem more interesting, suppose that the producer has to wait until the consumer is finished before it generates more data.
In a polling system, the consumer would waste many CPU cycles while it waited for the producer to produce. Once the producer was finished, it would start polling, wasting more CPU cycles waiting for the consumer to finish, and so on. Clearly, this situation is undesirable.
To avoid polling, Java includes an elegant interprocess communication mechanism via the following methods:
  • wait( ): This method tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( ).
  • notify( ): This method wakes up the first thread that called wait( ) on the same object.
  • notifyAll( ): This method wakes up all the threads that called wait( ) on the same object.c The highest priority thread will run first.
These methods are implemented as final methods in Object, so all classes have them. All three methods can be called only from within a synchronized context.
These methods are declared within Object. Various forms of wait( ) exist that allow you to specify a period of time to wait.

Thread Priorities, Groups and Methods of Thread Class

Thread Priorities:

Every Java thread has a priority that helps the operating system determine the order in which threads are scheduled.
Java priorities are in the range between MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a constant of 10). By default, every thread is given priority NORM_PRIORITY (a constant of 5).
Threads with higher priority are more important to a program and should be allocated processor time before lower-priority threads. However, thread priorities cannot guarantee the order in which threads execute and very much platform dependentant.

Thread Group:

A ThreadGroup is used to represent a group of Threads. A tree structure can be formed from ThreadsGroups containing other ThreadGroups & Threads. Threads can only access the ThreadGroup to which they belong. This means that access to a ThreadGroups parent or other ThreadGroup is not permitted. Once a ThreadGroup is created various information can be obtained such as the ThreadGroups name, how many threads are active, the maximum priority that can be contained within the group and a host of other information. 

Thread Methods:

Following is the list of important methods available in the Thread class.
SNMethods with Description
1public void start()
Starts the thread in a separate path of execution, then invokes the run() method on this Thread object.
2public void run()
If this Thread object was instantiated using a separate Runnable target, the run() method is invoked on that Runnable object.
3public final void setName(String name)
Changes the name of the Thread object. There is also a getName() method for retrieving the name.
4public final void setPriority(int priority)
Sets the priority of this Thread object. The possible values are between 1 and 10.
5public final void setDaemon(boolean on)
A parameter of true denotes this Thread as a daemon thread.
6public final void join(long millisec)
The current thread invokes this method on a second thread, causing the current thread to block until the second thread terminates or the specified number of milliseconds passes.
7public void interrupt()
Interrupts this thread, causing it to continue execution if it was blocked for any reason.
8public final boolean isAlive()
Returns true if the thread is alive, which is any time after the thread has been started but before it runs to completion.
The previous methods are invoked on a particular Thread object. The following methods in the Thread class are static. Invoking one of the static methods performs the operation on the currently running thread
SNMethods with Description
1public static void yield()
Causes the currently running thread to yield to any other threads of the same priority that are waiting to be scheduled
2public static void sleep(long millisec)
Causes the currently running thread to block for at least the specified number of milliseconds
3public static boolean holdsLock(Object x)
Returns true if the current thread holds the lock on the given Object.
4public static Thread currentThread()
Returns a reference to the currently running thread, which is the thread that invokes this method.
5public static void dumpStack()
Prints the stack trace for the currently running thread, which is useful when debugging a multithreaded application

Life Cycle of a Thread:

A thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies. Following diagram shows complete life cycle of a thread.
Java Thread
Above mentioned stages are explained here:
  • New: A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.
  • Runnable: After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.
  • Waiting: Sometimes a thread transitions to the waiting state while the thread waits for another thread to perform a task.A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.
  • Timed waiting: A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.
  • Terminated: A runnable thread enters the terminated state when it completes its task or otherwise terminates.

Multi Threading in Java

Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.
A multithreading is a specialized form of multitasking. Multitasking threads require less overhead than multitasking processes.
I need to define another term related to threads: process: A process consists of the memory space allocated by the operating system that can contain one or more threads. A thread cannot exist on its own; it must be a part of a process. A process remains running until all of the non-daemon threads are done executing.
Multithreading enables you to write very efficient programs that make maximum use of the CPU, because idle time can be kept to a minimum.


Difference Between Process and Thread:

1.Threads share the address space of the process that created it;
 processes have their own address.

2.Threads have direct access to the data segment of its process; 
processes have their own copy of the data segment of the parent process. 

3.Threads can directly communicate with other threads of its process; 
processes must use interprocess communication to communicate with sibling processes(child process) 

4.Threads have almost no overhead; processes have considerable overhead.
(Overhead related to context switching)

5.New threads are easily created; new processes require lot of new allocation
 by operating System

6.Threads can exercise considerable control over threads of the same process; 
processes can only exercise control over child processes. 

7.Changes to the main thread (cancellation, priority change, etc.) may affect the 
behavior of the other threads of the process; 
changes to the parent process does not affect child processes. 

Using Throw Keyword (How to manually throw Exception in Java?)

In java we have already defined exception classes such asArithmeticExceptionArrayIndexOutOfBoundsException,NullPointerException etc. There are certain conditions defined for these exceptions and on the occurrence of those conditions they are implicitly thrown by JVM(java virtual machine).
Do you know that a programmer can create a new exception and throw it explicitly? These exceptions are known as user-defined exceptions. In order to throw user defined exceptions, throw keyword is being used. In this tutorial, we will see how to create a new exception and throw it in a program using throw keyword.
You can also throw an already defined exception like ArithmeticException,IOException etc.

Syntax of throw statement

throw AnyThrowableInstance;
Example:
//A void method
public void sample()
{
   //Statements
   //if (somethingWrong) then
   IOException e = new IOException();
   throw e;
   //More Statements
 }
Note:
  • A call to the above mentioned sample method should be always placed in a try block as it is throwing a checked exception – IOException. This is how it the call to above method should be done:
    MyClass obj =  new MyClass();
    try{
          obj.sample();
    }catch(IOException ioe)
     {
          //Your error Message here
          System.out.println(ioe);
      }
  • Exceptions in java are compulsorily of type Throwable. If you attempt to throw an object that is not throwable, the  compiler refuses to compile your program and it would show a compilation error.
Flow of execution while throwing an exception using throw keyword
Whenever a throw statement is encountered in a program the next statement doesn’t execute. Control immediately transferred to catch block to see if the thrown exception is handled there. If the exception is not handled there then next catch block is being checked for exception and so on. If none of the catch block is handling the thrown exception then a system generated exception message is being populated on screen, same what we get for un-handled exceptions.
E.g.
class ThrowDemo{
   public static void main(String args[]){
      try{
    char array[] = {'a','b','g','j'};
    /*I'm displaying the value which does not
     * exist so this should throw an exception
     */
    System.out.println(array[78]);
      }catch(ArithmeticException e){
     System.out.println("Arithmetic Exception!!");
       }
   }
}
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 
78 at beginnersbook.com.ThrowDemo.main(Details.java:9)
Since the exception thrown was not handled in the catch blocks the system generated exception message got displayed for that particular exception.

Explain Try - Catch in Java

What is Try Block?

The try block contains a block of program statements within which an exception might occur. A try block is always followed by a catch block, which handles the exception that occurs in associated try block. A try block must followed by a Catch block or Finally block or both.

Syntax of try block

try{
   //statements that may cause an exception
}

What is Catch Block?

A catch block must be associated with a try block. The corresponding catch block executes if an exception of a particular type occurs within the try block. For example if an arithmetic exception occurs in try block then the statements enclosed in catch block for arithmetic exception executes.

Syntax of try catch in java

try
{
     //statements that may cause an exception
}
catch (exception(type) e(object))‏
{
     //error handling code
}

Flow of try catch block

  1. If an exception occurs in try block then the control of execution is passed to the catch block from try block. The exception is caught up by the corresponding catch block. A single try block can have multiple catch statements associated with it, but each catch block can be defined for only one exception class. The program can also contain nested try-catch-finally blocks.
  2. After the execution of all the try blocks, the code inside the finally block executes. It is not mandatory to include a finally block at all, but if you do, it will run regardless of whether an exception was thrown and handled by the try and catch blocks.

An example of Try catch in Java

class Example1 {
   public static void main(String args[]) {
     int num1, num2;
     try { 
        // Try block to handle code that may cause exception
        num1 = 0;
        num2 = 62 / num1;
        System.out.println("Try block message");
     } catch (ArithmeticException e) { 
            // This block is to catch divide-by-zero error
            System.out.println("Error: Don't divide a number by zero");
       }
     System.out.println("I'm out of try-catch block in Java.");
   }
}
Output:
Error: Don't divide a number by zero
I'm out of try-catch block in Java.

Multiple catch blocks in Java

1. A try block can have any number of catch blocks.
2. A catch block that is written for catching the class Exception can catch all other exceptions
Syntax:
catch(Exception e){
  //This catch block catches all the exceptions
}
3. If multiple catch blocks are present in a program then the above mentioned catch block should be placed at the last as per the exception handling best practices.
4. If the try block is not throwing any exception, the catch block will be completely ignored and the program continues.
5. If the try block throws an exception, the appropriate catch block (if one exists) will catch it
–catch(ArithmeticException e) is a catch block that can catch ArithmeticException
–catch(NullPointerException e) is a catch block that can catch NullPointerException
6. All the statements in the catch block will be executed and then the program continues.

Example of Multiple catch blocks

class Example2{
   public static void main(String args[]){
     try{
         int a[]=new int[7];
         a[4]=30/0;
         System.out.println("First print statement in try block");
     }
     catch(ArithmeticException e){
        System.out.println("Warning: ArithmeticException");
     }
     catch(ArrayIndexOutOfBoundsException e){
        System.out.println("Warning: ArrayIndexOutOfBoundsException");
     }
     catch(Exception e){
        System.out.println("Warning: Some Other exception");
     }
   System.out.println("Out of try-catch block...");
  }
}
Output:
Warning: ArithmeticException
Out of try-catch block...
In the above example there are multiple catch blocks and these catch blocks executes sequentially when an exception occurs in try block. Which means if you put the last catch block ( catch(Exception e)) at the first place, just after try block then in case of any exception this block will execute as it has the ability to handle all exceptions. This catch block should be placed at the last to avoid such situations.