用java写员工管理系统 员工属性:员工编号、员工姓名、性别、年龄、所在部门 1————员工添加 2————员工删除 3————员工信息修改 4————根据员工编号查询员工信息 5————查询所有员工信息 要求员工信息存储在TreeSet中,.按照员工编号进行升序排序
时间: 2023-07-29 21:12:16 浏览: 626
好的,这是一个比较典型的 Java 项目。员工管理系统可以使用面向对象的思想来实现,每个员工可以用一个类来表示,包含员工编号、姓名、性别、年龄、所在部门等属性。下面是一个简单的示例:
```java
public class Employee implements Comparable<Employee> {
private int id;
private String name;
private char gender;
private int age;
private String department;
public Employee(int id, String name, char gender, int age, String department) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.department = department;
}
// getters and setters
@Override
public int compareTo(Employee other) {
return Integer.compare(this.id, other.id);
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", gender=" + gender + ", age=" + age + ", department="
+ department + "]";
}
}
```
这里实现了 `Comparable` 接口,以便可以按照员工编号进行排序。
接下来是员工管理系统的主类,包含添加、删除、修改、查询等操作。
```java
import java.util.Scanner;
import java.util.TreeSet;
public class EmployeeManager {
private TreeSet<Employee> employees;
public EmployeeManager() {
employees = new TreeSet<>();
}
public void addEmployee() {
Scanner input = new Scanner(System.in);
System.out.print("Enter employee id: ");
int id = input.nextInt();
System.out.print("Enter employee name: ");
String name = input.next();
System.out.print("Enter employee gender: ");
char gender = input.next().charAt(0);
System.out.print("Enter employee age: ");
int age = input.nextInt();
System.out.print("Enter employee department: ");
String department = input.next();
Employee employee = new Employee(id, name, gender, age, department);
employees.add(employee);
System.out.println("Employee added: " + employee);
}
public void removeEmployee() {
Scanner input = new Scanner(System.in);
System.out.print("Enter employee id: ");
int id = input.nextInt();
Employee employee = new Employee(id, "", ' ', 0, "");
if (employees.contains(employee)) {
employees.remove(employee);
System.out.println("Employee removed: " + employee);
} else {
System.out.println("Employee not found with id: " + id);
}
}
public void updateEmployee() {
Scanner input = new Scanner(System.in);
System.out.print("Enter employee id: ");
int id = input.nextInt();
Employee employee = new Employee(id, "", ' ', 0, "");
if (employees.contains(employee)) {
employees.remove(employee);
System.out.println("Enter new employee details:");
System.out.print("Enter employee name: ");
String name = input.next();
System.out.print("Enter employee gender: ");
char gender = input.next().charAt(0);
System.out.print("Enter employee age: ");
int age = input.nextInt();
System.out.print("Enter employee department: ");
String department = input.next();
Employee newEmployee = new Employee(id, name, gender, age, department);
employees.add(newEmployee);
System.out.println("Employee updated: " + newEmployee);
} else {
System.out.println("Employee not found with id: " + id);
}
}
public void searchEmployee() {
Scanner input = new Scanner(System.in);
System.out.print("Enter employee id: ");
int id = input.nextInt();
Employee employee = new Employee(id, "", ' ', 0, "");
if (employees.contains(employee)) {
System.out.println("Employee found: " + employee);
} else {
System.out.println("Employee not found with id: " + id);
}
}
public void listEmployees() {
System.out.println("List of all employees:");
for (Employee employee : employees) {
System.out.println(employee);
}
}
public static void main(String[] args) {
EmployeeManager manager = new EmployeeManager();
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("1. Add employee");
System.out.println("2. Remove employee");
System.out.println("3. Update employee");
System.out.println("4. Search employee");
System.out.println("5. List all employees");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
int choice = input.nextInt();
switch (choice) {
case 1:
manager.addEmployee();
break;
case 2:
manager.removeEmployee();
break;
case 3:
manager.updateEmployee();
break;
case 4:
manager.searchEmployee();
break;
case 5:
manager.listEmployees();
break;
case 0:
System.out.println("Exiting program...");
System.exit(0);
default:
System.out.println("Invalid choice!");
}
}
}
}
```
这里使用了一个 `TreeSet` 来存储所有员工信息,每个操作都可以通过调用 `TreeSet` 的方法来实现。注意在每个操作中,都需要先根据员工编号构造一个临时的 `Employee` 对象,以便可以在 `TreeSet` 中进行查找或删除。
阅读全文