//Employee class............. package employeedetails; public class EmployeeDetails { private String EmpName; private int EmpId; public String getEmpName(){ return EmpName; } public int getEmpId(){ return EmpId; } EmployeeDetails(String EmpName,int EmpId){ this.EmpName=EmpName; this.EmpId=EmpId; } public String toString(){ return "EMployeeName= "+EmpName+" EmployeeID= "+EmpId; } }
// Main class package employeedetails; import java.util.Enumeration; import java.util.Hashtable; import java.util.Scanner; public class Employee { String name; int id; Scanner s= new Scanner(System.in); EmployeeDetails employeeDetails=null; Hashtable<string,employeedetails> h= new Hashtable<string,employeedetails> (); public static void main(String[] args) { Employee employee= new Employee(); employee.callMe(); } public void callMe(){ System.out.println("you want to add emp details"); char ch= Employee.getChar(); if(ch=='y'||ch=='Y'){ getData(); employeeDetails= new EmployeeDetails(name,id); h.put(name, employeeDetails); for(Enumeration<employeedetails> en = h.elements();en.hasMoreElements();) { employeeDetails = en.nextElement(); System.out.println(employeeDetails); } } else{} } public void getData(){ System.out.println("Enter Emp name"); name= InputValidation.getString(); System.out.println("Enter Emp id"); id= Integer.parseInt(InputValidation.getInteger()); callMe(); } public static char getChar(){ Scanner scn= new Scanner(System.in); char ch=scn.next().toLowerCase().charAt(0); if(!(ch=='y'||ch=='Y'||ch=='n'||ch=='N')){ System.out.println("Enter only YES/NO"); ch= getChar(); } return ch; } }
o/p: you want to add emp details y Enter Emp name bee Enter Emp id 123 you want to add emp details y Enter Emp name kira Enter Emp id 23456 you want to add emp details y Enter Emp name been Enter Emp id 899 you want to add emp details n EMployeeName= been EmployeeID= 899 EMployeeName= been EmployeeID= 899 EMployeeName= been EmployeeID= 899
main
callMe
getData
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)