请查看如何输入员工详细信息,更新,删除,使用hashmap进行显示

时间:2015-10-17 04:50:00

标签: java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

class Employee
{
private String Id;
private String Name;
private String Department;
private String Salary;

public Employee(String Id, String Name, String Department,
String Salary)
{
this.Id=Id;
this.Name=Name;
this.Department=Department;
this.Salary=Salary;
}

public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getDepartment() {
return Department;
}
public void setDepartment(String department) {
Department = department;
}
public String getSalary() {
return Salary;
}
public void setSalary(String salary) {
Salary = salary;
}

public String toString() 
{ 
return this.Id+"\t"+this.Name+"\t\t"+this.Department+"\t\t\t"+this.Salary;
}
}


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

try {

Map< String, List<Employee> > m=new HashMap< String, List<Employee> >();
List<Employee> ListForFinance=new ArrayList<Employee>();
Scanner scn1=new Scanner(System.in);
String Id;
String Name;
String Department;
String Salary;

while(true)
{
System.out.print("\nThe Choices:\n1>add\n2>modification\n3>remove\n4>display\n\n");
System.out.println("Enter the choice: ");
System.out.println("To quit type -1");
int num=scn1.nextInt();
if(num == -1)
{
break;
}

switch(num)
{
case 1:
{
System.out.print("\nDepartment: ");
Department=scn1.next();
System.out.print("\nId: ";
Id=scn1.next();
System.out.print("\nName: ");
Name=scn1.next();
System.out.print("\nSalary: ");
Salary=scn1.next();

Employee employee1=new Employee(Id,Name,Department,Salary);
ListForFinance.add(employee1);
m.put(Department, ListForFinance);
break;
}

case 2:
{
System.out.println("Type Department to be modified");
Department=scn1.next();
System.out.println("Modification values");
System.out.print("\nId: ");
Id=scn1.next();
System.out.print("\nName: ");
Name=scn1.next();
System.out.print("\nSalary: ";
Salary=scn1.next();

Set<String> s=m.keySet();
Iterator<String> i=s.iterator();

Employee employee1=new Employee(Id,Name,Department,Salary);
m.get(Department.setId("Id");
ListForFinance.add(employee1);
m.put(Department, ListForFinance);

while(i.hasNext())
{
System.out.println(i.next());
}

break;
}



case 3:
{
System.out.println("=========================================================");
System.out.println("ID"+"\t"+"NAME"+"\t\t"+"DEPARTMENT"+"\t\t"+"SALARY");
System.out.println("=========================================================");
Set<String> s=m.keySet();
Iterator<String> i=s.iterator();

while(i.hasNext()) 
{
     String dept=i.next();
     List<Employee> employees=m.get(dept);
     for(int j=0;j<employees.size();j++)
     {
         System.out.print("\n"+employees.get(j)+"\n\n"); 
     }  
}
break;
}

}
}
}

catch(Exception e)
{
System.out.println("NOTE: \n"+"Please enter specified key format..!!!");
System.out.println("======================================");
System.out.println("Now you are Signing out");
System.out.println("Thank You,Login Again");
System.out.println("======================================"); 
}
}
}

default:
System.out.println("=============================================================";
System.out.print("Wrong key Pressed,please enter the correct key\n";
System.out.println("Try again...!!!";
System.out.println("=============================================================";
}
}
} 


Please find how to enter employee details,update,remove,displaying using hashmap wherin employee dept is taken as a key and id,name,salary is stored in the values using arraylist ......Currently Iam struck at CASE 2...PLease help me out

在案例1中我试图插入员工详细信息,并在案例2中iam尝试修改值。如果我尝试将相同的部门放入任何详细信息,则应创建新条目...即使我应该能够修改它,我没有得到怎么做..请帮助我

3 个答案:

答案 0 :(得分:0)

你的问题不明确, 用于在列表中设置员工

Arraylist<Employee> list = new ArrayList<Employees>;
Employee emp = new Employee();
emp.setname("John");
emp.setEmpCode(1);
list.add(emp);

要从列表中获取员工,请使用此功能。

Employee emp = list.getItem(0);// change 0 to your position or make a loop to get all employees

答案 1 :(得分:0)

import java.util.*; import java.util.concurrent.TimeUnit;

class Employee{

public String name; public String city; public String email; public String phone; public String age;`enter code here`

public Employee(String name,String city,String email,String age,String phone) { super(); this.name = name; this.city=city; this.email = email; this.age=age; this.phone = phone; }

public String getPhone() { return phone; }

public String getAge() { return age; }

public String getName() { return name; } public String getEmail() { return email; } public String getAddress() { return city; } public void setPhone(String phone) { this.phone = phone; } }

public class Display_Employee implements Comparator {

/* public int compare(Employee o1, Employee o2) { // in the case if when age is Integer return o1.getAge() - o2.getAge(); }*/ /public int compare(Employee o1, Employee o2) { //for short by Name return o1.getFirstName().compareTo(o2.getFirstName()); }/ public int compare(Employee o1, Employee o2) { //for short by Name return o1.getAge().compareTo(o2.getAge()); } public static void main(String[] args) throws Exception { int choice,i,size,mob_serch_result,email_serch_result; String name,city,email,phone,age,mob_serch,email_serch,update_search,choice_update,update_search_email; String name1,city1,email1,phone1,age1;

Scanner sc = new Scanner(System.in);

// Map< String, List > mapPhone=new HashMap< String, List >(); // Map< String, List > mapEmail=new HashMap< String, List >(); Map< String, Integer > mapEmail=new HashMap< String, Integer >(); Map< String, Integer > mapPhone=new HashMap< String, Integer >(); List list = new ArrayList(); //User Input /*System.out.println("How much details you want to Enter"); size=sc.nextInt() ; for(i=0;i<size;i++) {

System.out.println("\t\t\t\tEnter the "+(i+1)+" Employee Details \t\t\t\t\n"); System.out.println("Enter the name "); name=sc.next(); System.out.println("Enter the City "); city=sc.next(); System.out.println("Enter the email "); email=sc.next(); System.out.println("Enter the age "); age=sc.next(); System.out.println("Enter the MobNum "); phone=sc.next(); Employee employee=new Employee(name,city,email,age,phone); list.add(employee); mapPhone.put(phone,i); mapEmail.put(email,i); }*/

//Testing input Employee employee1=new Employee("Raj","Mumbai","raj@yahoo.com","27","7499031600"); list.add(employee1); phone="7499031600"; email="raj@yahoo.com"; mapPhone.put(phone,0); mapEmail.put(email,0); Employee employee2=new Employee("Rekha","Chennai","rekha@hotmail.com","24","9598551664"); list.add(employee2); phone1="9598551664"; email1="rekha@hotmail.com"; mapPhone.put(phone1,1); mapEmail.put(email1,1); Employee employee3=new Employee("Ram","Siliguri","ram@outlook.com","55","8563878480"); list.add(employee3); String phone2="8563878480"; String email2="ram@outlook.com"; mapPhone.put(phone2,2); mapEmail.put(email2,2); Employee employee4=new Employee("Lakhan","Bhopal","Lakhan@outlook.com","30","8563050698"); list.add(employee4); String phone3="8563050698"; String email3="Lakhan@outlook.com"; mapPhone.put(phone3,3); mapEmail.put(email3,3); Employee employee5=new Employee("Bharat","Ayodhya","Bharat@outlook.com","35","9044669201"); list.add(employee5); String phone4="9044669201"; String email4="Bharat@outlook.com"; mapPhone.put(phone4,4); mapEmail.put(email4,4 );

for (Employee s : list) { System.out.println("\n\n"+s.getName()+" " +s.getAddress()+" " +s.getAge()+" " +s.getEmail()+" " +s.getPhone()); } /Seaching part/ while(true) { System.out.println("==========================================================================================================="); System.out.println("Press 1 for Sort by age\nPress 2 for find the Person by mobile number\nPress 3 for find the Person by email\nPress 4 for update the mobiel number"); choice= sc.nextInt(); sc.nextLine(); switch(choice) { case 1: Collections.sort(list, new Display_Employee ()); for (Employee s : list) { System.out.println(" Name : "+s.getName()+" City : " +s.getAddress()+" Age : " +s.getAge()+" Email : " +s.getEmail()+" Phone : " +s.getPhone()); } System.out.println("===========================================================================================================");

break;
/*case 1 is Running perfectly*/

case 2:
System.out.println("Enter the mobiel number of Employee ");
mob_serch=sc.next();
mob_serch_result=mapPhone.get(mob_serch);
System.out.println("Mobile mob_serch_result is "+mob_serch_result);
Employee Emp_phone_obj=(Employee)list.get(mob_serch_result);
System.out.println("Employee Name:  "+Emp_phone_obj.getName()+"  Email : "+Emp_phone_obj.getEmail()+"  Age : "+Emp_phone_obj.getAge()+"  City : "+Emp_phone_obj.getAddress());
break;                                                                /*case 2 is Running perfectly*/
case 3:
System.out.println("Enter the emailId of Employee ");
email_serch=sc.nextLine();
email_serch_result=mapEmail.get(email_serch);
System.out.println("Email_serch_result is "+email_serch_result);
Employee Emp_email_obj=(Employee)list.get(email_serch_result);
System.out.println("Employee Name :  "+Emp_email_obj.getName()+"  Email : "+Emp_email_obj.getEmail()+"  Age : "+Emp_email_obj.getAge()+"  City : "+Emp_email_obj.getAddress());
break;
case 4:
System.out.println("Enter the mobile number ");
update_search=  sc.next();
mob_serch_result=mapPhone.get(update_search);
Employee Emp_update_obj=(Employee)list.get(mob_serch_result);
update_search_email=Emp_update_obj.getEmail();
System.out.println("\t\t\t\t\t\t\tYour Search Result");
System.out.println(" Name:  "+Emp_update_obj.getName()+"  Email : "+Emp_update_obj.getEmail()+"  Age : "+Emp_update_obj.getAge()+"  City : "+Emp_update_obj.getAddress());
System.out.println("\t\t\t\t\t\t\tPress Y or y for Confirm and Update\tPress N or n for Cancle");
choice_update=sc.next();
if(choice_update.equalsIgnoreCase("Y")||choice_update.equalsIgnoreCase("y"))
{
  mapPhone.remove(update_search);
  mapEmail.remove(update_search_email);
  list.remove(Emp_update_obj);
  System.out.println("Enter the Firstname ");
  name1=sc.next();
  System.out.println("Enter the City ");
  city1=sc.next();
  System.out.println("Enter the email ");
  email1=sc.next();
  System.out.println("Enter the age ");
  age1=sc.next();
  System.out.println("Enter the MobNum ");
  phone1=sc.next();
  Employee employee=new Employee(name1,city1,email1,age1,phone1);
  list.add(employee);
  mapPhone.put(phone1,  mob_serch_result);
  mapEmail.put(email1,  mob_serch_result);
}
break;
default:
System.out.println("\n\t****Please give a valid Input****\t\n");
break;
} }

}//End of main() method }//end of DisplayArrayList class

答案 2 :(得分:0)

import java.util.*;
import java.util.concurrent.TimeUnit;
class Employee implements Comparator<Employee> {
  private String name;
  private String city;
  private String email;
  private String phone;
  private int age;
  public Employee(){}
    public void setAge(Integer age) {
      this.age = age;
    }
    public void setName(String Name) {
      this.name = Name;
    }
    public void setEmail(String email) {
      this.email = email;
    }
    public void setPhone(String phone) {
      this.phone=null;
      this.phone = phone;
    }
    public void setCity(String city) {
      this.city = city;
    }
    public String getPhone() {
      return phone;
    }

    public int getAge() {
      return age;
    }

    public String getName() {
      return name;
    }
    public String getEmail() {
      return email;
    }
    public String getAddress() {
      return city;
    }
    public int compare(Employee o1, Employee o2) {        //  in the case if when age is Integer
      return o1.getAge() - o2.getAge();
    }

  }
  public class Display_Employee  {
    public static void main(String[] args)
    {
      int choice,i,size,mob_serch_result,email_serch_result,age,age1;
      String name,city,email,phone,mob_serch,email_serch,update_search,choice_update,update_search_email;
      String name1,city1,email1,phone1,Update_choice,choice_update_for_phone,choice_update_for_email;
      Scanner sc = new Scanner(System.in);
      Map< String, Integer > mapEmail=new HashMap< String, Integer >();
      Map< String, Integer > mapPhone=new HashMap< String, Integer >();
      List<Employee> list = new ArrayList<Employee>();
      /*
      System.out.println("How much details you want to Enter");
      size=sc.nextInt()  ;
      for(i=0;i<size;i++)
      {
      System.out.println("\t\t\t\tEnter the  "+(i+1)+"  Employee Details \t\t\t\t\n");
      System.out.println("Enter the name ");
      name=sc.next();
      System.out.println("Enter the City ");
      city=sc.next();
      System.out.println("Enter the email ");
      email=sc.next();
      System.out.println("Enter the age ");
      age=sc.nextInt();
      System.out.println("Enter the MobNum ");
      phone=sc.next();
      Employee employee=new Employee();
      employee.setAge(age);
      employee.setName(name);
      employee.setCity(city);
      employee.setPhone(phone);
      employee.setEmail(email);
      list.add(employee);
      mapPhone.put(phone,i);
      mapEmail.put(email,i);
    }*/

    /*Test input Start*/
    name="Anoop";
    city="kanpur";
    email="anoop@gmail.com";
    age=23;
    phone="9415418279";
    Employee employee=new Employee();
    employee.setAge(age);
    employee.setName(name);
    employee.setCity(city);
    employee.setPhone(phone);
    employee.setEmail(email);
    list.add(employee);
    mapPhone.put(phone,0);
    mapEmail.put(email,0);

    /*Test input end*/

    for (Employee s : list)
    {
      System.out.println("\n\n"+s.getName()+" " +s.getAddress()+" " +s.getAge()+" " +s.getEmail()+" " +s.getPhone());
    }
    while(true)
    {
      System.out.println("===========================================================================================================");
      System.out.println("Press 1 for Sort by age\nPress 2 for find the Person by mobile number\nPress 3 for find the Person by email\nPress 4 for update the mobiel number\nPress 5 for print list and free the varible ");
      choice= sc.nextInt();
      sc.nextLine();
      switch(choice)
      {
        case 1:
        Collections.sort(list,new Employee());
        for (Employee s : list)
        {
          System.out.println("  Name : "+s.getName()+"  City  : " +s.getAddress()+" Age : " +s.getAge()+" Email : " +s.getEmail()+"    Phone : " +s.getPhone());
        }
        System.out.println("===========================================================================================================");

        break;
        case 2:
        System.out.println("Enter the mobile number of Employee ");
        mob_serch=sc.next();
        mob_serch_result=mapPhone.get(mob_serch);
        System.out.println("Mobile mob_serch_result is "+mob_serch_result);
        Employee Emp_phone_obj=(Employee)list.get(mob_serch_result);
        System.out.println("Employee Name:  "+Emp_phone_obj.getName()+"  Email : "+Emp_phone_obj.getEmail()+"  Age : "+Emp_phone_obj.getAge()+"  City : "+Emp_phone_obj.getAddress());
        break;
        case 3:
        System.out.println("Enter the emailId of Employee ");
        email_serch=sc.nextLine();
        email_serch_result=mapEmail.get(email_serch);
        System.out.println("Email_serch_result is "+email_serch_result);
        Employee Emp_email_obj=(Employee)list.get(email_serch_result);
        System.out.println("Employee Name :  "+Emp_email_obj.getName()+"  Email : "+Emp_email_obj.getEmail()+"  Age : "+Emp_email_obj.getAge()+"  City : "+Emp_email_obj.getAddress());
        break;
        case 4:
        System.out.println("\033[H\033[2J"+"\n\n\t\t\tPress P to Edit PhoneNumber & Press E for Edit Email ");
        choice_update_for_phone=sc.next();
        choice_update_for_email=choice_update_for_phone;
        if(choice_update_for_phone.equalsIgnoreCase("P")||choice_update_for_phone.equalsIgnoreCase("p"))
        {
          System.out.println("Enter the Old Mobile number ");
          update_search=sc.next();
          mob_serch_result=mapPhone.get(update_search);
          System.out.println("  mob_serch_result "+mob_serch_result);
          Employee Emp_update_obj_phone=(Employee)list.get(mob_serch_result);
          System.out.println("\t\t\t\t\t\t\tYour Search Result");
          System.out.println(" Name:  "+Emp_update_obj_phone.getName()+"  Email : "+Emp_update_obj_phone.getEmail()+"  Age : "+Emp_update_obj_phone.getAge()+"  City : "+Emp_update_obj_phone.getAddress());
          System.out.println("\t\t\t\tPress Y or y for Confirm and Update\tPress N or n for Cancle");
          choice_update=sc.next();
          if(choice_update.equalsIgnoreCase("Y")||choice_update.equalsIgnoreCase("y"))
          {
            System.out.println("Enter the New MobNum ");
            phone1=sc.next();
            Emp_update_obj_phone.setPhone(phone1);
            System.out.println("\t\t\t\tupdated entry is :\n");
            mapPhone.put(phone1,mob_serch_result);                                        //Map update
            System.out.println(" Name:  "+Emp_update_obj_phone.getName()+"  Email : "+Emp_update_obj_phone.getEmail()+"  Age : "+Emp_update_obj_phone.getAge()+"  City : "+Emp_update_obj_phone.getAddress()+"  PhoneNumber : "+Emp_update_obj_phone.getPhone());
          }
        }
        else if(choice_update_for_email.equalsIgnoreCase("e")||choice_update_for_email.equalsIgnoreCase("E"))
        {
          System.out.println("Enter the old email ");
          update_search_email=  sc.next();
          email_serch_result=mapEmail.get(update_search_email);
          System.out.println(" email_serch_result = "+  email_serch_result);
          Employee Emp_update_obj_email=(Employee)list.get(email_serch_result);
          System.out.println("\t\t\t\t\t\t\tYour Search Result");
          System.out.println(" Name:  "+Emp_update_obj_email.getName()+"  Email : "+Emp_update_obj_email.getEmail()+"  Age : "+Emp_update_obj_email.getAge()+"  City : "+Emp_update_obj_email.getAddress());
          System.out.println("\t\t\t\t\t\t\tPress Y or y for Confirm and Update\tPress N or n for Cancle");
          choice_update=sc.next();
          if(choice_update.equalsIgnoreCase("Y")||choice_update.equalsIgnoreCase("y"))
          {
            System.out.println("Enter the new email ");
            email1=sc.next();
            Emp_update_obj_email.setEmail(email1);
            mapEmail.put(email1,email_serch_result);
          }
        }
        break;
        case 5:
        Iterator<Employee> itr=list.iterator();

        while(itr.hasNext())
        {
          Employee employe = itr.next();
          System.out.print("  name:  "+employe.getName());
          System.out.print("  email:  "+employe.getEmail());
          System.out.print("  age:  "+employe.getAge());
          System.out.println("  phone:  "+employe.getPhone());
        }
        System.gc();
        break;
        default:
        System.out.println("\n\t****Please give a valid Input****\t\n");
        break;
      }

    }
  }//End of main() method
}//end of DisplayArrayList class