写一个员工类Employee,有员工编号,姓名,年龄。 用map存储公司的员工,员工作为key,薪水作为value

要求:
输出所有员工的薪水 
员工离职
员工加薪

按员工年龄从大到小输出

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));
		}
		
	}
	

}


发布了111 篇原创文章 · 获赞 40 · 访问量 21万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览