员工管理

看到java 我就试着够了,天天累成狗,我就先来个员工系统

import java.awt.List;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;


public class TestEMD {
public static ArrayList<Employee> ems;
public static void addEmployee() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入员工编号:");
String id = sc.nextLine();
System.out.println("请输入员工姓名:");
String name = sc.nextLine();
System.out.println("请输入员工职务:(employee,manager,chairman)");
String position = sc.nextLine();
System.out.println("请输入员工请假天数:");
int holiday = sc.nextInt();
System.out.println("请输入员工基本工资:");
double salary = sc.nextDouble();

if (position.equals("employee")) {
Employee newOne = new CommonEmployee(id, name, position, holiday,
salary);
ems.add(newOne);
System.out.print("增加数据成功");
newOne.display();
} else if (position.equals("manager")) {
Employee newOne = new Manager(id, name, position, holiday, salary);
ems.add(newOne);
System.out.print("增加数据成功");
newOne.display();
} else if (position.equals("chairman")) {
Employee newOne = new Director(id, name, position, holiday, salary);
ems.add(newOne);
System.out.print("增加数据成功");
newOne.display();
} else
System.out.println("您所输入的职务不存在");

}

public static void delEmployee() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的员工姓名");

String delName = sc.nextLine();
boolean bl = false;

for (Employee employee : ems) {
if(employee.getName().equals(delName)) {
employee.display();
ems.remove(employee);
System.out.println("删除数据成功");
bl = true;
break;
}
}
if (!bl) {
System.out.println("您要删除的人不存在");
}
}

public static void updateEmployee() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要修改的员工姓名");
String updateName = sc.nextLine();

boolean bl = false;

for (Employee employee : ems) {
if(employee.getName().equals(updateName)) {
System.out.println("请重新输入员工信息");
System.out.println("请输入员工编号:");
String id = sc.nextLine();
employee.setID(id);
System.out.println("请输入员工姓名:");
String name = sc.nextLine();
employee.setName(name);
System.out.println("请输入员工职务:(employee,manager,chairman)");
String position = sc.nextLine();
employee.setPosition(position);
System.out.println("请输入员工请假天数:");
int holiday = sc.nextInt();
employee.setHoliday(holiday);
System.out.println("请输入员工基本工资:");
double salary = sc.nextDouble();
employee.setSalary(salary);
bl = true;
}
}
if (!bl) {
System.out.println("您要修改的人不存在");
}
}

public static void queryEmployee() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您要查找的员工姓名:");
String name = sc.nextLine();
boolean bl = false;
for (Employee employee : ems) {
if(employee.getName().equals(name)) {
employee.display();
}
}
if(!bl) {
System.out.println("您要查找的员工不存在");
}
}
public static ArrayList<Employee> CreateEmInfo(String filename) {
ems = new ArrayList<Employee>();
String str = null;
try {
FileInputStream fin = new FileInputStream(filename);
InputStreamReader ir = new InputStreamReader(fin, "UTF-8");
BufferedReader fbr = new BufferedReader(ir);

while((str = fbr.readLine()) != null) {
String[] emsInfo = str.split("\\|");
System.out.println(emsInfo[0]);
System.out.println(emsInfo[1]);
System.out.println(emsInfo[2]);
System.out.println(emsInfo[3]);
System.out.println(emsInfo[4]);
if (emsInfo[2].equals("employee")) {
Employee newOne = new Employee(emsInfo[0], emsInfo[1], emsInfo[2], Integer.parseInt(emsInfo[3]), Double.parseDouble(emsInfo[4]));
ems.add(newOne);
//System.out.print("增加数据成功");
//newOne.display();
} else if (emsInfo[2].equals("manager")) {
Employee newOne = new Manager(emsInfo[0], emsInfo[1], emsInfo[2], Integer.parseInt(emsInfo[3]), Double.parseDouble(emsInfo[4]));
ems.add(newOne);
//System.out.print("增加数据成功");
//newOne.display();
} else if (emsInfo[2].equals("chairman")) {
Employee newOne = new Director(emsInfo[0], emsInfo[1], emsInfo[2], Integer.parseInt(emsInfo[3]), Double.parseDouble(emsInfo[4]));
ems.add(newOne);
//System.out.print("增加数据成功");
//newOne.display();
}
}
fbr.close();
ir.close();
fin.close();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return ems;
}
public static void savaToFile(ArrayList<Employee> ems, String filename) {
try {
String str = null;
File newfile = new File(filename);
FileOutputStream fout = new FileOutputStream(newfile);
OutputStreamWriter or = new OutputStreamWriter(fout, "UTF-8");
BufferedWriter bw = new BufferedWriter(or);

for (Employee employee : ems) {
str = employee.getID() + "|" + employee.getName() + "|" + employee.getPosition() + "|" +employee.getHoliday()+ "|" + employee.getSalary() + "\n";
bw.write(str);
}
bw.close();
or.close();
fout.close();
System.out.println("保存成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
CreateEmInfo("ems.txt");

while (true) {
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("|------------|");
System.out.println("请选择业务:");
int choose = sc.nextInt();
switch (choose) {
case 1:
addEmployee();
break;
case 2:
delEmployee();
break;
case 3:
updateEmployee();
break;
case 4:
queryEmployee();
break;
case 5:
savaToFile(ems, "ems.txt");
break;
case 6:
System.exit(0);
default:
System.out.println("您所需的业务不存在,请重新输入");
}
}
}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值