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

员工管理系统(集合与IO流的结合使用 beta3.0 BufferedReader / ObjectOutputStream)的更多相关文章

  1. 员工管理系统(集合与IO流的结合使用 beta2.0 ObjectInputStream/ ObjectOutputStream)

    package cn.employee; import java.io.Serializable; public class Employee implements Serializable{ pri ...

  2. 员工管理系统(集合与IO流的结合使用 beta5.0 BufferedReader/ BufferedWriter)

    package cn.gee; public class Emp { private String id;//员工编号 一般是唯一的 private String sname; private int ...

  3. 员工管理系统(集合与IO流的结合使用 beta4.0 ObjectInputStream/ ObjectOutputStream)

    package cn.employee_io; import java.io.Serializable; public class Employee implements Serializable{ ...

  4. 员工管理系统(集合与IO流的结合使用 beta1.0 ArrayList<Employee>)

    package cn.employee; public class Employee { private int empNo; private String name; private String ...

  5. Java集合、IO流、线程知识

    一.集合: 1. 集合框架: 1)Collection (1)List:有序的,有索引,元素可重复. (add(index, element).add(index, Collection).remov ...

  6. Properties集合与IO流

    package com.itheima.demo07.Prop; import java.io.FileOutputStream; import java.io.FileReader; import ...

  7. Java基础知识强化之IO流笔记53:IO流练习之 自定义类模拟BufferedReader的readLine()功能案例

    1. 用Reader模拟BufferedReader的readLine()功能:   readLine():一次读取一行,根据换行符判断是否结束,只返回内容,不返回换行符 2. 代码实现和思路分析: ...

  8. Java里的IO流里的FileReader里的BufferedReader读取并在前打印行数!

    哈哈!!我又来了!!这个里面没有运用readLine 的方法!!纯手打!! import java.io.BufferedReader; import java.io.FileNotFoundExce ...

  9. java高级之Io流

    1.1,什么是io流? 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作 ...

随机推荐

  1. VS2012关于hash_map的使用简略

    VS关于hash_map使用的一些经常使用构造方法汇总,包含基本类型和结构体,相信够一般模仿使用: # include<hash_map> #include<iostream> ...

  2. 安卓版本和Api Level

    Platform Version API Level VERSION_CODE Notes Android 4.4 19 KITKAT Platform Highlights Android 4.3 ...

  3. 02-Swift学习笔记-元组类型

    02-Swift学习笔记-元组类型 元组类型由N个任意类型的数据组成(N>=0) 元组类型的数据称为"元素" eg var size = (x:100 , y:100) si ...

  4. NPOI解析Excel

    园子的文章: http://www.cnblogs.com/csqb-511612371/category/654604.html 关键就是如何解析Excel表头,特别是合并单元格的..还有对应NPO ...

  5. 2016/04/29 smarty模板 1, 初步 目标 : 变量 运算符 表达式 流程控制 函数

    ① 从配置文件中读取配置: 1,在模板页面加载配置文件 html页面 不是php页面 <{config_load file='fo.conf'}> 2,在需要用到配置的地方加 <{# ...

  6. C++中各大有名的科学计算库

    在 C++中,库的地位是非常高的.C++之父 Bjarne Stroustrup先生多次表示了设计库来扩充功能要好过设计更多的语法的言论.现实中,C++的库门类繁多,解决 的问题也是极其广泛,库从轻量 ...

  7. Why was 80 Chosen as the Default HTTP Port and 443 as the Default HTTPS Port?

    https://www.howtogeek.com/233383/why-was-80-chosen-as-the-default-http-port-and-443-as-the-default-h ...

  8. HDU4027 Can you answer these queries? —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/HDU-4027 A lot of battleships of evil are arranged in a line before ...

  9. YTU 2844: 改错题A-看电影

    2844: 改错题A-看电影 时间限制: 1 Sec  内存限制: 128 MB 提交: 69  解决: 47 题目描述 注:本题只需要提交标记为修改部分之间的代码,请按照C++方式提交. 小平家长为 ...

  10. django flask缓存memcache的key生成方法介绍

    去年的一个django项目中,使用了memcache作为系统缓存,并实现多台机器上的缓存共享.配置的cache如下图所示: 最近在项目调试过程中,发现memcache在进行缓存时,使用的key并不是实 ...