1.bean层:定义Worker类型
package com.manage.bean;
import java.io.Serializable;
public class Worker implements Serializable{
// 员工编号 员工姓名 员工性别 工资 入职时间
private String id, name, sex, time;
private double salary;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Worker(String id, String name, String sex, double salary, String time) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.time = time;
this.salary = salary;
}
public Worker() {
super();
}
}
2.DataBase层:从文件中存储调用数据,初始化数据
package com.manage.data;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.manage.bean.Worker;
public class DataBase implements Serializable{
List<Worker> list = new ArrayList<>();
File file = new File("src/worker.txt");
public List<Worker> getWorker(){
ObjectInputStream ois = null;
if(file.exists()) {
try {
ois = new ObjectInputStream(new FileInputStream(file));
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
List<Worker> list = (List<Worker>) ois.readObject();
try {
return list;
} catch (Exception e) {
e.printStackTrace();
Init();
}finally {
if(ois!=null) {
ois.close();
}
}
} catch (Exception e1) {
e1.printStackTrace();
Init();
}
}
} else {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Init();
}
return list;
}
// 有如下数据:
// 员工编号 员工姓名 员工性别 工资 入职时间
// 10011 李云龙 男 18000 2022-10-01
// 10012 李真棒 女 16000 2022-08-22
// 10013 夏芒 男 9000 2022-10-11
// 10014 刘码 女 8000 2022-09-12
private void Init() {
Worker w1 = new Worker("10011", "李云龙", "男", 18000, "2022-10-01");
Worker w2 = new Worker("10012", "李真棒", "女", 16000, "2022-08-22");
Worker w3 = new Worker("10013", "夏芒", "男", 9000, "2022-10-11");
Worker w4 = new Worker("10014", "刘码", "女", 8000, "2022-09-12");
list.add(w1);
list.add(w2);
list.add(w3);
list.add(w4);
WriteData(list);
}
/**
* 将list重写入文件
*/
public void WriteData(List<Worker> list1){
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(list1);
} catch (Exception e) {
e.printStackTrace();
}finally {
if(oos!=null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
3.Dao层:筛选数据
package com.manage.dao;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import com.manage.bean.Worker;
import com.manage.data.DataBase;
public class WorkerDao implements Serializable{
DataBase data = new DataBase();
List<Worker> list = data.getWorker();
/**
* 1.查询所有员工信息
*/
public List<Worker> querryAllMasage() {
return list;
}
/**
* 判断是否重复
*/
public boolean judgeRep(String id) {
return list.stream().anyMatch(i->i.getId().equals(id));
}
/**
* 2.新增一条员工信息
*/
public void addMassage(Worker wo) {
list.add(wo);
data.WriteData(list);
}
/**
* 3.根据员工编号删除指定员工信息
*/
public void delById(String id) {
list.removeIf(i->i.getId().equals(id));
data.WriteData(list);
}
/**
* 4.查询工资在5000 - 10000范围的员工有哪些
*/
public Iterable<Worker> querrySalRange(double i, double j) {
return list.stream().filter(k->k.getSalary()>=i&&k.getSalary()<=j)
.collect(Collectors.toList());
}
/**
* 5.查询10月份入职的员工有哪些
*/
public Iterable<Worker> querryByMonth(int i) {
return list.stream().filter(k->k.getTime().contains("-"+i+"-")).collect(Collectors.toList());
}
/**
* 6.按照性别显示员工信息
*/
public Map<String, List<Worker>> showBySex() {
return list.stream().collect(Collectors.groupingBy(Worker :: getSex));
}
/**
* 7.查询姓李员工有哪些?
*/
public List<Worker> querryByFirstName(String string) {
return list.stream().filter(i->i.getName().startsWith(string)).collect(Collectors.toList());
}
}
4.view层:输入输出数据
package com.manage.view;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import com.manage.bean.Worker;
import com.manage.dao.WorkerDao;
public class View implements Serializable{
static Scanner scan = new Scanner(System.in);
static WorkerDao dao = new WorkerDao();
public static void main(String[] args) {
boolean isloop = true;
while(isloop) {
System.out.println("按0.退出");
System.out.println("按1.查询所有员工信息");
System.out.println("按2.新增一条员工信息");
System.out.println("按3.根据员工编号删除指定员工信息");
System.out.println("按4.查询工资在5000 - 10000范围的员工有哪些");
System.out.println("按5.查询10月份入职的员工有哪些");
System.out.println("按6.按照性别显示员工信息");
System.out.println("按7.查询姓李员工有哪些");
System.out.println("请输入操作指令:");
int in = scan.nextInt();
switch (in) {
case 0:
isloop = false;
System.out.println("退出成功");
break;
case 1:
// 1.查询所有员工信息
querryAllMasage();
break;
case 2:
// 2.新增一条员工信息
addMassage();
break;
case 3:
// 3.根据员工编号删除指定员工信息
delById();
break;
case 4:
// 4.查询工资在5000 - 10000范围的员工有哪些?
querrySalRange();
break;
case 5:
// 5.查询10月份入职的员工有哪些
querryByMonth();
break;
case 6:
// 6.按照性别显示员工信息
showBySex();
break;
case 7:
// 7.查询姓李员工有哪些?
querryByFirstName();
break;
default:
System.out.println("输入指令有误");
break;
}
}
}
/**
* 7.查询姓李员工有哪些?
*/
private static void querryByFirstName() {
System.out.println("姓李的员工信息如下:");
System.out.println("员工编号\t员工姓名\t员工性别\t工资\t入职时间");
dao.querryByFirstName("李").forEach(i->System.out.println(i.getId()+"\t"+i.getName()+"\t"+i.getSex()+"\t"+i.getSalary()+"\t"+i.getTime()));
}
/**
* 6.按照性别显示员工信息
*/
private static void showBySex() {
Map<String, List<Worker>> map = dao.showBySex();
System.out.println("共有"+map.size()+"种性别");
map.keySet().forEach(j->{
System.out.println("其中性别为"+j+"的员工信息如下:");
System.out.println("员工编号\t员工姓名\t员工性别\t工资\t入职时间");
map.get(j).forEach(i->System.out.println(i.getId()+"\t"+i.getName()+"\t"+i.getSex()+"\t"+i.getSalary()+"\t"+i.getTime()));
});
}
/**
* 5.查询10月份入职的员工有哪些
*/
private static void querryByMonth() {
System.out.println("10月份入职的员工信息如下:");
System.out.println("员工编号\t员工姓名\t员工性别\t工资\t入职时间");
dao.querryByMonth(10).forEach(i->System.out.println(i.getId()+"\t"+i.getName()+"\t"+i.getSex()+"\t"+i.getSalary()+"\t"+i.getTime()));
}
/**
* 4.查询工资在5000 - 10000范围的员工有哪些
*/
private static void querrySalRange() {
System.out.println("工资在5000 - 10000范围的员工信息如下:");
System.out.println("员工编号\t员工姓名\t员工性别\t工资\t入职时间");
dao.querrySalRange(5000, 10000).forEach(i->System.out.println(i.getId()+"\t"+i.getName()+"\t"+i.getSex()+"\t"+i.getSalary()+"\t"+i.getTime()));
}
/**
* 3.根据员工编号删除指定员工信息
*/
private static void delById() {
System.out.println("请输入员工编号:");
String id = scan.next();
if(dao.judgeRep(id)) {
dao.delById(id);
System.out.println("删除成功");
}else System.out.println("输入的编号不存在");
}
/**
* 2.新增一条员工信息
*/
private static void addMassage() {
System.out.println("请输入员工编号:");
String id = scan.next();
if(!dao.judgeRep(id)) {
System.out.println("请输入员工姓名:");
String name = scan.next();
System.out.println("请输入员工性别:");
String sex = scan.next();
System.out.println("请输入工资:");
double salary = scan.nextDouble();
System.out.println("请输入入职时间:");
String time = scan.next();
Worker wo= new Worker(id, name, sex, salary, time);
dao.addMassage(wo);
System.out.println("添加成功");
}else System.out.println("输入的编号已存在");
}
/**
* 1.查询所有员工信息
*/
private static void querryAllMasage() {
// 员工编号 员工姓名 员工性别 工资 入职时间
System.out.println("员工编号\t员工姓名\t员工性别\t工资\t入职时间");
List<Worker> list = new ArrayList<>();
dao.querryAllMasage().forEach(i->System.out.println(i.getId()+"\t"+i.getName()+"\t"+i.getSex()+"\t"+i.getSalary()+"\t"+i.getTime()));
}
}