用java写一个职工信息查询程序 功能要求: 设计职工类,职工信息包括职工号、姓名、性别、年龄、学历、工资、住址、电话等(自定义)(职工号不重复)。编写一个Java应用程序,实现职工信息从文件staff.txt读取并通过姓名、工号查找指定职工信息。
时间: 2023-12-07 12:05:42 浏览: 120
用JAVA语言编写的职工信息查询的程序
4星 · 用户满意度95%
好的,我可以给您提供一个简单的职工信息查询程序的Java代码示例,您可以参考。首先,我们需要先创建一个职工类Worker:
```java
public class Worker {
private String workerNo; // 职工号
private String name; // 姓名
private String gender; // 性别
private int age; // 年龄
private String education; // 学历
private double salary; // 工资
private String address; // 住址
private String phone; // 电话
// 构造函数
public Worker(String workerNo, String name, String gender, int age, String education, double salary, String address, String phone) {
this.workerNo = workerNo;
this.name = name;
this.gender = gender;
this.age = age;
this.education = education;
this.salary = salary;
this.address = address;
this.phone = phone;
}
// getter和setter方法
public String getWorkerNo() {
return workerNo;
}
public void setWorkerNo(String workerNo) {
this.workerNo = workerNo;
}
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 getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
```
然后,我们需要创建一个工具类来读取staff.txt文件中的职工信息,并将其存储到一个Worker对象的列表中:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
public class WorkerUtil {
// 从文件中读取职工信息并存储到列表中
public static List<Worker> readWorkersFromFile(String filePath) {
List<Worker> workers = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
if (fields.length != 8) {
continue;
}
String workerNo = fields[0];
String name = fields[1];
String gender = fields[2];
int age = Integer.parseInt(fields[3]);
String education = fields[4];
double salary = Double.parseDouble(fields[5]);
String address = fields[6];
String phone = fields[7];
Worker worker = new Worker(workerNo, name, gender, age, education, salary, address, phone);
workers.add(worker);
}
} catch (Exception e) {
e.printStackTrace();
}
return workers;
}
}
```
接着,我们需要创建一个主程序类来实现职工信息查询功能:
```java
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 从文件中读取职工信息
List<Worker> workers = WorkerUtil.readWorkersFromFile("staff.txt");
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输入查询方式(1:按姓名查询,2:按职工号查询,0:退出):");
int option = scanner.nextInt();
if (option == 0) {
break;
}
if (option == 1) {
System.out.println("请输入姓名:");
String name = scanner.next();
boolean found = false;
for (Worker worker : workers) {
if (worker.getName().equals(name)) {
found = true;
System.out.println(worker.getWorkerNo() + "," + worker.getName() + "," + worker.getGender() + "," + worker.getAge() + "," + worker.getEducation() + "," + worker.getSalary() + "," + worker.getAddress() + "," + worker.getPhone());
}
}
if (!found) {
System.out.println("没有找到姓名为" + name + "的职工信息");
}
} else if (option == 2) {
System.out.println("请输入职工号:");
String workerNo = scanner.next();
boolean found = false;
for (Worker worker : workers) {
if (worker.getWorkerNo().equals(workerNo)) {
found = true;
System.out.println(worker.getWorkerNo() + "," + worker.getName() + "," + worker.getGender() + "," + worker.getAge() + "," + worker.getEducation() + "," + worker.getSalary() + "," + worker.getAddress() + "," + worker.getPhone());
}
}
if (!found) {
System.out.println("没有找到职工号为" + workerNo + "的职工信息");
}
} else {
System.out.println("输入有误,请重新输入!");
}
}
}
}
```
在实现职工信息查询功能之前,我们需要在项目根目录下创建一个staff.txt文件,并将职工信息存储在其中。每行表示一个职工信息,字段之间用逗号分隔,例如:
```
001,张三,男,28,本科,5000,北京市朝阳区,13800000001
002,李四,女,32,硕士,8000,北京市海淀区,13800000002
003,王五,男,25,本科,4500,北京市西城区,13800000003
```
然后,我们就可以运行程序,输入查询方式和要查询的信息,即可查询到指定的职工信息了。
阅读全文