import java.util.Scanner;
public class EmployeeManager {
private Employee[] employees;
private int size;
public EmployeeManager(int capacity) {
employees = new Employee[capacity];
size = 0;
}
public void addEmployee() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入员工姓名:");
String name = scanner.next();
System.out.print("请输入员工性别:");
String gender = scanner.next();
System.out.print("请输入员工年龄:");
int age = scanner.nextInt();
System.out.print("请输入员工所在部门:");
String department = scanner.next();
System.out.print("请输入员工职务:");
String position = scanner.next();
Employee employee = new Employee(name, gender, age, department, position);
employees[size++] = employee;
System.out.println("员工信息添加成功!");
}
public void queryEmployee() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要查询的员工姓名:");
String name = scanner.next();
boolean flag = false;
for (int i = 0; i < size; i++) {
if (employees[i].getName().equals(name)) {
employees[i].printInfo();
flag = true;
break;
}
}
if (!flag) {
System.out.println("没有找到该员工信息!");
}
}
public static void main(String[] args) {
EmployeeManager manager = new EmployeeManager(10);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 员工添加");
System.out.println("2. 员工查询");
int choice = scanner.nextInt();
switch (choice) {
case 1:
manager.addEmployee();
break;
case 2:
manager.queryEmployee();
break;
default:
System.out.println("输入有误,请重新选择!");
}
}
}
}
class Employee {
private String name;
private String gender;
private int age;
private String department;
private String position;
public Employee(String name, String gender, int age, String department, String position) {
this.name = name;
this.gender = gender;
this.age = age;
this.department = department;
this.position = position;
}
public void printInfo() {
System.out.println("员工姓名:" + name);
System.out.println("员工性别:" + gender);
System.out.println("员工年龄:" + age);
System.out.println("所在部门:" + department);
System.out.println("职务:" + position);
}
public String getName() {
return name;
}
}
```
import java.util.Arrays;
import java.util.Scanner;
public class StudentManager {
private final int size = 3;
private Student[] students = new Student[size];
private int currentSize = 0;
public StudentManager() {
students[0] = new Student("heima001", "张三", 20);
students[1] = new Student("heima002", "李四", 21);
students[2] = new Student("heima003", "王五", 22);
currentSize = size;
}
public void addStudent() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入学号:");
String id = scanner.next();
System.out.print("请输入姓名:");
String name = scanner.next();
System.out.print("请输入年龄:");
int age = scanner.nextInt();
// 判断学号是否唯一
boolean flag = false;
for (int i = 0; i < currentSize; i++) {
if (students[i].getId().equals(id)) {
flag = true;
break;
}
}
if (flag) {
System.out.println("学号已存在,添加失败!");
} else {
Student student = new Student(id, name, age);
students[currentSize++] = student;
System.out.println("学生信息添加成功!");
}
}
public void queryStudents() {
for (int i = 0; i < currentSize; i++) {
students[i].printInfo();
}
}
public void deleteStudent() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要删除的学生学号:");
String id = scanner.next();
// 查找学生索引
int index = -1;
for (int i = 0; i < currentSize; i++) {
if (students[i].getId().equals(id)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("没有找到该学生信息!");
} else {
// 移动数组元素
for (int i = index; i < currentSize - 1; i++) {
students[i] = students[i + 1];
}
// 将最后一个元素置为null
students[--currentSize] = null;
System.out.println("学生信息删除成功!");
}
}
public void updateAge() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要修改的学生学号:");
String id = scanner.next();
boolean flag = false;
for (int i = 0; i < currentSize; i++) {
if (students[i].getId().equals(id)) {
students[i].setAge(students[i].getAge() + 1);
System.out.println("学生年龄修改成功!");
flag = true;
break;
}
}
if (!flag) {
System.out.println("没有找到该学生信息!");
}
}
public static void main(String[] args) {
StudentManager manager = new StudentManager();
Scanner scanner = new Scanner(System.in);
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. 退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
manager.addStudent();
break;
case 2:
manager.queryStudents();
break;
case 3:
manager.deleteStudent();
break;
case 4:
manager.updateAge();
break;
case 5:
System.out.println("感谢使用学生管理系统!");
System.exit(0);
default:
System.out.println("输入有误,请重新选择!");
}
}
}
}
class Student {
private String id;
private String name;
private int age;
public Student(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public void printInfo() {
System.out.printf("学号:%s 姓名:%s 年龄:%d\n", id, name, age);
}
public String getId() {
return id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
5. 用Set存储实现员工管理系统,要求员工编号不重复
Employee.java:
```
import java.util.Objects;
public class Employee {
private int id;
private String name;
private String gender;
private int age;
private String department;
public Employee(int id, String name, String gender, int age, String department) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.department = department;
}
// Getters and Setters
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 getDepartment() { return department; }
public void setDepartment(String department) { this.department = department; }
// Override equals() and hashCode() to ensure id uniqueness in Set
@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;
}
@Override
public int hashCode() { return Objects.hash(id); }
// Override toString() for displaying employee info
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", department='" + department + '\'' +
'}';
}
}
```
EmployeeManager.java:
```
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class EmployeeManager {
private Set
public EmployeeManager() {
this.employees = new HashSet<>();
}
// Add employee
public boolean addEmployee(Employee employee) {
return employees.add(employee);
}
// Remove employee by id
public boolean removeEmployeeById(int id) {
for (Employee employee : employees) {
if (employee.getId() == id) {
return employees.remove(employee);
}
}
return false;
}
// Update employee info
public boolean updateEmployee(Employee employee) {
Employee emp = searchEmployeeById(employee.getId());
if (emp != null) {
emp.setName(employee.getName());
emp.setGender(employee.getGender());
emp.setAge(employee.getAge());
emp.setDepartment(employee.getDepartment());
return true;
} else {
return false;
}
}
// Search employee by id
public Employee searchEmployeeById(int id) {
for (Employee employee : employees) {
if (employee.getId() == id) {
return employee;
}
}
return null;
}
// Get all employees
public Set
return employees;
}
public static void main(String[] args) {
EmployeeManager manager = new EmployeeManager();
Scanner scanner = new Scanner(System.in);
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————查询所有员工信息");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("请输入员工编号:");
int id = scanner.nextInt();
System.out.print("请输入员工姓名:");
String name = scanner.next();
System.out.print("请输入员工性别:");
String gender = scanner.next();
System.out.print("请输入员工年龄:");
int age = scanner.nextInt();
System.out.print("请输入所在部门:");
String department = scanner.next();
Employee employee = new Employee(id, name, gender, age, department);
if (manager.addEmployee(employee)) {
System.out.println("添加成功");
} else {
System.out.println("添加失败,员工编号重复");
}
break;
case 2:
System.out.print("请输入员工编号:");
int deleteId = scanner.nextInt();
if (manager.removeEmployeeById(deleteId)) {
System.out.println("删除成功");
} else {
System.out.println("删除失败,该员工不存在");
}
break;
case 3:
System.out.print("请输入员工编号:");
int updateId = scanner.nextInt();
System.out.print("请输入员工姓名:");
String updateName = scanner.next();
System.out.print("请输入员工性别:");
String updateGender = scanner.next();
System.out.print("请输入员工年龄:");
int updateAge = scanner.nextInt();
System.out.print("请输入所在部门:");
String updateDepartment = scanner.next();
Employee updateEmployee = new Employee(updateId, updateName, updateGender, updateAge, updateDepartment);
if (manager.updateEmployee(updateEmployee)) {
System.out.println("修改成功");
} else {
System.out.println("修改失败,该员工不存在");
}
break;
case 4:
System.out.print("请输入员工编号:");
int searchId = scanner.nextInt();
Employee searchEmployee = manager.searchEmployeeById(searchId);
if (searchEmployee != null) {
System.out.println(searchEmployee.toString());
} else {
System.out.println("查询失败,该员工不存在");
}
break;
case 5:
Set
for (Employee emp : employees) {
System.out.println(emp.toString());
}
break;
default:
System.out.println("选择无效,请重新选择");
break;
}
}
}
}
```
6. 将员工信息存储在SortedSet(TreeSet)中
Employee.java:
```java
import java.util.Objects;
// Implement Comparable interface and override compareTo() method for sorting in TreeSet
public class Employee implements Comparable
private int id;
private String name;
private String gender;
private int age;
private String department;
public Employee(int id, String name, String gender, int age, String department) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.department = department;
}
// Getters and Setters
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 getDepartment() { return department; }
public void setDepartment(String department) { this.department = department; }
// Override equals() and hashCode() to ensure id uniqueness in Set
@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;
}
@Override
public int hashCode() { return Objects.hash(id); }
// Override toString() for displaying employee info
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", department='" + department + '\'' +
'}';
}
// Override compareTo() method for comparing by id in TreeSet
@Override
public int compareTo(Employee o) {
return Integer.compare(this.id, o.getId());
}
}
```
EmployeeManager.java:
```java
import java.util.Scanner;
import java.util.SortedSet;
import java.util.TreeSet;
public class EmployeeManager {
private SortedSet
public EmployeeManager() {
this.employees = new TreeSet<>();
}
// Add employee
public boolean addEmployee(Employee employee) {
return employees.add(employee);
}
// Remove employee by id
public boolean removeEmployeeById(int id) {
for (Employee employee : employees) {
if (employee.getId() == id) {
return employees.remove(employee);
}
}
return false;
}
// Update employee info
public boolean updateEmployee(Employee employee) {
Employee emp = searchEmployeeById(employee.getId());
if (emp != null) {
emp.setName(employee.getName());
emp.setGender(employee.getGender());
emp.setAge(employee.getAge());
emp.setDepartment(employee.getDepartment());
return true;
} else {
return false;
}
}
// Search employee by id
public Employee searchEmployeeById(int id) {
for (Employee employee : employees) {
if (employee.getId() == id) {
return employee;
}
}
return null;
}
// Get all employees
public SortedSet
return employees;
}
public static void main(String[] args) {
EmployeeManager manager = new EmployeeManager();
Scanner scanner = new Scanner(System.in);
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————查询所有员工信息");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("请输入员工编号:");
int id = scanner.nextInt();
System.out.print("请输入员工姓名:");
String name = scanner.next();
System.out.print("请输入员工性别:");
String gender = scanner.next();
System.out.print("请输入员工年龄:");
int age = scanner.nextInt();
System.out.print("请输入所在部门:");
String department = scanner.next();
Employee employee = new Employee(id, name, gender, age, department);
if (manager.addEmployee(employee)) {
System.out.println("添加成功");
} else {
System.out.println("添加失败,员工编号重复");
}
break;
case 2:
System.out.print("请输入员工编号:");
int deleteId = scanner.nextInt();
if (manager.removeEmployeeById(deleteId)) {
System.out.println("删除成功");
} else {
System.out.println("删除失败,该员工不存在");}
break;
case 3:
System.out.print("请输入员工编号:");
int updateId = scanner.nextInt();
System.out.print("请输入员工姓名:");
String updateName = scanner.next();
System.out.print("请输入员工性别:");
String updateGender = scanner.next();
System.out.print("请输入员工年龄:");
int updateAge = scanner.nextInt();
System.out.print("请输入所在部门:");
String updateDepartment = scanner.next();
Employee updateEmployee = new Employee(updateId, updateName, updateGender, updateAge, updateDepartment);
if (manager.updateEmployee(updateEmployee)) {
System.out.println("修改成功");
} else {
System.out.println("修改失败,该员工不存在");
}
break;
case 4:
System.out.print("请输入员工编号:");
int searchId = scanner.nextInt();
Employee searchEmployee = manager.searchEmployeeById(searchId);
if (searchEmployee != null) {
System.out.println(searchEmployee.toString());
} else {
System.out.println("查询失败,该员工不存在");
}
break;
case 5:
SortedSet
for (Employee emp : employees) {
System.out.println(emp.toString());
}
break;
default:
System.out.println("选择无效,请重新选择");
break;
}
}
}
}
```