1.创建一个需要的自定义类
public class Employee {
private int ID;
private String name;
private String gender;
private int age;
private String homeTown;
private double salary;
public Employee(int ID, String name, String gender, int age, String homeTown, double salary) {
this.ID = ID;
this.name = name;
this.gender = gender;
this.age = age;
this.homeTown = homeTown;
this.salary = salary;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getHomeTown() {
return homeTown;
}
public void setHomeTown(String homeTown) {
this.homeTown = homeTown;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Employee(){}
@Override
public String toString() {
return ID +
"\t"+name + '\'' +
"\t"+gender + '\'' +
"\t\t"+age +
"\t\t"+homeTown +
"\t\t\t"+salary;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return ID == employee.ID &&
age == employee.age &&
Double.compare(employee.salary, salary) == 0 &&
Objects.equals(name, employee.name) &&
Objects.equals(gender, employee.gender) &&
Objects.equals(homeTown, employee.homeTown);
}
@Override
public int hashCode() {
return Objects.hash(ID, name, gender, age, homeTown, salary);
}
}
2.在创建一个类,然后把逻辑代码包装在方法中。
public class EmployeeManagement {
public void setMenu(){
Employee employee=new Employee();
List list=new ArrayList();
list.add(new Employee(10010,"李刚","男",23,"山东省菏泽市",6700));
list.add(new Employee(10011,"王强","男",25,"山东省菏泽市",6000));
list.add(new Employee(10012,"刘明","男",27,"河南省周口市",7700));
list.add(new Employee(10013,"刘华强","男",30,"河南省漯河市",13000));
System.out.println("--------------------欢迎登陆员工信息管理系统-------------------------------");
System.out.println();
System.out.println("编号\t姓名\t性别\t年龄\t籍贯\t\t\t\t薪资");
for (Object d:list) {
System.out.println(d);
}
System.out.println("------------------------------------------------------------------------");
System.out.println("1.添加员工\t2.删除员工\t3.修改员工\t4.查询员工\t5.退出系统(请选择1-5):");
Scanner sc=new Scanner(System.in);
int number=sc.nextInt();
switch (number){
case 1:
while (true) {
System.out.println("请输入你要添加的员工信息:");
System.out.println("ID:");
int id1 = sc.nextInt();
System.out.println("name:");
String name1 = sc.next();
System.out.println("gender:");
String Gender1 = sc.next();
System.out.println("age:");
int Age1 = sc.nextInt();
System.out.println("homeTown:");
String HomeTown1 = sc.next();
System.out.println("salary:");
double salary1 = sc.nextDouble();
System.out.println("添加成功!");
System.out.println("是否继续添加员工");
String confirm = sc.next();
list.add(new Employee(id1, name1, Gender1, Age1, HomeTown1, salary1));
if ("Y".equalsIgnoreCase(confirm)) {
continue;
}else if ("N".equalsIgnoreCase(confirm)) {
System.out.println("--------------------欢迎登陆员工信息管理系统-------------------------------");
System.out.println();
System.out.println("编号\t姓名\t性别\t年龄\t籍贯\t\t\t\t薪资");
for (Object b : list) {
System.out.println(b);
}
System.out.println("------------------------------------------------------------------------");
}
break;
}
case 2:
while (true) {
System.out.println("请输入您要删除的员工信息:");
System.out.println("ID:");
int id2 = sc.nextInt();
System.out.println("name:");
String name2 = sc.next();
System.out.println("gender:");
String Gender2 = sc.next();
System.out.println("age:");
int Age2 = sc.nextInt();
System.out.println("homeTown:");
String HomeTown2 = sc.next();
System.out.println("salary:");
double salary2 = sc.nextDouble();
System.out.println("删除成功!");
System.out.println("是否继续删除员工");
String keyboardConfirm = sc.next();
if (list.contains(new Employee(id2, name2, Gender2, Age2, HomeTown2, salary2))) {
list.remove(new Employee(id2, name2, Gender2, Age2, HomeTown2, salary2));
}
if ("Y".equalsIgnoreCase(keyboardConfirm)) {
continue;
} else if ("N".equalsIgnoreCase(keyboardConfirm)) {
System.out.println("--------------------欢迎登陆员工信息管理系统-------------------------------");
System.out.println();
System.out.println("编号\t姓名\t性别\t年龄\t籍贯\t\t\t\t薪资");
for (Object a : list) {
System.out.println(a);
}
System.out.println("------------------------------------------------------------------------");
}
break;
}
case 3:
System.out.println("请输入你要修改的员工的原有信息");
System.out.println("ID:");
int id3=sc.nextInt();
System.out.println("name:");
String name3=sc.next();
System.out.println("gender:");
String Gender3=sc.next();
System.out.println("age:");
int Age3=sc.nextInt();
System.out.println("homeTown:");
String HomeTown3=sc.next();
System.out.println("salary:");
double salary3=sc.nextDouble();
if (list.contains(new Employee(id3,name3,Gender3,Age3,HomeTown3,salary3))) {
list.remove(new Employee(id3,name3,Gender3,Age3,HomeTown3,salary3));
}
System.out.println("输入你要修改的员工信息:");
System.out.println("ID:");
int id4=sc.nextInt();
System.out.println("name:");
String name4=sc.next();
System.out.println("gender:");
String Gender4=sc.next();
System.out.println("age:");
int Age4=sc.nextInt();
System.out.println("homeTown:");
String HomeTown4=sc.next();
System.out.println("salary:");
double salary4=sc.nextDouble();
list.add(new Employee(id4,name4,Gender4,Age4,HomeTown4,salary4));
System.out.println("--------------------欢迎登陆员工信息管理系统-------------------------------");
System.out.println();
System.out.println("编号\t姓名\t性别\t年龄\t籍贯\t\t\t\t薪资");
for (Object e:list) {
System.out.println(e);
}
System.out.println("------------------------------------------------------------------------");
break;
case 4:
System.out.println("请输入你要查询的员工信息");
System.out.println("ID:");
int id5=sc.nextInt();
System.out.println("name:");
String name5=sc.next();
System.out.println("gender:");
String Gender5=sc.next();
System.out.println("age:");
int Age5=sc.nextInt();
System.out.println("homeTown:");
String HomeTown5=sc.next();
System.out.println("salary:");
double salary5=sc.nextDouble();
if (list.contains(new Employee(id5,name5,Gender5,Age5,HomeTown5,salary5))){
System.out.println("在员工管理系统中查询到该成员:");
System.out.println("------------------------------------------------------------------------");
System.out.println("编号\t姓名\t性别\t年龄\t籍贯\t\t\t\t薪资");
System.out.println(new Employee(id5,name5,Gender5,Age5,HomeTown5,salary5));
System.out.println("------------------------------------------------------------------------");
}else {
System.out.println("你输入的员工信息不在员工管理系统中!!!");
}
break;
case 5:
System.out.println("退出系统!");
System.exit(1);
break;
}
}
}
3.创建执行类
public class EmployeeManagementTest {
public static void main(String[] args) {
EmployeeManagement employeeManagement=new EmployeeManagement();
employeeManagement.setMenu();
}
}
实现员工管理系统
最新推荐文章于 2023-11-19 23:46:07 发布