Java面向对象练习-员工管理系统(ArrayList+IO写入文件)

需求详见:需求内容
本次将存储方式变下,通过List保存员工信息,并最终写入文件。只修改Test.java。

代码新增四个文件操作方法:
createFile:创建文件
saveFile:保存单个员工信息
saveFileListAndClearHistory:保存多个员工信息覆盖原有文件
clearFile:清空文件

Test.java代码如下:

package jingshenxiaohuo.com.test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @program: testApplication
 * @description:
 **/
public class Test {
    private static File file = new File("D:/Employ.txt");
    private static Scanner sc = new Scanner(System.in);
    private static ArrayList<Employee> em = new ArrayList<Employee>();

    //静态代码块,类初始化时执行
    static {
        //先创建文件
        createFile();
    }

    //操作入口
    public static void opt() {
        System.out.println("----     工资管理系统                  ----");
        System.out.println("-------------------------------");
        System.out.println("---        1     增加                        ---");
        System.out.println("---        2     删除                        ---");
        System.out.println("---        3     修改                        ---");
        System.out.println("---        4     查询                        ---");
        System.out.println("---        0     退出                        ---");
        System.out.println("-------------------------------");
        System.out.println("请输入你要选择的操作:");
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        switch (s) {
            case "1":
                addEmployee();
                break;
            case "2":
                delEmployee();
                break;
            case "3":
                updateEmployee();
                break;
            case "4":
                queryEmployee();
                break;
            case "0":
                System.out.println("谢谢使用");
                break;
            default:
                System.out.println("指令错误请重新输入!");
                opt();
                break;
        }

    }

    //新增员工
    public static void addEmployee() {
        System.out.println("------增加员工------");
        System.out.println("请输入相关信息:");
        System.out.print("ID:");
        String id = sc.next();
        System.out.print("姓名:");
        String name = sc.next();
        System.out.print("职务:");
        String position = sc.next();
        System.out.print("请假天数:");
        int holiday = sc.nextInt();
        System.out.print("基本工资:");
        double salary = sc.nextInt();
        Employee a = null;
        switch (position) {
            case "普通员工":
                a = new CommonEmployee(id,name,position,holiday,salary);
                break;
            case "经理":
                a = new Manager(id,name,position,holiday,salary);
                break;
            case "董事长":
                a = new Director(id,name,position,holiday,salary);
                break;
            default:
                System.out.println("不存在此职务,请重新输入!");
                addEmployee();
                break;
        }
        if (a != null) {
            a.sumSalary();
            em.add(a);
            saveFile(a);
            System.out.println("添加成功!");
        }
        opt();
    }

    //删除员工
    public static void delEmployee() {
        System.out.println("----------删除员工---------");
        System.out.println("请输入员工姓名:");
        String n = sc.next();
        for (int i = 0; i < em.size(); i++) {
            if (em.get(i).name.equals(n)) {
                System.out.println("你要删除的是:");
                em.get(i).display();
                System.out.println("你确定要删除吗?\n [Y]确定,[N]取消");
                String s = sc.next();
                if (s.equals("y")) {
                    em.remove(i);
                    //重新写入List
                    saveFileListAndClearHistory(em);
                    System.out.println("删除成功!");
                    opt();
                } else if (s.equals("n")) {
                    opt();
                } else {
                    System.out.println("输入指令不正确,请重新输入!");
                    delEmployee();
                }
            } else {
                if (i != em.size() - 1) {
                    continue;
                } else {
                    System.out.println("你输入的账号不存在!请重新输入!");
                    delEmployee();
                }

            }
        }
    }

    //修改员工
    public static void updateEmployee(){
        System.out.println("--------------修改员工资料-------------");
        System.out.println("请输入你要修改的姓名:");
        String s = sc.next();
        out: for (int i = 0; i < em.size(); i++) {
            if (em.get(i).name.equals(s)) {
                System.out.println("你要修改的是:");
                em.get(i).display();
                System.out.println("请重新输入相关信息:");
                System.out.print("ID:");
                String id = sc.next();
                System.out.print("姓名:");
                String name = sc.next();
                System.out.print("职务:");
                String position = sc.next();
                System.out.print("请假天数:");
                int holiday = sc.nextInt();
                System.out.print("基本工资:");
                double salary = sc.nextDouble();
                if (em.get(i).position.equals(position)) {
                    em.get(i).ID = id;
                    em.get(i).name = name;
                    em.get(i).position = position;
                    em.get(i).holiday = holiday;
                    em.get(i).salary = salary;
                    em.get(i).sumSalary();
                    System.out.println("修改成功!");
                    em.get(i).display();
                }else{
                    Employee a = null;
                    switch (position) {
                        case "普通员工":
                            a = new CommonEmployee(id,name,position,holiday,salary);
                            break;
                        case "经理":
                            a = new Manager(id,name,position,holiday,salary);
                            break;
                        case "董事长":
                            a = new Director(id,name,position,holiday,salary);
                            break;
                        default:
                            System.out.println("不存在此职务,请重新输入!");
                            addEmployee();
                            break;
                    }
                    if (a != null) {
                        a.sumSalary();
                        em.set(i, a);
                        saveFileListAndClearHistory(em);
                    }

                }
                opt();
            } else {
                if (i != em.size() - 1) {
                    continue out;
                } else {
                    System.out.println("你输入的员工不存在!请重新输入!");
                    updateEmployee();
                }
            }
        }
    }

    //查询员工
    public static void queryEmployee() {
        System.out.println("--------------所有员工信息---------------");
        try {
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            String s = "";
            while ((s = br.readLine()) != null) {
                System.out.println(s);
            }
            br.close();
        } catch (Exception e) {
            System.out.println("读取失败!异常信息:"+e.getMessage());
        }
        opt();
    }


    //创建文件
    private static void createFile() {
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                System.out.println("创建失败!异常信息:"+e.getMessage());
            }
        } else {
            file.delete();
        }
    }
    //增量保存文件
    private static void saveFile(Employee employee){
        try {
            FileWriter fw = new FileWriter(file, true);
            fw.append(employee.ID + "\t");
            fw.append(employee.name + "\t");
            fw.append(employee.position + "\t");
            fw.append(employee.holiday + "\t");
            fw.append(employee.salary + "\t\r\n");
            fw.flush();
            fw.close();
        }catch (Exception e){
            System.out.println("保存失败!异常信息:"+e.getMessage());
        }
    }
    //全量覆盖文件
    private static void saveFileListAndClearHistory(List<Employee> employeeList){
        try {
            FileWriter fw = new FileWriter(file);
            for (Employee employee : employeeList) {
                fw.append(employee.ID + "\t");
                fw.append(employee.name + "\t");
                fw.append(employee.position + "\t");
                fw.append(employee.holiday + "\t");
                fw.append(employee.salary + "\t\r\n");
            }
            fw.flush();
            fw.close();
        }catch (Exception e){
            System.out.println("保存失败!异常信息:"+e.getMessage());
        }
    }
    //清空文件
    private static void clearFile(){
        try {
            FileWriter fileWriter = new FileWriter(file);
            fileWriter.flush();
            fileWriter.close();
        } catch (Exception e) {
            System.out.println("清空失败!异常信息:"+e.getMessage());
        }
    }

    public static void main(String[] args) {
        Test.opt();
    }
}

  • 5
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值