package cn.employee_io;

 import java.io.Serializable;

 public class Employee implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
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;
} }

Employee.java

 package cn.employee_io;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
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;
boolean flag=false; public Service() throws IOException, ClassNotFoundException {
reader();
} /**
* 读文件
* @throws IOException
* @throws ClassNotFoundException
*/
@SuppressWarnings("unchecked")
public void reader() throws IOException, ClassNotFoundException {//ObjectInputStream先写后读
file = new File("src/cn/employee_io/emp.txt");
if (!file.exists()) {
writer();
}else{
ois = new ObjectInputStream(new FileInputStream(file));
list = (List<Employee>) ois.readObject(); // 给list赋值
ois.close();
}
} /**
* 写文件
* @throws IOException
* @throws ClassNotFoundException
*/
public void writer() throws IOException, ClassNotFoundException {
file = new File("src/cn/employee_io/emp.txt");
oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(list);
oos.flush();
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());
}
}
} /**
* 删除员工
* @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("修改失败!");
}
}
}
}

Service.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();
}
}

TestEmp.java

最新文章

  1. [C1] C1ComboBox 的非编辑状态优化
  2. linux 学习干货
  3. 如何决定DCOM是否可用
  4. php装饰器模式完成文章编辑
  5. scp 使用
  6. ecstore2.0数据库词典
  7. HDU 3567 Eight II BFS预处理
  8. js获取json数据
  9. poj2955:括号匹配,区间dp
  10. fork()子进程与waitpid()
  11. OpenGL------三维变换
  12. python——面向对象基础
  13. HTTP架构介绍(1) Web服务器和代理服务器
  14. http强转https websocket
  15. jQuery.rotate.js(控制图片转动)
  16. Ubuntu18.04下配置Nginx+RTMP服务器,实现点播/直播/录制功能
  17. SQLServer 2014 本地机房HA+灾备机房DR解决方案
  18. django在读取数据库时未筛选到符合条件的记录会报错
  19. 【转】【java】论integer是地址传递还是值传递
  20. [Windows Azure] How to Scale an Application

热门文章

  1. @Retention n. 保留
  2. Java编程50题
  3. 避免使用vector&amp;lt;bool&amp;gt;
  4. 把A表中的a字段和b字段数据 复制到B表中的aa字段和bb字段
  5. JS日历控件 灵活设置: 精确的时分秒.
  6. 使用7zip压解各种文件的经常使用命令
  7. 记一次UICollectionView中visibleCells的坑
  8. asp.net mvc + javascript生成下载文件
  9. Class.forName() 详解
  10. java 类属性、方法加载的顺序