package com.day5;
import java.util.Scanner;
public class EmployeeUtil {
Scanner sc = new Scanner(System.in);
//定义一个员工工号用于自增
int tempId = 1006;
//定义一个存储所有员工信息的数组索引
int index = 5;
//定义一个存储所有员工信息的数组
Employee[] employees = new Employee[10];
//初始化
public void inti() {
Employee emp1 = new Employee(1001, "张三", "男", 34, 10000);
Employee emp2 = new Employee(1002, "李四", "男", 44, 10000);
Employee emp3 = new Employee(1003, "王麻子", "男", 24, 10000);
Employee emp4 = new Employee(1004, "李欣", "女", 25, 10000);
Employee emp5 = new Employee(1005, "万茜", "女", 32, 10000);
emp1.bonus = bonus(emp1);
employees[0] = emp1;
emp2.bonus = bonus(emp2);
employees[1] = emp2;
emp3.bonus = bonus(emp3);
employees[2] = emp3;
emp4.bonus = bonus(emp4);
employees[3] = emp4;
emp5.bonus = bonus(emp5);
employees[4] = emp5;
}
//年终奖
public double bonus(Employee emp){
emp.bonus = (emp.money * 12) * 0.25;
return emp.bonus;
}
//展示员工信息方法
public void showEmployee() {
System.out.println("====员工信息如下====");
for (Employee emp : employees) {
if (emp != null) {
System.out.println("员工工号:" + emp.empId);
System.out.println("员工姓名:" + emp.name);
System.out.println("员工性别:" + emp.gender);
System.out.println("员工年龄:" + emp.age);
System.out.println("员工薪资:" + emp.money);
System.out.println("员工年终奖:" + emp.bonus);
System.out.println("-------------------------");
}
}
}
//添加员工信息的方法
public void addEmployee() {
Employee emp = new Employee();
emp.empId = tempId++;
System.out.println("请输入员工的姓名:");
emp.name = sc.next();
System.out.println("请输入员工的性别:");
emp.gender = sc.next();
System.out.println("请输入员工的年龄:");
emp.age = sc.nextInt();
System.out.println("请输入员工的薪资:");
emp.money = sc.nextInt();
emp.bonus = bonus(emp);
saveEmployee(emp);
}
//存入员工信息的方法
public void saveEmployee(Employee emp) {
if (index < employees.length) {
employees[index] = emp;
index++;
System.out.println("添加员工信息成功!");
}
}
//删除员工信息的方法
public void deleteEmployee() {
System.out.println("请输入你要删除的员工的工号:");
int deleteId = sc.nextInt();
int flag = -1;
//对工号继续判断是否存在
for (int i = 0; i < employees.length; i++) {
Employee emp = employees[i];
if (emp.empId == deleteId) {
flag = i;
break;
}
}
if (flag == -1) {
System.out.println("该员工信息不存在!");
} else {
//删除
employees[flag] = null;
System.out.println("删除成功!");
}
}
public void updateEmployee() {
System.out.println("请输入你要修改的员工的工号:");
int updateId = sc.nextInt();
//对工号继续判断是否存在
int flag = -1;
for (int i = 0; i < employees.length; i++) {
Employee emp = employees[i];
if (emp.empId == updateId) {
flag = i;
break;
}
}
if (flag == -1) {
System.out.println("该员工信息不存在,请重新输入");
} else {
//修改
for (int i = flag; ; ) {
System.out.println("请输入员工的姓名:");
employees[flag].name = sc.next();
System.out.println("请输入员工的性别:");
employees[flag].gender = sc.next();
System.out.println("请输入员工的年龄:");
employees[flag].age = sc.nextInt();
System.out.println("请输入员工的薪资:");
employees[flag].money = sc.nextInt();
employees[flag].bonus = bonus(employees[flag]);
break;
}
System.out.println("修改成功!");
}
}
}