要求:
輸出所有員工的薪水
員工離職
員工加薪
輸出所有員工的薪水
員工離職
員工加薪
按員工年齡從大到小輸出
public class Employee implements Comparable<Employee>{
private String no;
private String name;
private int age;
public Employee() {
super();
}
public Employee(String no, String name, int age) {
super();
this.no = no;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "編號:" + no + ", 姓名:" + name + ", 年齡:" + age;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int compareTo(Employee o) {
// TODO Auto-generated method stub
return this.age - o.age;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((no == null) ? 0 : no.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (no == null) {
if (other.no != null)
return false;
} else if (!no.equals(other.no))
return false;
return true;
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<Employee,Integer> map = new HashMap<Employee, Integer>();
Employee em = new Employee();
map.put(new Employee("A001", "張員工", 19), 3800);
map.put(new Employee("A002", "趙員工", 23), 3500);
map.put(new Employee("A005", "容員工", 21), 4000);
map.put(new Employee("A003", "李員工", 20), 4600);
map.put(new Employee("A004", "翟員工", 22), 3200);
System.out.println("***遍歷員工信息***");
Set<Employee> set = map.keySet();
for(Employee key : set) {
System.out.println(key+ ", 工資 : " + map.get(key));
}
System.out.println("***員工加薪***");
//所有員工加薪
/*
for(Employee key : set) {
System.out.println(key+ ", 工資 : " + map.get(key)*2);
}*/
map.put(new Employee("A004", "翟員工", 22), 3800);
for(Employee key : set) {
System.out.println(key+ ", 工資 : " + map.get(key));
}
System.out.println("***按員工年齡從大到小排序***");
//先從Map轉化爲list
List<Map.Entry<Employee, Integer>> list = new ArrayList<Map.Entry<Employee,Integer>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<Employee, Integer>>() {
public int compare(Map.Entry<Employee, Integer> o1,
Map.Entry<Employee, Integer> o2) {
return ( o1.getKey().getAge()-o2.getKey().getAge());
}
});
for (int i = 0; i < list.size(); i++) {
Entry<Employee, Integer> ent=list.get(i);
System.out.println(ent.getKey()+", 工資 : "+ent.getValue());
}
System.out.println("***員工離職***");
map.remove(new Employee("A004", "翟員工", 22));
// System.out.println(map);
for(Employee key : set) {
System.out.println(key+ ", 工資 : " + map.get(key));
}
}
}