25
package comc1; import java.util.ArrayList; import java.util.Scanner; public class EmployeeManagement { public static void main(String args[]) { ArrayList<String> empNameList=new ArrayList<String>(); Scanner sc =new Scanner(System.in); System.out.println("How many names do you want to add to the system?"); int num=sc.nextInt(); EmployeeManagement empMgmt=new EmployeeManagement(); empMgmt.addEmpName(num,empNameList); } public void addEmpName(int numEmp,ArrayList<String> empList) { Scanner sc=new Scanner(System.in); for(int i=1;i<=numEmp;i++) { System.out.println("ENter nameof the employee"); String name=sc.nextLine(); empList.add(name); } } } ---------------------------------- package comc2; import java.util.Date; public class Employee { private String designation; private Date dob;

Assn4 1 Coll

Embed Size (px)

DESCRIPTION

collections

Citation preview

Page 1: Assn4 1 Coll

package comc1;

import java.util.ArrayList;import java.util.Scanner;

public class EmployeeManagement {public static void main(String args[]){

ArrayList<String> empNameList=new ArrayList<String>();

Scanner sc =new Scanner(System.in);System.out.println("How many names do you want to add to the

system?");int num=sc.nextInt();EmployeeManagement empMgmt=new EmployeeManagement();empMgmt.addEmpName(num,empNameList);

}

public void addEmpName(int numEmp,ArrayList<String> empList){

Scanner sc=new Scanner(System.in);

for(int i=1;i<=numEmp;i++){

System.out.println("ENter nameof the employee");String name=sc.nextLine();empList.add(name);

}}

}

----------------------------------

package comc2;

import java.util.Date;

public class Employee {

private String designation;private Date dob;private int employeeID;private String name;private float salary;

public String getDesignation(){

return designation;

Page 2: Assn4 1 Coll

}

public Date getDob(){

return dob;}

public int getEmployeeID(){

return employeeID;}

public String getName(){

return name;}

public float getSalary(){

return salary;}

public void setDesignation(String designation){

this.designation=designation;}

public void setDob(Date dob){

this.dob=dob;}

public void setEmployeeID(int employeeID){

this.employeeID=employeeID;}

public void setName(String name){

this.name=name;}

public void setSalary(float salary){

this.salary=salary;}

}

package comc2;

Page 3: Assn4 1 Coll

import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.Iterator;import java.util.Scanner;

public class EmployeeManagement {

public static void main(String args[]){

//Employee e =new Employee();ArrayList<Employee> empList=new ArrayList<Employee>();

Scanner sc4 =new Scanner(System.in);System.out.println("How many employees details do you want

to add to the system?");int num=sc4.nextInt();

//Employee[] e=new Employee[num];

EmployeeManagement empMgmt=new EmployeeManagement();empMgmt.addEmployees(num,empList);empMgmt.displayEmployees(empList);

}

public void addEmployees(int numEmp,ArrayList<Employee> empList){

Scanner sc=new Scanner(System.in);

for(int i=1;i<=numEmp;i++){

Employee e =new Employee(); //should be kept inside loop

System.out.println("Enter name of the employee");String name = sc.nextLine();e.setName(name);

System.out.println("Enter designation:"); String designation = sc.nextLine(); e.setDesignation(designation); Scanner sc1=new Scanner(System.in); System.out.println("Enter id of the employee"); int employeeID=sc1.nextInt(); e.setEmployeeID(employeeID);

Page 4: Assn4 1 Coll

System.out.println("Enter salary of the employee"); float salary=sc1.nextFloat(); e.setSalary(salary); System.out.println("Enter dob of the employee"); SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy"); Scanner sc2 = new Scanner(System.in); System.out.println("Eample: 12-25-2103"); System.out.print("Enter date: "); String str = sc2.nextLine(); try { //Date date = sdf.parse(str); e.setDob(sdf.parse(str)); //sdf = new SimpleDateFormat("EEE, d MMM yyyy"); //System.out.println("Date: " + sdf.format(date)); } catch (ParseException exp) { System.out.println("Parse Exception"); }

//System.out.println("Dob is:"+e.getDob());

empList.add(e);}

}

public void displayEmployees(ArrayList<Employee> empList){

Iterator<Employee> itr = empList.iterator();

while(itr.hasNext()){

Employee e = itr.next();System.out.print("\n"+e.getName());

}

/*for(Employee e:empList){

System.out.print(e.getName());}*/

/*for(int i=0;i<empList.size();i++){

Page 5: Assn4 1 Coll

System.out.print("\n"+empList.get(i).getName()+""+empList.get(i).getDesignation());

//System.out.print(e); }*/

/*Employee e =new Employee(); for(int i=0;i<empList.size();i++) {

e1=empList.get(i); //same as above..varies just in assinging e1

System.out.print(e1.getName()+""+e1.getDesignation()+""+e1.getEmployeeID()+""+e1.getSalary()+""+e1.getDob());

}*/}

}

------------------

package comc2;

import java.util.*;import java.text.*; public class ScanDate{ public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy"); Scanner sc = new Scanner(System.in); System.out.println("Eample: 12-25-2103"); System.out.print("Enter date: "); String str = sc.nextLine(); try { Date date = sdf.parse(str); sdf = new SimpleDateFormat("EEE, d MMM yyyy"); System.out.println("Date: " + sdf.format(date)); } catch (ParseException e) { System.out.println("Parse Exception"); } }}

Page 6: Assn4 1 Coll

---------------------

employee.java (same as above)

package comc3;

import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.Iterator;import java.util.Scanner;

public class EmployeeManagement {

public static void main(String args[]){

//Employee e =new Employee();ArrayList<Employee> empList=new ArrayList<Employee>();

Scanner sc4 =new Scanner(System.in);System.out.println("How many employees details do you want

to add to the system?");int num=sc4.nextInt();

Scanner sc5=new Scanner(System.in);System.out.print("Enter name to be deleted:");String eName=sc5.nextLine();

Scanner sc6=new Scanner(System.in);System.out.print("Enter name to be checked:");String eName1=sc5.nextLine();

EmployeeManagement empMgmt=new EmployeeManagement();empMgmt.addEmployees(num,empList);empMgmt.displayEmployees(empList);empMgmt.deleteEmployee(eName, empList);empMgmt.isEmployeePresent(eName1, empList);

}

public void addEmployees(int numEmp,ArrayList<Employee> empList){

Scanner sc=new Scanner(System.in);

for(int i=1;i<=numEmp;i++){

Page 7: Assn4 1 Coll

Employee e =new Employee(); //should be kept inside loop

System.out.println("Enter name of the employee");String name = sc.nextLine();e.setName(name);/*Employee e1 =new Employee(); for(int j=0;j<empList.size();j++){e1=empList.get(i);if(e1.getName().equals(name)){

System.out.println("This name is already present");

}else{

e.setName(name);}}*/

System.out.println("Enter designation:"); String designation = sc.nextLine(); e.setDesignation(designation); Scanner sc1=new Scanner(System.in); System.out.println("Enter id of the employee"); int employeeID=sc1.nextInt(); e.setEmployeeID(employeeID); System.out.println("Enter salary of the employee"); float salary=sc1.nextFloat(); e.setSalary(salary); System.out.println("Enter dob of the employee"); SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy"); Scanner sc2 = new Scanner(System.in); System.out.println("Eample: 12-25-2103"); System.out.print("Enter date: "); String str = sc2.nextLine(); try { e.setDob(sdf.parse(str)); } catch (ParseException exp) { System.out.println("Parse Exception"); }

empList.add(e);}

Page 8: Assn4 1 Coll

}

public void displayEmployees(ArrayList<Employee> empList){

Iterator<Employee> itr = empList.iterator();

while(itr.hasNext()){

Employee e = itr.next();System.out.print("\n"+e.getName());

}

/*for(Employee e:empList){

System.out.print(e.getName());}*/

}

public boolean deleteEmployee(String eName,ArrayList<Employee> empList)

{boolean b=false; //to print one statement in a loop(flag)

Employee e1 =new Employee(); for(int i=0;i<empList.size();i++){

e1=empList.get(i);if(eName.equals(e1.getName())){

empList.remove(e1);System.out.print("\n"+eName+" is deleted from the

list");b=true;break;/*for(Employee e:empList){

System.out.print("\n new list after deletion is:"+e.getName());

}*/}else{

b=false;//System.out.print("\n"+eName+" not there in

list");}

Page 9: Assn4 1 Coll

}if(b==true){

System.out.print("\n element deleted");}else{

System.out.print("\n required element not present to be deleted");

}

return true;

}

public boolean isEmployeePresent(String eName,ArrayList<Employee> empList)

{boolean b=false;

Employee e1 =new Employee(); for(int i=0;i<empList.size();i++){

e1=empList.get(i);//if(eName.equalsIgnoreCase(e1.getName()))if(e1.getName().contains(eName)){

System.out.print("\n"+e1.getName()+" is present in the list");

b=true;break;

}else{

b=false;}

}if(b==true){

System.out.print("\n IsEmployeePresent: present");}else{

System.out.print("\n IsEmployeePresent: not present");}

return b;

Page 10: Assn4 1 Coll

}

}-------------------

package comset;

import java.util.Date;

public class Employee {

private String designation;private Date dob;private int employeeID;private String name;private float salary;

public String getDesignation(){

return designation;}

public Date getDob(){

return dob;}

public int getEmployeeID(){

return employeeID;}

public String getName(){

return name;}

public float getSalary(){

return salary;}

public void setDesignation(String designation){

this.designation=designation;}

Page 11: Assn4 1 Coll

public void setDob(Date dob){

this.dob=dob;}

public void setEmployeeID(int employeeID){

this.employeeID=employeeID;}

public void setName(String name){

this.name=name;}

public void setSalary(float salary){

this.salary=salary;}

public boolean equals(Object o) { if (!(o instanceof Employee)) { return false; } return this.name.equalsIgnoreCase(((Employee)o).name); } @Override public int hashCode() { return name.length(); }

/*public boolean equals(Object o){

if (!(o instanceof Employee)) {

return false; } return this.employeeID == ((Employee)o).employeeID;

}

public int hashCode(){

return employeeID;

}*/

}

package comset;

Page 12: Assn4 1 Coll

import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.HashSet;import java.util.Iterator;import java.util.Scanner;import java.util.Set;

public class EmployeeManagement {

public static void main(String args[]){

Set<Employee> empList=new HashSet<Employee>();

Scanner sc4 =new Scanner(System.in);System.out.println("How many employees details do you want

to add to the system?");int num=sc4.nextInt();

Scanner sc5=new Scanner(System.in);System.out.print("Enter name to be deleted:");String eName=sc5.nextLine();

Scanner sc6=new Scanner(System.in);System.out.print("Enter name to be checked:");String eName1=sc6.nextLine();

EmployeeManagement empMgmt=new EmployeeManagement();empMgmt.addEmpName(num,empList);empMgmt.displayEmployees(empList);empMgmt.deleteEmployee(eName, empList);empMgmt.isEmployeePresent(eName1, empList);

}

public void addEmpName(int numEmp,Set<Employee> empList){

Scanner sc=new Scanner(System.in);

for(int i=1;i<=numEmp;i++){

Employee e =new Employee(); //should be kept inside loop

Scanner sc1=new Scanner(System.in);

System.out.println("Enter id of the employee"); int employeeID=sc1.nextInt(); e.setEmployeeID(employeeID); //System.out.println(empList.add(e)); /* if(empList.add(e)==false)

Page 13: Assn4 1 Coll

{ System.out.println("id is duplicate value"); continue; }*/ /* while(!empList.add(e)) { System.out.println("id is duplicate value"); break; }*/

System.out.println("Enter name of the employee");String name = sc.nextLine();e.setName(name);//System.out.println(empList.add(e));if(empList.add(e)==false)

{ System.out.println("name is duplicate value"); continue; }

System.out.println("Enter designation:"); String designation = sc.nextLine(); e.setDesignation(designation); System.out.println("Enter salary of the employee"); float salary=sc1.nextFloat(); e.setSalary(salary); System.out.println("Enter dob of the employee"); SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy"); Scanner sc2 = new Scanner(System.in); System.out.println("Eample: 12-25-2103"); System.out.print("Enter date: "); String str = sc2.nextLine(); try { e.setDob(sdf.parse(str)); } catch (ParseException exp) { System.out.println("Parse Exception"); }

empList.add(e); //System.out.println(empList.add(e));

}}

public void displayEmployees(Set<Employee> empList){

Page 14: Assn4 1 Coll

Iterator<Employee> itr = empList.iterator();

while(itr.hasNext()){

Employee e = itr.next();System.out.print("\n"+e.getName());

}

/*for(Employee e:empList){

System.out.print(e.getName());}*/

}

public boolean deleteEmployee(String eName,Set<Employee> empList){

boolean b=false; //to print one statement in a loop(flag)

//Employee e1 =new Employee(); //for(int i=0;i<empList.size();i++)//{

Iterator<Employee> itr = empList.iterator();

while(itr.hasNext()){

Employee e = itr.next();//System.out.print("\n"+e.getName());

//e1=empList.get(i);if(eName.equals(e.getName())){

empList.remove(e);System.out.print("\n"+eName+" is deleted from the

list");b=true;break;/*for(Employee e:empList){

System.out.print("\n new list after deletion is:"+e.getName());

}*/}else{

b=false;//System.out.print("\n"+eName+" not there in

list");}

}//}

Page 15: Assn4 1 Coll

if(b==true){

System.out.print("\n element deleted");}else{

System.out.print("\n required element not present to be deleted");

}

return true;}

public boolean isEmployeePresent(String eName,Set<Employee> empList)

{boolean b=false; //to print one statement in a loop(flag)

Iterator<Employee> itr = empList.iterator();

while(itr.hasNext()){Employee e = itr.next();

//if(e.getName().contains(eName))if(eName.equals(e.getName())){

System.out.print("\n"+e.getName()+" is present in the list");

b=true;break;

}else{

b=false;}

}if(b==true){

System.out.print("\n IsEmployeePresent: present");

}else{

System.out.print("\n IsEmployeePresent: not present");

}return true;

}

Page 16: Assn4 1 Coll

}-----------------------package comMap;

import java.util.Date;

public class Employee {

private String designation;private Date dob;private int employeeID;private String name;private float salary;

public String getDesignation(){

return designation;}

public Date getDob(){

return dob;}

public int getEmployeeID(){

return employeeID;}

public String getName(){

return name;}

public float getSalary(){

return salary;}

public void setDesignation(String designation){

this.designation=designation;}

public void setDob(Date dob){

this.dob=dob;}

public void setEmployeeID(int employeeID){

Page 17: Assn4 1 Coll

this.employeeID=employeeID;}

public void setName(String name){

this.name=name;}

public void setSalary(float salary){

this.salary=salary;}

public boolean equals(Object o){

if (!(o instanceof Employee)) {

return false; } return this.employeeID == ((Employee)o).employeeID;

}

public int hashCode(){

return employeeID;

}

}

package comMap;

public class Hobby {private String hobbyDescription;private String hobbyName;

public String getHobbyDescription(){

return hobbyDescription;}

public String getHobbyName(){

return hobbyName;}

public void setHobbyDescription(String hobbyDescription){

this.hobbyDescription=hobbyDescription;}

Page 18: Assn4 1 Coll

public void setHobbyName(String hobbyName){

this.hobbyName=hobbyName;}

}

package comMap;

import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.ConcurrentModificationException;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import java.util.Scanner;import java.util.Set;

public class EmployeeManagement {public static void main(String args[]){

Map<Employee,Hobby> empList=new HashMap<Employee,Hobby>();

Scanner sc4 =new Scanner(System.in);System.out.println("How many employees details do you want

to add to the system?");int num=sc4.nextInt();

Scanner sc5=new Scanner(System.in);System.out.print("Enter id to be deleted:");int employeeID=sc5.nextInt();

Scanner sc6=new Scanner(System.in);System.out.print("Enter id to be checked:");int employeeID1=sc6.nextInt();

EmployeeManagement empMgmt=new EmployeeManagement();empMgmt.addEmpName(num,empList);empMgmt.displayEmployees(empList);empMgmt.deleteEmployee(employeeID, empList);empMgmt.isEmployeePresent(employeeID1, empList);

}

public void addEmpName(int numEmp,Map<Employee,Hobby> empList){

for(int i=1;i<=numEmp;i++){

Page 19: Assn4 1 Coll

Employee e =new Employee(); Hobby h=new Hobby();

Scanner sc1=new Scanner(System.in);System.out.println("Enter id of the employee");

int employeeID=sc1.nextInt(); e.setEmployeeID(employeeID); //System.out.println(empList.add(e)); /* if(empList.add(e)==false) { System.out.println("id is duplicate value"); continue; }*/

System.out.println("Enter name of the employee");Scanner sc=new Scanner(System.in);String name = sc.nextLine();e.setName(name);//System.out.println(empList.add(e));/*if(empList.add(e)==false)

{ System.out.println("name is duplicate value"); continue; }*/

System.out.println("Enter designation:"); String designation = sc.nextLine(); e.setDesignation(designation); System.out.println("Enter salary of the employee"); float salary=sc1.nextFloat(); e.setSalary(salary); System.out.println("Enter dob of the employee"); SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy"); Scanner sc2 = new Scanner(System.in); System.out.println("Eample: 12-25-2103"); System.out.print("Enter date: "); String str = sc2.nextLine(); try { e.setDob(sdf.parse(str)); } catch (ParseException exp) { System.out.println("Parse Exception"); } System.out.println("Enter Hobby-name:");

Page 20: Assn4 1 Coll

String hn = sc.next(); h.setHobbyName(hn); System.out.println("Enter Hobby-description"); String hd = sc.next(); h.setHobbyDescription(hd);

empList.put(e,h); }

}

public void displayEmployees(Map<Employee,Hobby> hm){

for (Map.Entry<Employee, Hobby> m : hm.entrySet()) { System.out.println(m.getKey() +" "+m.getKey().getName() +" " + m.getValue().getHobbyName()); }

}

public boolean deleteEmployee(int id,Map<Employee,Hobby> m1){try{for (Entry<Employee, Hobby> entry : m1.entrySet()){

Employee e = entry.getKey(); // here you have the employee // check if this employee have same id than given if(e.getEmployeeID() == id) { // map contains the employee //System.out.println (m1.remove(e)); m1.remove(e); System.out.println ("After deletion:------"); for (Map.Entry<Employee, Hobby> m : m1.entrySet()) { System.out.println(m.getKey() + " " + m.getValue().getHobbyName()); } } }

}catch(ConcurrentModificationException exc){

System.out.println("ConcurrentModificationException exception:--");

}return true;

}

Page 21: Assn4 1 Coll

public boolean isEmployeePresent(int id,Map<Employee,Hobby> m2){

boolean b=false;for (Entry<Employee, Hobby> entry : m2.entrySet()){

Employee e = entry.getKey(); //System.out.println(m2.get(e));

if (e.getEmployeeID() == id){

Hobby k=m2.get(e);

for (Map.Entry<Employee, Hobby> m : m2.entrySet()) { if(m.getValue() == k) System.out.println(m.getKey().getName() + " " + m.getValue().getHobbyName()); }

b=true; break;

} else b=false;

}

if (b == true) System.out.println("The employee is present"); else System.out.println("The employee is not present");

return b;}

}