点击上方蓝色文字,获取更多推文。
使用Map接口的实现类完成员工工资(姓名-工资)的摸拟:
1)、添加几条信息
2)、列出所有的员工姓名
3)、列出所有员工姓名及其工资
4)、删除名叫“Tom”的员工信息
5)、输出Jack的工资,并将其工资改为1500元
6)、将所有工资低于1000元的员工的工资上涨20%
package HomeworkThree;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class HomeworkOne {
public static void main(String[] arg) {
Map<Integer, Employee> map = new HashMap<Integer, Employee>();
map.put(1, new Employee("张三", 30000));
map.put(2, new Employee("李四", 626));
map.put(3, new Employee("王五", 20651));
map.put(4, new Employee("Tom", 206));
for (Map.Entry<Integer, Employee> item : map.entrySet()) {
System.out.println("员工编号:" + item.getKey() + " " + "员工姓名:" + item.getValue().getName());
}
for (Entry<Integer, Employee> item : map.entrySet()) {
System.out.println("员工姓名:" + item.getValue().getName() + " " + "员工工资:" + item.getValue().getSolary());
}
// 删除名叫“Tom”的员工信息
System.out.println("=======删除名叫“Tom”的员工信息=====");
for (Entry<Integer, Employee> item : map.entrySet()) {
if (item.getValue().getName().equals("Tom")) {
int key = item.getKey();
map.remove(key);
}
}
System.out.println(map);
// 输出王五的工资,并将其工资改为1500元
System.out.println("===========输出王五的工资,并将其工资改为1500元==========");
for (Entry<Integer, Employee> item : map.entrySet()) {
if (item.getValue().getName().equals("王五")) {
int key = item.getKey();
map.put(key, new Employee("王五", 1500));
System.out.println("王五的工资:" + item.getValue().getSolary());
}
}
// 将所有工资低于1000元的员工的工资上涨20%
System.out.println("============将所有工资低于1000元的员工的工资上涨20%==============");
for (Entry<Integer, Employee> item : map.entrySet()) {
if (item.getValue().getSolary() < 1000) {
item.getValue().setSolary(item.getValue().getSolary() + (item.getValue().getSolary() * 20) / 100);
System.out.println(item);
}
}
}
private static Employee Employee(String string, int i) {
// TODO Auto-generated method stub
return null;
}
}
class Employee<Stirng, Integer> {
private String name;
private int solary;
public Employee() {
}
public Employee(String name, int solary) {
this.name = name;
this.solary = solary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSolary() {
return solary;
}
public void setSolary(int solary) {
this.solary = solary;
}
@Override
public String toString() {
return "employee [name=" + name + ", solary=" + solary + "]";
}
}
最后,一首歌送给大家
家人们,动一动你们的小手,右下方点赞点关注