import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class EmployeeProgram {

    public static void main(String[] args) {
        // 获取当前目录下所有员工文件
        File directory = new File(".");
        File[] files = directory.listFiles((dir, name) -> name.endsWith(".txt"));

        // 创建员工Map
        Map<String, Employee> employeeMap = new HashMap<>();

        // 读取并解析员工文件
        for (File file : files) {
            Employee employee = parseEmployeeFile(file);
            if (employee != null) {
                employeeMap.put(employee.getName(), employee);
            }
        }

        // 用户输入员工名字
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入员工名字: ");
        String name = scanner.nextLine();

        // 查找员工并输出信息
        Employee employee = employeeMap.get(name);
        if (employee != null) {
            System.out.println(employee.getName() + "," + employee.getAge() + "," + employee.getSalary() + "," + employee.getJoinDate());
            LocalDate anniversaryDate = employee.getJoinDate().plusYears(20);
            LocalDate saturday = getNextSaturday(anniversaryDate);
            System.out.println('入职20周年纪念日派对日期: ' + saturday);
        } else {
            System.out.println('查无此人');
        }
    }

    private static Employee parseEmployeeFile(File file) {
        try (FileInputStream fis = new FileInputStream(file);
             Scanner scanner = new Scanner(fis)) {
            String name = scanner.nextLine();
            int age = Integer.parseInt(scanner.nextLine());
            double salary = Double.parseDouble(scanner.nextLine());
            String joinDateStr = scanner.nextLine();

            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate joinDate = LocalDate.parse(joinDateStr, formatter);

            return new Employee(name, age, salary, joinDate);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static LocalDate getNextSaturday(LocalDate date) {
        LocalDate nextSaturday = date;
        while (nextSaturday.getDayOfWeek() != DayOfWeek.SATURDAY) {
            nextSaturday = nextSaturday.plusDays(1);
        }
        return nextSaturday;
    }
}

class Employee {
    private String name;
    private int age;
    private double salary;
    private LocalDate joinDate;

    public Employee(String name, int age, double salary, LocalDate joinDate) {
        this.name = name;
        this.age = age;
        this.salary = salary;
        this.joinDate = joinDate;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public double getSalary() {
        return salary;
    }

    public LocalDate getJoinDate() {
        return joinDate;
    }
}

该程序首先从当前目录下读取所有以.txt结尾的员工文件,并解析出每个员工的姓名、年龄、工资和入职日期,然后将这些信息存入一个Map中,其中key为员工姓名,value为Employee对象。 接下来,程序提示用户输入员工姓名,并根据姓名在Map中查找对应的Employee对象。如果找到,则输出员工的姓名、年龄、工资和入职日期,并计算该员工入职20周年纪念日当周的周六日期;如果未找到,则输出“查无此人”。 该程序使用Java 8的LocalDate类来处理日期操作,并使用DateTimeFormatter类来解析员工文件的入职日期。程序代码结构清晰,注释详细,易于理解和维护。 程序可以根据实际情况进行修改,例如可以增加其他员工信息字段,也可以修改程序读取员工文件的方式,例如从网络上下载员工文件等。 希望这个程序能够帮助您更好地理解Java编程语言,并为您的实际应用提供参考。


原文地址: http://www.cveoy.top/t/topic/o1Vy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录