栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java小项目之简单职工信息管理系统(io流存储数据)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java小项目之简单职工信息管理系统(io流存储数据)

项目要求

设计一个职工信息管理系统,提供以下功能:
(1)职工信息录入功能(职工信息用文件保存)——输入
(2)职工信息浏览功能——输出
(3)查询或排序功能:(至少一种查询方式)——算法,按工资查询,按学历查询等
(4)职工信息删除功能
(5)职工信息修改功能
(6)职工信息包括职工号、姓名、性别、出生年月、学历、职务、工资、住址、电话等,并且要求职工号不重复.

代码实现 主类
public class ProgramMain {
	 public static void main(String[] args) {
	        Menu.selectMenu();
	    }
}
内容定义类
public class Constant {
	
	public static final String ONE = "1";

	public static final String TWO = "2";

	public static final String THREE = "3";

	public static final String FOUR = "4";

	public static final String FIVE = "5";

	public static final String SIX = "6";

	public static final String SEVEN = "7";

	public static final String QUERY_TYPE_ONE = "01";

	public static final String QUERY_TYPE_TWO = "02";

	public static final String CONTINUE_TYPE_N = "N";
}
菜单类
import java.util.Scanner;



public class Menu {
	
	public static void selectMenu(){
        printMenuInfo();
        Scanner sc = new Scanner(System.in);
        String choice=sc.nextLine();
        switch(choice){
            case Constant.ONE:
                System.out.println("录入信息:");
                EmployeeFactory.getEmployee().add();
                break;
            case Constant.TWO:
                System.out.println("查看全部职工信息:");
                EmployeeFactory.getEmployee().findAll();
                break;
            case Constant.THREE:
                System.out.println("按条件查询或排序:01.按学历查询 02.按职工号排序");
                EmployeeFactory.getEmployee().findByConditionOrSort();
                break;
            case Constant.FOUR:
                System.out.println("修改信息:");
                EmployeeFactory.getEmployee().edit();
                break;
            case Constant.FIVE:
                System.out.println("删除信息(根据职工号进行删除):");
                EmployeeFactory.getEmployee().del();
                break;
            case Constant.SIX:
                System.out.println("统计所有职工数(根据职工号进行统计):");
                EmployeeFactory.getEmployee().sum();
                break;
            case Constant.SEVEN:
                System.out.println("退出系统");
                return;
            default:
                System.out.println("无此功能");
                break;
        }
        selectMenu();
    }

    public static void printMenuInfo(){
        System.out.println("**********************************************职工信息管理系统*********************************************");
        System.out.println("*1.录入信息t2.查看全部信息t 3.按条件查询或排序t 4.修改信息t 5.删除信息t 6.统计所有职工数t 7.退出系统*");
        System.out.println("*********************************************************************************************************");
        System.out.print("n");
        System.out.print("请选择菜单:");
    }
}

职工类
import java.io.Serializable;

public class Employee implements Comparable, Serializable {

    
    private static final long serialVersionUID = 1L;
    private String id;
    private String name;
    private String gender;
    private String birth;
    private String edu;
    private String job;
    private double salary;
    private String address;
    private String phone;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    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 String getBirth() {
        return birth;
    }

    public void setBirth(String birth) {
        this.birth = birth;
    }

    public String getEdu() {
        return edu;
    }

    public void setEdu(String edu) {
        this.edu = edu;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    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;
    }

    public Employee() {}
    
    public Employee(String id, String name) {
        this.id = id;
        this.name = name;
    }
    
    public Employee(String id, String name,String edu) {
        this.id = id;
        this.name = name;
        this.edu = edu;
    }
    public Employee(String id, String name, String gender, String birth, String edu, String job, double salary, String address, String phone) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.birth = birth;
        this.edu = edu;
        this.job = job;
        this.salary = salary;
        this.address = address;
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "职工号='" + id + ''' +
                ", 姓名='" + name + ''' +
                ", 性别='" + gender + ''' +
                ", 出生年月='" + birth + ''' +
                ", 学历='" + edu + ''' +
                ", 职务='" + job + ''' +
                ", 工资=" + salary +
                ", 住址='" + address + ''' +
                ", 电话='" + phone + '''
                ;
    }

    @Override
    public int compareTo(Employee o) {
        return Integer.parseInt(this.id) - Integer.parseInt(o.getId());
    }
}
单例对象类
public class EmployeeFactory {
    private static IEmployee employee;
    public static IEmployee getEmployee(){
        if (employee == null){
            employee = new EmployeeImpl();
        }
        return employee;
    }
}
功能接口
public interface IEmployee {
	void add();
	void findAll();
	void findByConditionOrSort();
	void edit();
	void del();
	void sum();
}
接口实现类
import java.util.*;

public class EmployeeImpl implements IEmployee{

    private Scanner sc=new Scanner(System.in);
    public List employeeList = new ArrayList<>();

    @Override
    public void add() {
        boolean flag = false;
        while (true){
            System.out.println("请输入职工号:");
            String id = sc.nextLine();
            if(isExsit(flag, id)){
                flag = false;
                continue;
            }
            System.out.println("请输入姓名:");   String name=sc.nextLine();
            System.out.println("请输入性别:");   String gender=sc.nextLine();
            System.out.println("请输入出生年月:");String birth=sc.nextLine();
            System.out.println("请输入学历:");   String edu=sc.nextLine();
            System.out.println("请输入职务:");   String job=sc.nextLine();
            System.out.println("请输入工资:");   String salary=sc.nextLine();
            System.out.println("请输入住址:");   String address=sc.nextLine();
            System.out.println("请输入电话:");   String phone=sc.nextLine();
            employeeList.add(new Employee(id,name,gender,birth,edu,job,Double.parseDouble(salary),address,phone));
            if(Constant.CONTINUE_TYPE_N.equalsIgnoreCase(getContinueType())){
                FileUtil.writeFile(employeeList);
                break;
            }
        }
    }

    @Override
    public void findAll() {
        if(employeeList.size() == 0){
            System.out.println("暂未查询到职工信息!!!");
        }else{
            FileUtil.readFile();
        }
    }

    @Override
    public void findByConditionOrSort() {
        while (true){
            System.out.println("请选择查询类型01或02:");
            String tytpe = sc.nextLine();
            if (Constant.QUERY_TYPE_ONE.equals(tytpe)){
                System.out.println("请输入学历信息:");
                printEduInfo();
            }else if (Constant.QUERY_TYPE_TWO.equals(tytpe)){
                sort();
            }else {
                System.out.println("抱歉,不存在该类型");
                continue;
            }

            if(Constant.CONTINUE_TYPE_N.equalsIgnoreCase(getContinueType())){
                break;
            }
        }
    }


    @Override
    public void edit() {
        while (true){
            System.out.println("请提供需要修改职工的原职工号:");
            String id = sc.nextLine();
            boolean flag = getEmployeeById(id);
            if (!flag){
                System.out.println("抱歉,没有找到该职工信息!!!");
                continue;
            }
            System.out.println("请输入修改后的职工姓名:");
            String name = sc.nextLine();
            updateEmployee(id,name);
            if(Constant.CONTINUE_TYPE_N.equalsIgnoreCase(getContinueType())){
                break;
            }
        }
    }

    @Override
    public void del() {
        while (true){
            System.out.println("请输入需要删除的职工号:");
            remove(sc.nextLine());
            if(Constant.CONTINUE_TYPE_N.equalsIgnoreCase(getContinueType())){
                break;
            }
        }
    }

    @Override
    public void sum() {
        System.out.println("职工总数为:" + employeeList.size() + "人");
    }


    private List filterInfo(String edu) {
        List emps = new ArrayList<>();
        Iterator iterator = employeeList.iterator();
        while (iterator.hasNext()) {
            Employee next = iterator.next();
            String name = next.getEdu();
            if (name.equals(edu)) {
                emps.add(next);
            }
        }
        return emps;
    }

    private void remove(String empId) {
        boolean flag = false;
        Iterator iterator = employeeList.iterator();
        while (iterator.hasNext()) {
            Employee next = iterator.next();
            if (empId.equals(next.getId())) {
                flag = true;
                iterator.remove();
            }
        }
        if (flag){
            System.out.println("删除"+empId+"号职工成功");
            FileUtil.writeFile(employeeList);
            return;
        }
        System.out.println("抱歉,没有查询到该职工!");


    }

    private boolean getEmployeeById(String id) {
        boolean flag = false;
        Iterator iterator = employeeList.iterator();
        while (iterator.hasNext()) {
            Employee next = iterator.next();
            if (id.equals(next.getId())) {
                flag = true;
                System.out.println("职工信息:" + next.toString());
                break;
            }
        }
        return flag;
    }

    private void updateEmployee(String id,String name) {
        boolean flag = false;
        Iterator iterator = employeeList.iterator();
        while (iterator.hasNext()) {
            Employee next = iterator.next();
            if (id.equals(next.getId())) {
                flag = true;
                next.setName(name);
                System.out.println("修改成功,修改之后的职工信息:" + next.toString());
                break;
            }
        }
        if (flag){
            FileUtil.writeFile(employeeList);
            return;
        }
    }

    private void sort() {
        System.out.println("-------------排序前-------------");
        for (Employee employee : employeeList) {
            System.out.println(employee.toString());
        }
        System.out.println("-------------排序后-------------");
        Collections.sort(employeeList);
        for (Employee employee : employeeList) {
            System.out.println(employee.toString());
        }

    }

    private void printEduInfo() {
        String edu = sc.nextLine();
        List emps = filterInfo(edu);
        if(emps == null || emps.size() == 0){
            System.out.println("没有查询到学历信息!");
        }else{
            for (Employee emp : emps) {
                System.out.println(emp.toString());
            }
        }
    }

    private boolean isExsit(boolean flag, String id) {
        for(Employee emp : employeeList){
            if(id.equals(emp.getId())){
                System.out.println("抱歉,职工号已存在!");
                flag = true;
                break;
            }
        }
        return flag;
    }

    private String getContinueType() {
        System.out.println("请问是否继续录入?Y/N");
        return sc.nextLine();
    }
}
文件工具类
import java.io.*;
import java.util.List;

public class FileUtil {
	private static File file=new File("employeeInfo.txt");
    public static void writeFile(List employeeList) {
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(file));
            bw.write("职工信息如下:");
            bw.newline();
            for (Employee employee : employeeList) {
                bw.write(employee.toString());
                bw.newline();

            }
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void readFile() {
        BufferedReader br;
        String contentLine;
        try {
            br = new BufferedReader(new FileReader(file));
            while ((contentLine = br.readLine()) != null) {
                System.out.println(contentLine);
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/318771.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号