用java设计一个人事管理系统,能够实现员工信息(员工编号、姓名、性别、年龄、部门、联系方式)的增 加、删除、修改、查询以及更新功能,将员工信息及其变动情况存储到out.txt中。要求使用类的定义、GUI界面设计、文件IO、集合
时间: 2023-10-20 14:32:48 浏览: 200
基于SpringBoot+Mybatis+Thymeleaf架构实现的员工管理系统(源码+数据库),适合java毕业设计,课设等
好的,我会尽力回答你的问题。
首先,我们可以设计一个Employee类,用于存储员工信息。每个Employee对象将包含员工编号、姓名、性别、年龄、部门、联系方式等属性。代码如下:
```java
public class Employee {
private int id;
private String name;
private String gender;
private int age;
private String department;
private String contact;
public Employee(int id, String name, String gender, int age, String department, String contact) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.department = department;
this.contact = contact;
}
// getter and setter methods
}
```
接下来,我们可以设计一个GUI界面,用于显示员工信息,并提供增加、删除、修改、查询和更新员工信息的功能。代码如下:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
public class HRSystemGUI extends JFrame implements ActionListener {
private ArrayList<Employee> employeeList = new ArrayList<>();
private JTextField idField, nameField, genderField, ageField, departmentField, contactField;
private JTextArea messageArea;
public HRSystemGUI() {
super("人事管理系统");
// 创建窗口组件
JButton addButton = new JButton("增加");
JButton deleteButton = new JButton("删除");
JButton modifyButton = new JButton("修改");
JButton queryButton = new JButton("查询");
JButton updateButton = new JButton("更新");
JLabel idLabel = new JLabel("编号:");
JLabel nameLabel = new JLabel("姓名:");
JLabel genderLabel = new JLabel("性别:");
JLabel ageLabel = new JLabel("年龄:");
JLabel departmentLabel = new JLabel("部门:");
JLabel contactLabel = new JLabel("联系方式:");
idField = new JTextField(10);
nameField = new JTextField(10);
genderField = new JTextField(10);
ageField = new JTextField(10);
departmentField = new JTextField(10);
contactField = new JTextField(10);
messageArea = new JTextArea(10, 20);
JScrollPane scrollPane = new JScrollPane(messageArea);
// 设置布局
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(6, 2));
panel1.add(idLabel);
panel1.add(idField);
panel1.add(nameLabel);
panel1.add(nameField);
panel1.add(genderLabel);
panel1.add(genderField);
panel1.add(ageLabel);
panel1.add(ageField);
panel1.add(departmentLabel);
panel1.add(departmentField);
panel1.add(contactLabel);
panel1.add(contactField);
JPanel panel2 = new JPanel();
panel2.setLayout(new GridLayout(1, 5));
panel2.add(addButton);
panel2.add(deleteButton);
panel2.add(modifyButton);
panel2.add(queryButton);
panel2.add(updateButton);
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(panel1, BorderLayout.NORTH);
container.add(scrollPane, BorderLayout.CENTER);
container.add(panel2, BorderLayout.SOUTH);
// 添加事件监听器
addButton.addActionListener(this);
deleteButton.addActionListener(this);
modifyButton.addActionListener(this);
queryButton.addActionListener(this);
updateButton.addActionListener(this);
// 加载员工信息
loadEmployees();
}
// 加载员工信息
private void loadEmployees() {
try {
BufferedReader reader = new BufferedReader(new FileReader("out.txt"));
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
Employee employee = new Employee(Integer.parseInt(fields[0]), fields[1], fields[2], Integer.parseInt(fields[3]), fields[4], fields[5]);
employeeList.add(employee);
}
reader.close();
showMessage("员工信息已加载!");
} catch (IOException e) {
showMessage("无法加载员工信息:" + e.getMessage());
}
}
// 显示消息
private void showMessage(String message) {
messageArea.append(message + "\n");
}
// 处理事件
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
switch (command) {
case "增加":
addEmployee();
break;
case "删除":
deleteEmployee();
break;
case "修改":
modifyEmployee();
break;
case "查询":
queryEmployee();
break;
case "更新":
updateEmployees();
break;
}
}
// 增加员工信息
private void addEmployee() {
int id = Integer.parseInt(idField.getText());
String name = nameField.getText();
String gender = genderField.getText();
int age = Integer.parseInt(ageField.getText());
String department = departmentField.getText();
String contact = contactField.getText();
Employee employee = new Employee(id, name, gender, age, department, contact);
employeeList.add(employee);
showMessage("员工信息已添加!");
}
// 删除员工信息
private void deleteEmployee() {
int id = Integer.parseInt(idField.getText());
for (Employee employee : employeeList) {
if (employee.getId() == id) {
employeeList.remove(employee);
showMessage("员工信息已删除!");
return;
}
}
showMessage("找不到该员工信息!");
}
// 修改员工信息
private void modifyEmployee() {
int id = Integer.parseInt(idField.getText());
for (Employee employee : employeeList) {
if (employee.getId() == id) {
employee.setName(nameField.getText());
employee.setGender(genderField.getText());
employee.setAge(Integer.parseInt(ageField.getText()));
employee.setDepartment(departmentField.getText());
employee.setContact(contactField.getText());
showMessage("员工信息已修改!");
return;
}
}
showMessage("找不到该员工信息!");
}
// 查询员工信息
private void queryEmployee() {
int id = Integer.parseInt(idField.getText());
for (Employee employee : employeeList) {
if (employee.getId() == id) {
showMessage("编号:" + employee.getId() + "\n姓名:" + employee.getName() + "\n性别:" + employee.getGender() + "\n年龄:" + employee.getAge() + "\n部门:" + employee.getDepartment() + "\n联系方式:" + employee.getContact());
return;
}
}
showMessage("找不到该员工信息!");
}
// 更新员工信息
private void updateEmployees() {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("out.txt"));
for (Employee employee : employeeList) {
writer.write(employee.getId() + "," + employee.getName() + "," + employee.getGender() + "," + employee.getAge() + "," + employee.getDepartment() + "," + employee.getContact() + "\n");
}
writer.close();
showMessage("员工信息已更新!");
} catch (IOException e) {
showMessage("无法更新员工信息:" + e.getMessage());
}
}
public static void main(String[] args) {
HRSystemGUI hrSystemGUI = new HRSystemGUI();
hrSystemGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
hrSystemGUI.setSize(400, 300);
hrSystemGUI.setVisible(true);
}
}
```
我们可以使用JFrame作为主窗口,将各种组件添加到窗口上。为了方便显示员工信息,我们使用了JTextArea组件,并将其添加到JScrollPane组件中,以支持滚动条。同时,我们还为各个按钮添加了事件监听器,用于处理增加、删除、修改、查询和更新员工信息的操作。
为了实现文件IO,我们使用了BufferedReader和BufferedWriter类,用于读取和写入文件。我们将员工信息存储到out.txt文件中,每个员工占一行,各属性之间用逗号分隔。
为了方便存储和管理员工信息,我们使用了ArrayList类,用于存储所有Employee对象。
最后,我们可以在main方法中创建一个HRSystemGUI对象,并设置窗口的大小和可见性。这样,我们就可以运行这个人事管理系统了。
注意:本示例程序并未对输入进行完整性检查和异常处理,实际使用时需要加以改进。
阅读全文