package collections;
import java.util.Date;
public class Employee1 {
String designation;
String dob;
int employeeId;
String name;
float salary;
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
public String toString()
{
return name+"\n"+salary+"\n"+employeeId+"\n"+dob+"\n"+designation;
}
public int hashCode(){
System.out.println("In hashcode");
int hashcode = 0;
hashcode = employeeId*20;
hashcode += name.hashCode();
hashcode+=designation.hashCode();
hashcode+=dob.hashCode();
return hashcode;
}
public boolean equals(Object obj){
System.out.println("In equals");
if (obj instanceof Employee1) {
Employee1 pp = (Employee1) obj;
return (pp.name.equals(this.name)&&pp.designation.equals(this.designation)&&pp.salary==(this.salary)&&pp.dob==(this.dob)&&pp.employeeId==(this.employeeId));
} else {
return false;
}
}
}
package collections;
import java.util.*;
public class EmployeeManagement3 {
public void addEmployees(int numEmp,HashSet<Employee1> emp)
{
Scanner sc=new Scanner(System.in);
for(int i=1;i<=numEmp;i++)
{
Employee1 empl=new Employee1();
System.out.println("Enter Employee Name: ");
String name=sc.next();
empl.setName(name);
System.out.println("Enter Employee ID: ");
int id=sc.nextInt();
empl.setEmployeeId(id);
System.out.println("Enter Employee Designation: ");
String des=sc.next();
empl.setDesignation(des);
System.out.println("Enter Employee DOB: ");
String dt=sc.next();
empl.setDob(dt);
System.out.println("Enter Employee Salary: ");
float sal=sc.nextFloat();
empl.setSalary(sal);
emp.add(empl);
}
}
public void displayEmployees(HashSet<Employee1> empList)
{
//There are many methods by which we can retrieve the data but i am showing 2 methods
//Displaying Employee details using For each loop
/*for(Employee1 e:empList)
{
System.out.println(e);
}*/
//Display Employee details using Iterator
Iterator itr=empList.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
public boolean isEmployeePresent(String empName,HashSet<Employee1> emp)
{
boolean exists=false;
for(Employee1 e:emp)
{
if(e.getName().equals(empName))
{
exists=true;
break;
}
}
return exists;
}
public boolean deleteEmployee(String empName,HashSet<Employee1> emp)
{
boolean exists=false;
for(Employee1 e:emp)
{
if(e.getName().equals(empName))
{
exists=true;
emp.remove(e);
break;
}
}
return exists;
}
public static void main(String[] args) {
HashSet<Employee1> al=new HashSet<Employee1>();
EmployeeManagement3 em=new EmployeeManagement3();
System.out.println("Enter the number of employees: ");
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
em.addEmployees(num,al);
em.displayEmployees(al);
System.out.println("Enter the employee you want to remove: ");
String empName1=sc.next();
boolean bool=em.deleteEmployee(empName1, al);
System.out.println("Is the Employee Present and deleted? "+bool);
System.out.println("After deletion :");
em.displayEmployees(al);
System.out.println("Enter the employee you want to search: ");
String empName=sc.next();
bool=em.isEmployeePresent(empName, al);
System.out.println("Is the Employee Present? "+bool);
}
}
import java.util.Date;
public class Employee1 {
String designation;
String dob;
int employeeId;
String name;
float salary;
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
public String toString()
{
return name+"\n"+salary+"\n"+employeeId+"\n"+dob+"\n"+designation;
}
public int hashCode(){
System.out.println("In hashcode");
int hashcode = 0;
hashcode = employeeId*20;
hashcode += name.hashCode();
hashcode+=designation.hashCode();
hashcode+=dob.hashCode();
return hashcode;
}
public boolean equals(Object obj){
System.out.println("In equals");
if (obj instanceof Employee1) {
Employee1 pp = (Employee1) obj;
return (pp.name.equals(this.name)&&pp.designation.equals(this.designation)&&pp.salary==(this.salary)&&pp.dob==(this.dob)&&pp.employeeId==(this.employeeId));
} else {
return false;
}
}
}
package collections;
import java.util.*;
public class EmployeeManagement3 {
public void addEmployees(int numEmp,HashSet<Employee1> emp)
{
Scanner sc=new Scanner(System.in);
for(int i=1;i<=numEmp;i++)
{
Employee1 empl=new Employee1();
System.out.println("Enter Employee Name: ");
String name=sc.next();
empl.setName(name);
System.out.println("Enter Employee ID: ");
int id=sc.nextInt();
empl.setEmployeeId(id);
System.out.println("Enter Employee Designation: ");
String des=sc.next();
empl.setDesignation(des);
System.out.println("Enter Employee DOB: ");
String dt=sc.next();
empl.setDob(dt);
System.out.println("Enter Employee Salary: ");
float sal=sc.nextFloat();
empl.setSalary(sal);
emp.add(empl);
}
}
public void displayEmployees(HashSet<Employee1> empList)
{
//There are many methods by which we can retrieve the data but i am showing 2 methods
//Displaying Employee details using For each loop
/*for(Employee1 e:empList)
{
System.out.println(e);
}*/
//Display Employee details using Iterator
Iterator itr=empList.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
public boolean isEmployeePresent(String empName,HashSet<Employee1> emp)
{
boolean exists=false;
for(Employee1 e:emp)
{
if(e.getName().equals(empName))
{
exists=true;
break;
}
}
return exists;
}
public boolean deleteEmployee(String empName,HashSet<Employee1> emp)
{
boolean exists=false;
for(Employee1 e:emp)
{
if(e.getName().equals(empName))
{
exists=true;
emp.remove(e);
break;
}
}
return exists;
}
public static void main(String[] args) {
HashSet<Employee1> al=new HashSet<Employee1>();
EmployeeManagement3 em=new EmployeeManagement3();
System.out.println("Enter the number of employees: ");
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
em.addEmployees(num,al);
em.displayEmployees(al);
System.out.println("Enter the employee you want to remove: ");
String empName1=sc.next();
boolean bool=em.deleteEmployee(empName1, al);
System.out.println("Is the Employee Present and deleted? "+bool);
System.out.println("After deletion :");
em.displayEmployees(al);
System.out.println("Enter the employee you want to search: ");
String empName=sc.next();
bool=em.isEmployeePresent(empName, al);
System.out.println("Is the Employee Present? "+bool);
}
}
0 comments:
Post a Comment