员工管理系统(文件读写更新版)

package FileEMP;


public class Employee {


protected String ID;       //员工编号
protected String name; //员工姓名
protected String position; //员工职务
protected int holiday; //请假天数
protected double salary; //基本工资

public Employee(String iD, String name, String position, int holiday,
double salary) {
super();
ID = iD;
this.name = name;
this.position = position;
this.holiday = holiday;
this.salary = salary;
}


// "编号:"+ID+" 姓名:"+name+" 职位:"
// +position+" 请假天数:"+holiday+" 工资:"+salary;
@Override
public String toString() {
return "编号:"+ID+" 姓名:"+name+" 职位:"
+position+" 请假天数:"+holiday+" 工资:"+salary;
}


public Employee() {
super();
// TODO Auto-generated constructor stub
}

/**
* 工资计算
*/
public double sumSalary(double salary,int holiday) {
return salary;
}



public String getID() {
return ID;
}


public void setID(String iD) {
ID = iD;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public String getPosition() {
return position;
}


public void setPosition(String position) {
this.position = position;
}


public int getHoliday() {
return holiday;
}


public void setHoliday(int holiday) {
this.holiday = holiday;
}


public double getSalary() {
return salary;
}


public void setSalary(double salary) {
this.salary = salary;
}



}


*************************************************************************************************************


package FileEMP;
import java.text.DecimalFormat;
   /**
* 普通员工
* @author 
*
*/
public class CommonEmployee extends Employee {


public CommonEmployee() {
super();
// TODO Auto-generated constructor stub
}


public CommonEmployee(String iD, String name, String position, int holiday,
double salary) {
super(iD, name, position, holiday, salary);
// TODO Auto-generated constructor stub
}



/**
* 工资计算
*/
@Override
public double sumSalary(double salary,int holiday) {

double s=salary+salary*0.1+salary*0.5+200-(salary/30)*holiday;
 
DecimalFormat df=new DecimalFormat("#.00");
 
String salary1=df.format(s);
double salary2 = Double.valueOf(salary1);
 
return salary2;
}

}


package FileEMP;
import java.text.DecimalFormat;




/**
* 董事
* @author 
*
*/
public class Director extends Employee {


public Director() {
super();
// TODO Auto-generated constructor stub
}


public Director(String iD, String name, String position, int holiday,
double salary) {
super(iD, name, position, holiday, salary);
// TODO Auto-generated constructor stub
}



/**
* 工资计算
*/
@Override
public double sumSalary(double salary,int holiday) {
double s=salary+salary*0.08+salary*0.3+2000+3000-salary/(31-holiday)*holiday;
DecimalFormat df=new DecimalFormat("#.00");
 
String salary1=df.format(s);
double salary2 = Double.valueOf(salary1);
 
return salary2;
}
}


*****************************************************************************************************************************

package FileEMP;
import java.text.DecimalFormat;




/**
* 经理
* @author 
*
*/
public class Manager extends Employee {


public Manager() {
super();
// TODO Auto-generated constructor stub
}


public Manager(String iD, String name, String position, int holiday,
double salary) {
super(iD, name, position, holiday, salary);
// TODO Auto-generated constructor stub
}



/**
* 工资计算
*/
@Override
public double sumSalary(double salary,int holiday) {
double s=salary+salary*0.2+salary*0.5+500-salary/(31-holiday)*holiday;
DecimalFormat df=new DecimalFormat("#.00");
 
String salary1=df.format(s);
double salary2 = Double.valueOf(salary1);
 
return salary2;

}



}


********************************************************************************************************************

package FileEMP;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Scanner;




public class FileManagerEMP {


static Employee ems;
static ArrayList<Employee> empList = new ArrayList<Employee>();
static Scanner sc = new Scanner(System.in);
static String ID;
static String name;
static String position;
static int holiday;
static double salary;
static double salary1;
static int n;
private static BufferedReader reader;


@SuppressWarnings("static-access")
public static void main(String[] args) throws IOException {


FileManagerEMP fManagerEMP = new FileManagerEMP();
int flag=0;
do {
n = Prompt();
switch (n) {
case 1:
fManagerEMP.Add();
flag=1;
break;
case 2:
fManagerEMP.Del();
flag=1;
break;
case 3:
fManagerEMP.Update();
flag=1;
break;
case 4:
fManagerEMP.find();
flag=1;
break;
case 5:
fManagerEMP.findAll();
flag=1;
break;
case 0:
System.out.println("退出系统!!!");
fManagerEMP.ReadFile();
fManagerEMP.save();
flag=0;
break;
default:
break;
}

} while (flag==1);



}


public FileManagerEMP() throws IOException  {
super();
//ReadFile ();
// TODO Auto-generated constructor stub

}

/**
* 从文件读出
* @throws IOException 
*/
public static void ReadFile () throws IOException {
File file = new File("d:/EMP.txt");

if (file.exists()) {
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
String[] s1 = new String[5];
int line = 1;


while ((tempString = reader.readLine()) != null) {
s1 = tempString.split("\\|");

ems = new Employee(s1[0], s1[1], s1[2], Integer
.parseInt(s1[3]), Double.parseDouble(s1[4]));

empList.add(ems);
line++;
}
reader.close();
} catch (IOException e) {
// TODO: handle exception
System.out.println("I/O Exception!!!");
}


} else {
file.createNewFile();
}
}


/**

* 写入文件
* @throws IOException 
* @throws UnsupportedEncodingException 
* @throws FileNotFoundException 
*/
public static void save () throws IOException {
File f1 = new File("d:/EMP.txt");
FileWriter fw = new FileWriter(f1);
for (int i = 0; i < empList.size(); i++) {
fw.append((new String(empList.get(i).getID().toString().getBytes("utf-8"),"utf-8")+ "|"
 +new String(empList.get(i).getName().toString().getBytes("utf-8"),"utf-8")+ "|"
 +new String(empList.get(i).getPosition().toString().getBytes("utf-8"),"utf-8")+ "|"
 +empList.get(i).getHoliday()+ "|"
 +empList.get(i).getSalary()+ "|")+"\r\n");

}
fw.close();
}

// 提示
public static int Prompt() {
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("|---0 退出---|");
System.out.println("|-----------|");
System.out.println("请选择业务:");
n = sc.nextInt();
return n;


}


// 插入
public static void Add() throws IOException {
ReadFile ();
System.out.println();
System.out.println("请输入员工编号:");
ID = sc.next();
System.out.println("请输入员工姓名:");
name = sc.next();
System.out.println("请输入员工职位(普通员工,经理,董事长):");
position = sc.next();
System.out.println("请输入员工请假天数:");
holiday = sc.nextInt();
System.out.println("请输入员工基本工资:");
salary = sc.nextDouble();


if (position.equals("普通员工")) {
Employee common = new CommonEmployee();
salary1 = common.sumSalary(salary, holiday);
} else if (position.equals("经理")) {
Employee common = new Manager();
salary1 = common.sumSalary(salary, holiday);
} else if (position.equals("董事长")) {
Employee common = new Director();
salary1 = common.sumSalary(salary, holiday);
} else {
System.out.println("没有该职位,请重新输入");
position = sc.next();
}
ems = new Employee(ID, name, position, holiday, salary1);
empList.add(ems);
System.out.println("插入成功!!!");
System.out.println(empList.get(empList.size()-1).toString());

save();
empList.clear();

}


/**
* 删除

* @return
* @throws IOException 
*/
private static void Del() throws IOException {
// TODO Auto-generated method stub
ReadFile();
System.out.println("请输入员工姓名:");
name = sc.next();
int index = 0;
int i = 0;
for (i = 0; i < empList.size(); i++) {
if (name.equals(empList.get(i).getName())) {
index = i;
System.out.println(empList.get(i).toString());
break;
} else {
continue;
}
}
if (i == empList.size()) {
System.out.println("没有要删除的员工");
} else {
System.out.println("删除成功!!!");
empList.remove(index);
}
save();
empList.clear();
}


/**
* 按姓名查询
* @throws IOException 
*/
public static boolean find() throws IOException {
ReadFile();
System.out.println("请输入员工姓名:");
name = sc.next();
boolean flag = false;
int i;
for (i = 0; i < empList.size(); i++) {
if (name.equals(empList.get(i).getName())) {
System.out.println(empList.get(i).toString());
flag = true;
break;
} else {
continue;
}
}
if (i == empList.size()) {
System.out.println("没有该员工");
flag = false;
}
empList.clear();
return flag;


}


/**
* 修改
* @throws IOException 
*/
private static void Update() throws IOException {
// TODO Auto-generated method stub
ReadFile();
System.out.println("请输入员工姓名:");
name = sc.next();
int i;
for (i = 0; i < empList.size(); i++) {
if (name.equals(empList.get(i).getName())) {
System.out.println(empList.get(i).toString());


System.out.println("请输入员工编号:");
ID = sc.next();
System.out.println("请输入员工姓名:");
name = sc.next();
System.out.println("请输入员工职位(普通员工,经理,董事长):");
position = sc.next();
System.out.println("请输入员工请假天数:");
holiday = sc.nextInt();
System.out.println("请输入员工基本工资:");
salary = sc.nextDouble();


if (position.equals("普通员工")) {
Employee common = new CommonEmployee();
salary1 = common.sumSalary(salary, holiday);
} else if (position.equals("经理")) {
Employee common = new Manager();
salary1 = common.sumSalary(salary, holiday);
} else if (position.equals("董事长")) {
Employee common = new Director();
salary1 = common.sumSalary(salary, holiday);
} else {
System.out.println("没有该职位,请重新输入");
position = sc.next();
}
ems = new Employee(ID, name, position, holiday, salary1);
empList.set(i, ems);
System.out.println(empList.get(i).toString());
break;
} else {


continue;
}
}
if (i == empList.size()) {
System.out.println("没有该员工");
}
save();
empList.clear();
}


/**
* 查询所有

* @return
* @throws IOException 
*/
public static void findAll() throws IOException {
//System.out.println("查询全部index1="+index);
ReadFile();
if(empList.size()==0){
System.out.println("共0条记录!!!");
}else {
for (int i = 0; i < empList.size(); i++) {
System.out.println(empList.get(i).toString());
}
}
empList.clear();
}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值