java 员工管理系统 向文本文件中备份

来源:互联网 发布:js获取浏览器的宽度 编辑:程序博客网 时间:2024/06/01 15:33
package emp;

   //顶级父类
  import java.util.*;


  public class emper {
      
 String id;
 String  name;
 String  posi;
 int    holiday;
 double  salary;
 
public emper(String id,String name,String posi,int holiday,double salary){
    
this.id = id;
this.name = name;
this.posi = posi;
this.holiday = holiday;
this.salary = salary;
 
}
 
public String getName(){
return this.name;
}
 
public double sumSalary(){
 
  return 0.00;
 
}
 
 public String toString(){
 
 return "编号:"+id+" 名字:"+name+ " 职位:"+posi
+" 请假天数:"+holiday+"  工资:"+ sumSalary();
 
 }



}





   //普通员工

package emp;
import java.util.*;


public class commonEmployee  extends emper{





   public commonEmployee(String id,String name,String posi,int holiday,double salary){
 
 
super(id, name, posi, holiday, salary);
}

 
   public double sumSalary(){
     
 return (this.salary*1.6+200)-(salary/30*holiday);
   }


}




  //经理

package emp;
import java.util.*;


public class Manager  extends emper{





   public Manager(String id,String name,String posi,int holiday,double salary){
 
 
super(id, name, posi, holiday, salary);
}

 
   public double sumSalary(){
     
 return (this.salary*1.7+500)-(salary/30*holiday);
   }


}






    //董事

package emp;
import java.util.*;


public class Director extends emper{





   public Director(String id,String name,String posi,int holiday,double salary){
 
 
super(id, name, posi, holiday, salary);
}

 
   public double sumSalary(){
     
 return (this.salary*1.38+5000)-(salary/30*holiday);
   }


}






//主要的逻辑程序

package emp;


import java.io.*;
import java.util.*;


import com.sun.javafx.geom.AreaOp.AddOp;


public class testEmp {


Scanner sc = new Scanner(System.in);


// 定义查找到的索引
int findex;
String  filename = "D:/emp.txt";   //备份文件的储存位置


ArrayList<emper> empList = new ArrayList<emper>();


public void choise() throws IOException {


System.out.println("|------------------|\n|-----1 增加 ------|"
+ "\n|-----2 删除 ------|\n"
+ "|-----3 修改 ------|\n|-----4 查询 ------|"
+ "\n|-----5 备份 ------|\n|-----0 退出 ------|\n|------------------|");


System.out.println("请选择业务");


int mchoise = sc.nextInt();


switch (mchoise) {
case 1:
Add();
System.out.println("添加成功");
break;


case 2:
del();
break;


case 3:
upd();
break;


case 4:
System.out.println("请输入要查找的姓名");
String fname = sc.next();


Find(fname);
break;

case 5:
Writer();
System.out.println("备份完成");
            break;
case 0:


System.out.print("退出成功");
Writer();
break;


default:
getAll();
Writer();
break;
}
}



 
//初始化,将文件内容导入员工 数组

 public void init() throws IOException{
 
   //判断d盘下的文件是否存在 ,如果不存在,生成文件
 File f1 = new File(filename) ;
 if(!f1.exists()){
 f1.createNewFile();
 System.out.println("文件已生成");
 }
 
 BufferedReader br = new BufferedReader(new FileReader(f1));
  
 String rline;
 int n = 0;
 br.readLine();  //跳过第一行 ;
 
while((rline =  br.readLine()) !=null   ){
  
  String[] read = rline.split(",");
  
  empList.add(new emper(read[0],read[1],read[2],Integer.parseInt(read[3]),Double.parseDouble(read[4])) );
  
}
   
   br.close();  //关闭
   System.out.println("初始化成功");
   
 }






  //备份
 public void Writer() throws IOException {

 BufferedWriter bw = new BufferedWriter( new FileWriter("D:/emp.txt") );
 
  bw.write("编号 , 姓名 , 职位 ,请假天数 , 工资");
  bw.newLine();   //换行;
  for(int w=0;w<empList.size();w++){
  String wid = empList.get(w).id;
  String wname = empList.get(w).name;
  String wposi = empList.get(w).posi;
  int wholi = empList.get(w).holiday;
  double wsalaery = empList.get(w).sumSalary();
 
  bw.write(wid+","+wname+","+wposi+","+wholi+","+wsalaery);
  
  bw.newLine();   //换行;
  }
  bw.close();
 
}


//修改
public void upd() {


System.out.println("请输入你要修改的名字");
String uname = sc.next();
if (Find(uname)) {


System.out.println("请输入员工编号");
String id = sc.next();
System.out.println("请输入员工职务(1 :普通员工, 2:经理, 3:董事)");


String posi = sc.next();
System.out.println("请输入员工请假天数");
int hday = sc.nextInt();
System.out.println("请输入员工基本工资");
double sal = sc.nextDouble();
          
if (posi.equals("1")) {
empList.set(findex,new commonEmployee(id, uname, "普通员工", hday, sal));


} else if (posi.equals("2")) {
empList.set(findex,new Manager(id, uname, "经理", hday, sal));

} else if (posi.equals("3")) {
empList.set(findex,new Director(id, uname, "董事", hday, sal));

}

System.out.println("修改成功");
System.out.println(empList.get(findex));

} else {
System.out.println("无此纪录");
}
}
   


//删除
public void del() {
System.out.println("请输入要删除的名字");
String dname = sc.next();


if (Find(dname)) {
empList.remove(findex);
System.out.println("纪录删除成功");


} else {
System.out.println("无此纪录");
}
}




// 增加数据
public void Add() {
System.out.println("请输入员工编号");
String id = sc.next();


System.out.println("请输入员工姓名");
String name = sc.next();


System.out.println("请输入员工职务(1 :普通员工, 2:经理, 3:董事)");


String posi = sc.next();
System.out.println("请输入员工请假天数");
int hday = sc.nextInt();
System.out.println("请输入员工基本工资");
double sal = sc.nextDouble();


if (posi.equals("1")) {
empList.add(new commonEmployee(id, name, "普通员工", hday, sal));


} else if (posi.equals("2")) {
empList.add(new Manager(id, name, "普通员工", hday, sal));
} else if (posi.equals("3")) {
empList.add(new Director(id, name, "普通员工", hday, sal));
}


System.out.println("增加数据成功");
System.out.println(empList.get(empList.size() - 1));


}


// 查全部
public void getAll() {
for (int w = 0; w < empList.size(); w++) {
Object emp = empList.get(w);
System.out.println(emp);
}
}


// 按姓名查找
public boolean Find(String name) {


for (int w = 0; w < empList.size(); w++) {
if (name.equals(empList.get(w).getName())) {
findex = w;
System.out.println(empList.get(w));
return true;
}
}
// System.out.println(empList.get(w));


return false;


}


}







//主程序

package emp;


import java.io.*;
import java.util.*;


import com.sun.javafx.geom.AreaOp.AddOp;


public class testEmp {


Scanner sc = new Scanner(System.in);


// 定义查找到的索引
int findex;
String  filename = "D:/emp.txt";   //备份文件的储存位置


ArrayList<emper> empList = new ArrayList<emper>();


public void choise() throws IOException {


System.out.println("|------------------|\n|-----1 增加 ------|"
+ "\n|-----2 删除 ------|\n"
+ "|-----3 修改 ------|\n|-----4 查询 ------|"
+ "\n|-----5 备份 ------|\n|-----0 退出 ------|\n|------------------|");


System.out.println("请选择业务");


int mchoise = sc.nextInt();


switch (mchoise) {
case 1:
Add();
System.out.println("添加成功");
break;


case 2:
del();
break;


case 3:
upd();
break;


case 4:
System.out.println("请输入要查找的姓名");
String fname = sc.next();


Find(fname);
break;

case 5:
Writer();
System.out.println("备份完成");
            break;
case 0:


System.out.print("退出成功");
Writer();
break;


default:
getAll();
Writer();
break;
}
}



  //初始化,将文件内容导入员工 数组

 public void init() throws IOException{
 
   //判断d盘下的文件是否存在 ,如果不存在,生成文件
 File f1 = new File(filename) ;
 if(!f1.exists()){
 f1.createNewFile();
 System.out.println("文件已生成");
 }
 
 BufferedReader br = new BufferedReader(new FileReader(f1));
  
 String rline;
 int n = 0;
 br.readLine();  //跳过第一行 ;
 
while((rline =  br.readLine()) !=null   ){
  
  String[] read = rline.split(",");
  
  empList.add(new emper(read[0],read[1],read[2],Integer.parseInt(read[3]),Double.parseDouble(read[4])) );
  
}
   
   br.close();  //关闭
   System.out.println("初始化成功");
   
 }






  //备份
 public void Writer() throws IOException {

 BufferedWriter bw = new BufferedWriter( new FileWriter("D:/emp.txt") );
 
  bw.write("编号 , 姓名 , 职位 ,请假天数 , 工资");
  bw.newLine();   //换行;
  for(int w=0;w<empList.size();w++){
  String wid = empList.get(w).id;
  String wname = empList.get(w).name;
  String wposi = empList.get(w).posi;
  int wholi = empList.get(w).holiday;
  double wsalaery = empList.get(w).sumSalary();
 
  bw.write(wid+","+wname+","+wposi+","+wholi+","+wsalaery);
  
  bw.newLine();   //换行;
  }
  bw.close();
 
}


//修改
public void upd() {


System.out.println("请输入你要修改的名字");
String uname = sc.next();
if (Find(uname)) {


System.out.println("请输入员工编号");
String id = sc.next();
System.out.println("请输入员工职务(1 :普通员工, 2:经理, 3:董事)");


String posi = sc.next();
System.out.println("请输入员工请假天数");
int hday = sc.nextInt();
System.out.println("请输入员工基本工资");
double sal = sc.nextDouble();
          
if (posi.equals("1")) {
empList.set(findex,new commonEmployee(id, uname, "普通员工", hday, sal));


} else if (posi.equals("2")) {
empList.set(findex,new Manager(id, uname, "经理", hday, sal));

} else if (posi.equals("3")) {
empList.set(findex,new Director(id, uname, "董事", hday, sal));

}

System.out.println("修改成功");
System.out.println(empList.get(findex));

} else {
System.out.println("无此纪录");
}
}
   


//删除
public void del() {
System.out.println("请输入要删除的名字");
String dname = sc.next();


if (Find(dname)) {
empList.remove(findex);
System.out.println("纪录删除成功");


} else {
System.out.println("无此纪录");
}
}




// 增加数据
public void Add() {
System.out.println("请输入员工编号");
String id = sc.next();


System.out.println("请输入员工姓名");
String name = sc.next();


System.out.println("请输入员工职务(1 :普通员工, 2:经理, 3:董事)");


String posi = sc.next();
System.out.println("请输入员工请假天数");
int hday = sc.nextInt();
System.out.println("请输入员工基本工资");
double sal = sc.nextDouble();


if (posi.equals("1")) {
empList.add(new commonEmployee(id, name, "普通员工", hday, sal));


} else if (posi.equals("2")) {
empList.add(new Manager(id, name, "普通员工", hday, sal));
} else if (posi.equals("3")) {
empList.add(new Director(id, name, "普通员工", hday, sal));
}


System.out.println("增加数据成功");
System.out.println(empList.get(empList.size() - 1));


}


// 查全部
public void getAll() {
for (int w = 0; w < empList.size(); w++) {
Object emp = empList.get(w);
System.out.println(emp);
}
}


// 按姓名查找
public boolean Find(String name) {


for (int w = 0; w < empList.size(); w++) {
if (name.equals(empList.get(w).getName())) {
findex = w;
System.out.println(empList.get(w));
return true;
}
}
// System.out.println(empList.get(w));


return false;


}


}






0 0