1 package cn.gee; 2 3 public class Emp { 4 private String id;//员工编号 一般是唯一的 5 private String sname; 6 private int age; 7 private float salary; 8 9 public Emp(){ 10 11 } 12 13 14 public Emp(String id, String sname, int age, float salary) { 15 super(); 16 this.id = id; 17 this.sname = sname; 18 this.age = age; 19 this.salary = salary; 20 } 21 22 23 24 25 @Override 26 public String toString() { 27 return id+","+sname+","+ age + "," + salary; 28 } 29 30 31 public String getId() { 32 return id; 33 } 34 public void setId(String id) { 35 this.id = id; 36 } 37 public String getSname() { 38 return sname; 39 } 40 public void setSname(String sname) { 41 this.sname = sname; 42 } 43 public int getAge() { 44 return age; 45 } 46 public void setAge(int age) { 47 this.age = age; 48 } 49 public float getSalary() { 50 return salary; 51 } 52 public void setSalary(float salary) { 53 this.salary = salary; 54 } 55 56 57 58 59 }
1 package cn.gee; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.FileNotFoundException; 6 import java.io.FileReader; 7 import java.io.FileWriter; 8 import java.io.IOException; 9 import java.util.LinkedList; 10 import java.util.List; 11 12 /** 13 * 1.每个员工信息 一条一条 写到文件中 14 * 2.把多个员工(集合)一起写进去或者读出来 15 * @author y 16 * 17 */ 18 public class EmpBussiness { 19 List<Emp> emps=new LinkedList<Emp>(); 20 21 22 public EmpBussiness() throws IOException{ 23 init(); 24 } 25 private void init() throws IOException{ 26 read(); 27 } 28 //获得已有的员工信息 29 private void read() throws IOException{ 30 FileReader fr=new FileReader("C:\zhougb\08\08\08\emps.txt"); 31 BufferedReader br=new BufferedReader(fr); 32 String empStr=null; 33 while((empStr=br.readLine())!=null){ 34 String[] empStrs=empStr.split(","); 35 emps.add(new Emp(empStrs[0],empStrs[1],Integer.parseInt(empStrs[2]),Float.parseFloat(empStrs[3]))); 36 } 37 fr.close(); 38 br.close(); 39 } 40 41 public void write() throws IOException{ 42 FileWriter fw=new FileWriter("C:\zhougb\08\08\08\emps.txt"); 43 BufferedWriter bw=new BufferedWriter(fw); 44 for(Emp e:emps){ 45 bw.write(e.toString()); 46 bw.newLine(); 47 } 48 bw.flush(); 49 fw.close(); 50 bw.close(); 51 } 52 53 54 //添加员工 55 public void add(Emp emp) throws IOException{ 56 // read(); 57 emps.add(emp); 58 write(); 59 60 } 61 62 63 //查询所有员工 64 public List<Emp> selectAll() throws IOException{ 65 //read(); 66 return emps; 67 } 68 69 //查询单个员工 70 public Emp select(String id){ 71 72 for(Emp e:emps){ 73 System.out.println("EmpBussiness.select()"+e.getId()); 74 if(id.equals(e.getId())){ 75 return e; 76 } 77 } 78 return null; 79 } 80 //删除单个员工 81 public boolean delete(String id) throws IOException{ 82 83 for(Emp e:emps){ 84 if(id.equals(e.getId())){ 85 emps.remove(e); 86 write(); 87 return true; 88 } 89 } 90 91 return false; 92 } 93 94 /**修改员工: 95 * 1.不论属性有没有值,都覆盖旧的数据 96 * 2.如果属性没有值,那么旧的数据就不覆盖 97 * @param nemp 98 * @return 99 */ 100 public boolean update(Emp nemp){ 101 boolean ret=false; 102 for(Emp e:emps){ 103 if(nemp.getId().equals(e.getId())){ 104 e.setAge(nemp.getAge()); 105 e.setSalary(nemp.getSalary()); 106 e.setSname(nemp.getSname()); 107 ret= true; 108 } 109 } 110 return ret; 111 } 112 }
1 package cn.gee; 2 3 import java.io.IOException; 4 import java.util.List; 5 import java.util.Random; 6 import java.util.Scanner; 7 8 public class View { 9 public static void main(String[] args) throws IOException { 10 EmpBussiness empBussiness=new EmpBussiness(); 11 System.out.println("------------------------------员工管理系统beta1.0--------------------------"); 12 System.out.println("1:添加员工 2:查询员工 3:删除员工 4:修改员工 5:所有员工 6:退出系统"); 13 Scanner sc=new Scanner(System.in); 14 int choice=sc.nextInt(); 15 while(choice<6){ 16 switch (choice) { 17 case 1: 18 System.out.println("请输入名称:"); 19 String sname=sc.next(); 20 System.out.println("请输入年龄:"); 21 int age=sc.nextInt(); 22 System.out.println("请输入薪资:"); 23 float salary=sc.nextFloat(); 24 empBussiness.add(new Emp(Math.round(Math.random()*10000)+"",sname,age,salary)); 25 System.out.println("添加成功~"); 26 break; 27 case 2: 28 System.out.println("请输入编号:"); 29 String id=sc.next(); 30 System.out.println("员工编号 员工姓名 员工年龄 员工薪资"); 31 System.out.println(doShow(empBussiness.select(id))); 32 break; 33 case 3: 34 System.out.println("请输入编号:"); 35 String did=sc.next(); 36 empBussiness.delete(did); 37 System.out.println("删除成功~"); 38 break; 39 case 4: 40 //empBussiness.delete(sc.next()); 41 break; 42 case 5: 43 System.out.println("员工编号 员工姓名 员工年龄 员工薪资"); 44 StringBuilder sb=new StringBuilder(); 45 List<Emp> emps=empBussiness.selectAll(); 46 for(Emp e:emps){ 47 sb.append(doShow(e)); 48 } 49 50 System.out.println(sb); 51 break; 52 case 6: 53 return ; 54 55 } 56 choice=sc.nextInt(); 57 } 58 59 } 60 61 private static String doShow(Emp e){ 62 StringBuilder sb=new StringBuilder(); 63 sb.append(e.getId()+" "); 64 sb.append(e.getSname()+" "); 65 sb.append(e.getAge()+" "); 66 sb.append(e.getSalary()+" "); 67 sb.append(" "); 68 return sb.toString(); 69 } 70 71 72 }