package cn.gee;

 public class Emp {
private String id;//员工编号 一般是唯一的
private String sname;
private int age;
private float salary; public Emp(){ } public Emp(String id, String sname, int age, float salary) {
super();
this.id = id;
this.sname = sname;
this.age = age;
this.salary = salary;
} @Override
public String toString() {
return id+","+sname+","+ age + "," + salary;
} public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
} }

Emp.java

 package cn.gee;

 import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List; /**
* 1.每个员工信息 一条一条 写到文件中
* 2.把多个员工(集合)一起写进去或者读出来
* @author y
*
*/
public class EmpBussiness {
List<Emp> emps=new LinkedList<Emp>(); public EmpBussiness() throws IOException{
init();
}
private void init() throws IOException{
read();
}
//获得已有的员工信息
private void read() throws IOException{
FileReader fr=new FileReader("C:\\zhougb\\08\\08\\08\\emps.txt");
BufferedReader br=new BufferedReader(fr);
String empStr=null;
while((empStr=br.readLine())!=null){
String[] empStrs=empStr.split(",");
emps.add(new Emp(empStrs[0],empStrs[1],Integer.parseInt(empStrs[2]),Float.parseFloat(empStrs[3])));
}
fr.close();
br.close();
} public void write() throws IOException{
FileWriter fw=new FileWriter("C:\\zhougb\\08\\08\\08\\emps.txt");
BufferedWriter bw=new BufferedWriter(fw);
for(Emp e:emps){
bw.write(e.toString());
bw.newLine();
}
bw.flush();
fw.close();
bw.close();
} //添加员工
public void add(Emp emp) throws IOException{
// read();
emps.add(emp);
write(); } //查询所有员工
public List<Emp> selectAll() throws IOException{
//read();
return emps;
} //查询单个员工
public Emp select(String id){ for(Emp e:emps){
System.out.println("EmpBussiness.select()"+e.getId());
if(id.equals(e.getId())){
return e;
}
}
return null;
}
//删除单个员工
public boolean delete(String id) throws IOException{ for(Emp e:emps){
if(id.equals(e.getId())){
emps.remove(e);
write();
return true;
}
} return false;
} /**修改员工:
* 1.不论属性有没有值,都覆盖旧的数据
* 2.如果属性没有值,那么旧的数据就不覆盖
* @param nemp
* @return
*/
public boolean update(Emp nemp){
boolean ret=false;
for(Emp e:emps){
if(nemp.getId().equals(e.getId())){
e.setAge(nemp.getAge());
e.setSalary(nemp.getSalary());
e.setSname(nemp.getSname());
ret= true;
}
}
return ret;
}
}

EmpBussiness.java

 package cn.gee;

 import java.io.IOException;
import java.util.List;
import java.util.Random;
import java.util.Scanner; public class View {
public static void main(String[] args) throws IOException {
EmpBussiness empBussiness=new EmpBussiness();
System.out.println("------------------------------员工管理系统beta1.0--------------------------");
System.out.println("1:添加员工 2:查询员工 3:删除员工 4:修改员工 5:所有员工 6:退出系统");
Scanner sc=new Scanner(System.in);
int choice=sc.nextInt();
while(choice<6){
switch (choice) {
case 1:
System.out.println("请输入名称:");
String sname=sc.next();
System.out.println("请输入年龄:");
int age=sc.nextInt();
System.out.println("请输入薪资:");
float salary=sc.nextFloat();
empBussiness.add(new Emp(Math.round(Math.random()*10000)+"",sname,age,salary));
System.out.println("添加成功~");
break;
case 2:
System.out.println("请输入编号:");
String id=sc.next();
System.out.println("员工编号 员工姓名 员工年龄 员工薪资");
System.out.println(doShow(empBussiness.select(id)));
break;
case 3:
System.out.println("请输入编号:");
String did=sc.next();
empBussiness.delete(did);
System.out.println("删除成功~");
break;
case 4:
//empBussiness.delete(sc.next());
break;
case 5:
System.out.println("员工编号 员工姓名 员工年龄 员工薪资");
StringBuilder sb=new StringBuilder();
List<Emp> emps=empBussiness.selectAll();
for(Emp e:emps){
sb.append(doShow(e));
} System.out.println(sb);
break;
case 6:
return ; }
choice=sc.nextInt();
} } private static String doShow(Emp e){
StringBuilder sb=new StringBuilder();
sb.append(e.getId()+" ");
sb.append(e.getSname()+" ");
sb.append(e.getAge()+" ");
sb.append(e.getSalary()+" ");
sb.append("\n");
return sb.toString();
} }

View.java

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

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

    Employee.java package cn.employee_io; public class Employee { private String empId; private String n ...

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

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

  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. IO流学习笔记(二)之BufferedWriter与BufferedReader及实例Demo

    在之前的学习笔记(http://blog.csdn.net/megustas_jjc/article/details/72853059)中,FileWriter与FileReader的Demo使用的中 ...

随机推荐

  1. [Javascript] Understanding the .constructor property on JavaScript Objects

    Constructor functions hold an interesting purpose in JavaScript. Unlike in classical languages, they ...

  2. mysql手记

    myisam innoDB是mysql经常使用的存储引擎 MyISAM不支持事务.也不支持外键.但其訪问速度快.对事务完整性没有要求. InnoDB存储引擎提供了具有提交.回滚和崩溃恢复能力的事务安全 ...

  3. SqlServer 经常使用分页方法总结

    SqlServer 经常使用分页方法总结 以下演示样例总结了,SqlServer数据库 经常使用分页方法,仅供学习參考 A. 使用 RowNumber 和 Between And 组合分页: /*** ...

  4. AnkhSVN介绍

    AnkhSVN介绍 Posted on 2012-11-15 23:24 ArRan 阅读(3120) 评论(1) 编辑 收藏 AnkhSVN是一款在VS中管理Subversion的插件,您可以在VS ...

  5. 【JS】JavaScript引擎的内部执行机制

     近期在复习JavaScript,看到setTimeout函数时.想起曾经刚学时,在一本书上看过setTimeout()里的回调函数执行的间隔时间有昌不是后面设置的值.曾经没想太多.网上看了JS大 ...

  6. Cleave js 使用

    1111111111111111 xxxxxx Cleave.js 键入时格式化< input />内容   信用卡号码格式 明确   美国运通:从34/37开始 34   签证:从4开始 ...

  7. JavaScript的高大强

    1,JavaScript的引入方式 1.1>Script标签内写代码 <Script> //这里写JS代码的地方 </Script> 1.2>引入额外的JS文件 & ...

  8. JDBC连接数据库查询信息的步骤(提取成配置文件方式)

    硬编码格式的弊端:数据库发生改变时,要重新修改代码,重新编译和部署 解决方法:将数据库信息写在配置文件当中,让程序通过读取配置文件来获得这些信息 jdbc.driver.class=com.mysql ...

  9. HEX文件格式学习笔记

    这也是一篇学习摘抄:原文地址:http://blog.csdn.net/syrchina/article/details/7004998        为了编写一个可以按照自己的要求进行ISP的程序, ...

  10. UVA - 11488 Hyper Prefix Sets(trie树)

    1.给n个只含0.1的串,求出这些串中前缀的最大和. 例1: 0000 0001 10101 010 结果:6(第1.2串共有000,3+3=6) 例2: 01010010101010101010 1 ...