Chain of Responsibility Design Pattern

Below is an example of Chain of Responsibility Design Pattern

One of the best example where Chain of Responsibility Design Pattern can be applied is below.
Say if you want to withdraw 2200 Rs, and ATM has 1000, 500 and 100 rupee notes.
As per chain of responsibility the request would be sent to object that dispenses 1000 rs note.
If 1000 rupee notes are NOT available then it would be sent to an object that dispenses 500 rs note.
The 500Rs object decides to dispense 4 notes and then 100 rs object would be asked to handle the rest.
* Advantage is, eventhough there are no 1000 rs not available, your request would be still handled by other objects like 500 and 100.

class Request{
	int value;
	String desc;
	
	public Request(int value, String desc)
	{
		this.value = value;
		this.desc = desc;
	}
	
	public int getValue() {
		return value;
	}
	public void setValue(int value) {
		this.value = value;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}	
}

abstract class Handler{
	Handler scsr;
	
	public void setSuccessor(Handler scsr)
	{
		this.scsr = scsr;
	}
	
	public abstract void handleRequest(Request rqs);
}

class ConcreteHandlerOne extends Handler{
	public void handleRequest(Request rqs)
	{
		if(rqs.getValue()<0)
		{
			System.out.println(" Handling Request details<0:"+rqs.getValue()+" "+rqs.getDesc());
		}
		else
		{
			scsr.handleRequest(rqs);
		}
	}
}

class ConcreteHandlerTwo extends Handler{
	public void handleRequest(Request rqs)
	{
		if(rqs.getValue()>0)
		{
			System.out.println("Handling Request details>0:"+rqs.getValue()+" "+rqs.getDesc());
		}
		else
		{
			scsr.handleRequest(rqs);
		}
	}
}

class ConcreteHandlerThree extends Handler{
	public void handleRequest(Request rqs)
	{
		if(rqs.getValue()==0)
		{
			System.out.println("Handling Request details==0:"+rqs.getValue()+" "+rqs.getDesc());
		}
		else
		{
			scsr.handleRequest(rqs);
		}
	}
}

public class CoREg {
	public static void main(String[] args) {
		Handler h1 = new ConcreteHandlerOne();
		Handler h2 = new ConcreteHandlerTwo();
		Handler h3 = new ConcreteHandlerThree();
		
		h1.setSuccessor(h3);
		h3.setSuccessor(h2);
		
		Request req = new Request(25, "Test Purpose");
		h1.handleRequest(req);
		
		Request reqn = new Request(-25, "Test Purpose");
		h1.handleRequest(reqn);
	}
}

Mini Project – 1

Develop a mini project, a console based Java Application, which can provide below options to manage Employee details in Database.

1. Create New Employee.
2. Delete an Employee, based on Employee ID
3. View Details of an Employee, based on Employee ID
4. View Details of all Employee
5. Exit from Application.

Whenever user selects an option, that specific Task need to be performed, and again above options need to be displayed, until user selects option 5, above.

import java.io.BufferedReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import java.io.*;

public class Project {
	public static void main(String[] args) throws IOException {
		Scanner scanner=new Scanner(System.in);
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		
		int choice='5';
		try{
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=DriverManager.getConnection("jdbc:mysql://localhost/companydetails","root","cherry");
		System.out.println("Connection is Established...");
		System.out.println("\n");
		Statement st=con.createStatement();
		do{
			System.out.println("Employees Menu");
			System.out.println("1.To Create New Employees.");
			System.out.println("2.To Delete An Employees.");
			System.out.println("3.To Display Details of one Employees.");
			System.out.println("4.To Display Details of all Employees.");
			
			System.out.println("5.Exit.");
			System.out.println("Enter Your Choice.");
			choice=scanner.nextInt();
			switch(choice)
			{
			case 1:
				System.out.println("Enter Employees id");
				System.out.println("Enter Employees Name");
				System.out.println("Enter Employees salary");
				System.out.println("Enter Employees Age");
				String eid = br.readLine();
				String ename = br.readLine();
				String esalary=br.readLine();
				String eage=br.readLine();
				st.executeUpdate("insert into employees values(eid,'"+ename+"',"+eage+" ,"+esalary+" )");
				System.out.println("Succesfully Employees Added.");
				
			case 2:
				System.out.println("Enter Employees id");
			    String Eid = br.readLine();
				st.executeUpdate("delete from employees where eid="+Eid);
				System.out.println("Succesfully Employees Deleted.");
				break;
			case 3:
				System.out.println("Enter Employees id");
			    String Eid1 = br.readLine();
				//st.executeQuery("select * from employees where eid="+Eid1);
				Statement st4=con.createStatement();
            	
            	String sql5="select eid,ename,eage,esalary from employees where eid="+Eid1+"";
            	
            	ResultSet rs5=st4.executeQuery(sql5);
            while(rs5.next())
            	{
            		
            		int id1=rs5.getInt("eid");
            		
            		String name=rs5.getString("ename");
            		
            		int age=rs5.getInt("eage");
            		
            		int salary=rs5.getInt("esalary");
            		
            		System.out.println("Emp Id:"+Eid1);
            		
            		System.out.println("Emp Name:"+name);
            		
            		System.out.println("Emp Age:"+age);
            		
            		System.out.println("Emp Salary:"+salary);
            		
            		System.out.println("Verification sucessfull!!!");
            	}
            		break;
			
			case 4:
				ResultSet rs=st.executeQuery("select * from employees");
				while(rs.next())
				{
					System.out.println(rs.getInt(1)+"\t\t"+rs.getString(2)+"\t\t"+rs.getInt(3)+"\t\t"+rs.getInt(4));
				}
				break;
			}
		}while(choice!=6);
		con.close();
		}catch(ClassNotFoundException e)
		{
			e.printStackTrace();
			
		}catch(SQLException e)
		{
			e.printStackTrace();
			}

	}
		
	}