Employee.java

 package cn.employee_io;

 public class Employee {
private String empId;
private String name;
private int age;
private double salary; public Employee() {
super();
} public Employee(String empId, String name, int age, double salary) {
super();
this.empId = empId;
this.name = name;
this.age = age;
this.salary = salary;
} public String getEmpId() {
return empId;
} public void setEmpId(String empId) {
this.empId = empId;
} 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;
} public double getSalary() {
return salary;
} public void setSalary(double salary) {
this.salary = salary;
} @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((empId == null) ? 0 : empId.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 (empId == null) {
if (other.empId != null)
return false;
} else if (!empId.equals(other.empId))
return false;
return true;
} public String toString() {
return empId + "," + name + "," + age+ "," + salary;
} }
 Service.java

 package cn.employee_io;

 import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedList;
import java.util.List; public class Service {
List<Employee> list = new LinkedList<Employee>();
File file = null;
ObjectInputStream ois = null;
ObjectOutputStream oos = null; public Service() throws IOException, ClassNotFoundException {
reader();
} /**
* 读文件
* @throws IOException
* @throws ClassNotFoundException
*/
public void reader() throws IOException, ClassNotFoundException {
/* file = new File("src/cn/employee_io/emp.txt");
if (!file.exists()) {
file.createNewFile();
}
ois = new ObjectInputStream(new FileInputStream(file));
list = (List<Employee>) ois.readObject(); // 给list赋值
ois.close();*/
BufferedReader br=new BufferedReader(new FileReader("src/cn/employee_io/emp.txt"));
String empStr=null;
while((empStr=br.readLine())!=null){
String[] empStrs=empStr.split(",");
list.add(new Employee(empStrs[0],empStrs[1],Integer.parseInt(empStrs[2]),Double.parseDouble(empStrs[3])));
}
br.close();
} /**
* 写文件
* @throws IOException
* @throws ClassNotFoundException
*/
public void writer() throws IOException, ClassNotFoundException {
file = new File("src/cn/employee_io/emp.txt");
if (!file.exists()) {
file.createNewFile();
}
oos = new ObjectOutputStream(new FileOutputStream(file));
for(Employee e:list){
oos.writeObject(e.toString()+"\r\n"); // 将list值写进文件里去
}
oos.close();
} /**
* 添加员工
* @param e
* @throws ClassNotFoundException
* @throws IOException
*/
public void add(Employee e) throws ClassNotFoundException, IOException {
if (!list.contains(e)) {
list.add(e);
writer();
System.out.println("添加成功!");
} else {
System.out.println("添加失败!");
}
} /**
* 查询所有员工
*/
public void queryAll(){
System.out.println("编号 "+"姓名 "+"年龄 "+"薪资");
for(Employee e:list){
System.out.println(e.getEmpId()+" "+e.getName()+" "+e.getAge()+" "+e.getSalary());
}
} /**
* 查询单个员工
* @param empId
*/
public void query(String empId){
for(int i=0;i<list.size();i++){
if(list.get(i).getEmpId().equals(empId)){
System.out.println("编号 "+"姓名 "+"年龄 "+"薪资");
System.out.println(list.get(i).getEmpId()+" "+list.get(i).getName()+" "+list.get(i).getAge()+" "+list.get(i).getSalary());
}
if((i==list.size()-1)&&(!list.get(i).getEmpId().equals(empId))){
System.out.println("不存在该员工!");
}
}
} /**
* 删除员工
* @throws IOException
* @throws ClassNotFoundException
*/
public void delete(String empId) throws ClassNotFoundException, IOException{
for(int i=0;i<list.size();i++){
if(list.get(i).getEmpId().equals(empId)){
list.remove(list.get(i));
writer();
System.out.println("删除成功!");
break;
}
if((i==list.size()-1)&&(!list.get(i).getEmpId().equals(empId))){
System.out.println("删除失败!");
}
}
} /**
* 修改员工
* @throws IOException
* @throws ClassNotFoundException
*
*/
public void update(Employee emp) throws ClassNotFoundException, IOException{
for(int i=0;i<list.size();i++){
if(list.get(i).getEmpId().equals(emp.getEmpId())){
list.get(i).setName(emp.getName());
list.get(i).setAge(emp.getAge());
list.get(i).setSalary(emp.getSalary());
writer();
System.out.println("修改成功!");
}
if((i==list.size()-1)&&(!list.get(i).getEmpId().equals(emp.getEmpId()))){
System.out.println("修改失败!");
}
}
}
}
 TestEmp.java

 package cn.employee_io;

 import java.io.IOException;
import java.util.Scanner; public class TestEmp {
static Scanner sc = new Scanner(System.in); static String empId;
static String name;
static int age;
static double salary;
static int num; public static void main(String[] args) throws ClassNotFoundException, IOException {
Service s=new Service(); ok: for (;;) {
printOptions();
num = sc.nextInt(); if (num < 1 || num > 6) {
System.out.println("输入有误,将重新开始选择!");
break ok;
} switch (num) {
case 1:
printEmpNo();
printName();
s.add(new Employee(empId,name,age,salary));
break;
case 2:
s.queryAll();
break;
case 3:
printEmpNo();
s.query(empId);
break;
case 4:
printEmpNo();
s.delete(empId);
break;
case 5:
printEmpNo();
printName();
s.update(new Employee(empId,name,age,salary));
break;
case 6:
return;
}
}
} public static void printOptions() {
System.out.println("***员工管理系统***");
System.out.println("1.添加员工");
System.out.println("2.查询所有员工");
System.out.println("3.查询员工");
System.out.println("4.删除员工");
System.out.println("5.修改员工");
System.out.println("6.退出");
System.out.println("请输入你要进行的操作:");
} public static void printEmpNo() {
System.out.println("请输入员工编号:");
empId = sc.next();
} public static void printName() {
System.out.println("请输入员工姓名:");
name = sc.next();
System.out.println("请输入员工年龄:");
age = sc.nextInt();
System.out.println("请输入员工薪资:");
salary=sc.nextDouble();
}
}

最新文章

  1. OC-03类的声明和实现
  2. Reactor模式与Proactor模式
  3. Same Tree
  4. shareSDK集成步骤
  5. HDP2.4安装(四):ambari安装
  6. Windows 小技巧: 變更輸入法順序
  7. 07_Java8新增的Lambda表达式
  8. lua基金会【五岁以下儿童】I/O文件操作
  9. 踩一踩微信小程序开发的坑---tabBar
  10. TestNG并行测试
  11. [LeetCode] Partition to K Equal Sum Subsets 分割K个等和的子集
  12. 原生JS实现轮播效果
  13. Angularjs中config中置入以下拦截器
  14. python subprocess pipe 实时输出日志
  15. EL表达式获取请求
  16. shell比较浮点数和整数
  17. xss:利用编码绕过(新手向)
  18. 刷题中熟悉Shell命令之Tenth Line和Transpose File [leetcode]
  19. hadoop jar x.jar 执行过程
  20. 学习LSM(Linux security module)之二:编写并运行一个简单的demo

热门文章

  1. SQL 连接(JOIN)
  2. android 多进程 Binder AIDL Service
  3. asp.net mvc的权限管理设计
  4. Angular45
  5. OFbiz实体引擎
  6. STL vector的介绍(1)
  7. MySQL 存储过程传參之in, out, inout 參数使用方法
  8. ios archives 出现的是other items而不是iOS Apps的解决方案
  9. 设计模式-(17)策略模式 (swift版)
  10. YTU 2918: Shape系列-4