14.职工信息查询程序 功能要求: 设计职工类,职工信息包括职工号、姓名、性别、年龄、学历、工资、住址、电话等 (自定义)(职工号不重复)。编写一个Java应用程序,实现职工信息从文件staf f.txt读取并通过姓名、工号查找指定职工信息。
时间: 2023-12-06 15:04:31 浏览: 231
好的,根据您的要求,我可以给您提供一个简单的职工信息查询程序的实现思路:
1. 定义职工类,包括职工号、姓名、性别、年龄、学历、工资、住址、电话等属性。可以使用构造方法和getter/setter方法对职工信息进行初始化和获取。
2. 定义一个职工管理类,用于实现职工信息的读取和查询。在职工管理类中,可以使用HashMap来存储职工信息,其中职工号作为键,职工对象作为值。
3. 在职工管理类中,编写一个方法用于从文件staff.txt中读取职工信息并将其存储到HashMap中。读取文件可以使用Java中的File和Scanner类。
4. 在职工管理类中,编写一个方法用于通过姓名或职工号来查询指定职工的信息。可以使用HashMap的get方法来获取指定的职工对象。
5. 在主函数中,创建职工管理对象,并调用读取职工信息和查询职工信息的方法,实现职工信息的查询。
以上是一个简单的职工信息查询程序的实现思路,您可以根据需求进行具体的实现。
相关问题
用java写一个职工信息查询程序 功能要求: 设计职工类,职工信息包括职工号、姓名、性别、年龄、学历、工资、住址、电话等(自定义)(职工号不重复)。编写一个Java应用程序,实现职工信息从文件staff.txt读取并通过姓名、工号查找指定职工信息。
好的,我可以给您提供一个简单的职工信息查询程序的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
```
然后,我们就可以运行程序,输入查询方式和要查询的信息,即可查询到指定的职工信息了。
3.职工信息管理系统代码要求:设计一个职工信息管理系统,职工信息包括职工号,姓名,性别,年龄,学历,工资,住址,电 话等(职工号不重复),并具有以下功能: (1)系统以菜单方式工作 (2)职工信息录入功能(职工信息用文件保存) (3)职工信息浏览功能 (4)查询和排序功能:按工资查询、按学历查询 (5)职工信息删除,修改功能
以下是职工信息管理系统的代码实现,包括职工信息的录入、浏览、查询、排序、删除和修改功能:
```python
# 定义员工信息类
class Employee:
def __init__(self, id, name, gender, age, education, salary, address, phone):
self.id = id
self.name = name
self.gender = gender
self.age = age
self.education = education
self.salary = salary
self.address = address
self.phone = phone
# 定义员工管理系统类
class EmployeeManagementSystem:
def __init__(self):
self.employees = []
self.filename = 'employees.txt'
# 菜单界面
def menu(self):
print('================== 职工信息管理系统 ==================')
print('1. 录入职工信息')
print('2. 浏览职工信息')
print('3. 查询职工信息')
print('4. 排序职工信息')
print('5. 删除职工信息')
print('6. 修改职工信息')
print('0. 退出系统')
print('====================================================')
# 录入员工信息
def add_employee(self):
id = input('请输入职工号:')
for e in self.employees:
if e.id == id:
print('职工号已存在,请重新输入!')
return
name = input('请输入姓名:')
gender = input('请输入性别:')
age = input('请输入年龄:')
education = input('请输入学历:')
salary = input('请输入工资:')
address = input('请输入住址:')
phone = input('请输入电话:')
employee = Employee(id, name, gender, age, education, salary, address, phone)
self.employees.append(employee)
self.save_to_file()
# 浏览员工信息
def browse_employee(self):
print('职工号\t姓名\t性别\t年龄\t学历\t工资\t住址\t电话')
for e in self.employees:
print('%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s' % (e.id, e.name, e.gender, e.age, e.education, e.salary, e.address, e.phone))
# 查询员工信息
def query_employee(self):
print('1. 按工资查询')
print('2. 按学历查询')
choice = input('请选择查询方式:')
if choice == '1':
salary = input('请输入工资:')
result = []
for e in self.employees:
if e.salary == salary:
result.append(e)
if len(result) > 0:
print('职工号\t姓名\t性别\t年龄\t学历\t工资\t住址\t电话')
for e in result:
print('%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s' % (e.id, e.name, e.gender, e.age, e.education, e.salary, e.address, e.phone))
else:
print('没有找到符合条件的员工!')
elif choice == '2':
education = input('请输入学历:')
result = []
for e in self.employees:
if e.education == education:
result.append(e)
if len(result) > 0:
print('职工号\t姓名\t性别\t年龄\t学历\t工资\t住址\t电话')
for e in result:
print('%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s' % (e.id, e.name, e.gender, e.age, e.education, e.salary, e.address, e.phone))
else:
print('没有找到符合条件的员工!')
else:
print('输入有误,请重新选择!')
# 排序员工信息
def sort_employee(self):
print('1. 按工资排序')
print('2. 按学历排序')
choice = input('请选择排序方式:')
if choice == '1':
self.employees.sort(key=lambda x:x.salary)
self.browse_employee()
elif choice == '2':
self.employees.sort(key=lambda x:x.education)
self.browse_employee()
else:
print('输入有误,请重新选择!')
# 删除员工信息
def delete_employee(self):
id = input('请输入要删除的职工号:')
for e in self.employees:
if e.id == id:
self.employees.remove(e)
print('删除成功!')
self.save_to_file()
return
print('职工号不存在,请重新输入!')
# 修改员工信息
def modify_employee(self):
id = input('请输入要修改的职工号:')
for e in self.employees:
if e.id == id:
name = input('请输入姓名:')
gender = input('请输入性别:')
age = input('请输入年龄:')
education = input('请输入学历:')
salary = input('请输入工资:')
address = input('请输入住址:')
phone = input('请输入电话:')
e.name = name
e.gender = gender
e.age = age
e.education = education
e.salary = salary
e.address = address
e.phone = phone
print('修改成功!')
self.save_to_file()
return
print('职工号不存在,请重新输入!')
# 将员工信息保存到文件中
def save_to_file(self):
with open(self.filename, 'w') as f:
for e in self.employees:
f.write('%s,%s,%s,%s,%s,%s,%s,%s\n' % (e.id, e.name, e.gender, e.age, e.education, e.salary, e.address, e.phone))
# 从文件中读取员工信息
def load_from_file(self):
try:
with open(self.filename, 'r') as f:
for line in f:
data = line.strip().split(',')
employee = Employee(data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7])
self.employees.append(employee)
except FileNotFoundError:
pass
# 运行员工管理系统
def run(self):
self.load_from_file()
while True:
self.menu()
choice = input('请选择操作:')
if choice == '1':
self.add_employee()
elif choice == '2':
self.browse_employee()
elif choice == '3':
self.query_employee()
elif choice == '4':
self.sort_employee()
elif choice == '5':
self.delete_employee()
elif choice == '6':
self.modify_employee()
elif choice == '0':
print('谢谢使用,再见!')
break
else:
print('输入有误,请重新选择!')
```
调用方法:
```python
ems = EmployeeManagementSystem()
ems.run()
```
在运行职工信息管理系统之前,需要先创建一个名为"employees.txt"的文件,用于保存员工信息。
阅读全文