一个可以存入文档的员工管理系统

直接上代码


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import com.situ.demo2.Person;

public class Employee {
	public List<Staff> staff = new ArrayList<>();

	public static void main(String[] args) {
		Employee user = new Employee();
		user.system();
	}

	public void system() {
		Scanner sc = new Scanner(System.in);
		while (true) {
			System.out.println("-------------------欢迎进入员工管理系统---------------------");
			System.out.println("1.添加员工   2.开除员工   3.查询员工   4.修改员工信息   5.记录   6.查看");
			int i = sc.nextInt();
			switch (i) {
			case 1:
				add();
				break;
			case 2:
				delate();
				break;
			case 3:
				query();
				break;
			case 4:
				modify();
				break;
			case 5:
				output();
				break;
			case 6:
				input();
				break;
			default:
				System.exit(0);
			}
		}
	}

	public void add() {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入员工的姓名,工号,年龄,性别:");
		String name = sc.next();
		int num = sc.nextInt();
		int age = sc.nextInt();
		String sex = sc.next();
		staff.add(new Staff(name, num, age, sex));
		System.out.println("添加成功!!!!");
	}

	public void delate() {
		System.out.println("请输入您开除的员工工号:");
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		for (int i = 0; i < staff.size(); i++) {
			if (num == staff.get(i).getNum())
				staff.set(i, new Staff(null, i, i, null));
		}
		System.out.println("可恶的资本家,开除成功!");
	}

	public void query() {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入员工工号:");
		int num = sc.nextInt();
		for (int i = 0; i < staff.size(); i++) {
			if (num == staff.get(i).getNum()) {
				System.out.println(staff.get(i));
				return;
			}
		}
		System.out.println("查无此人");
	}

	public void modify() {
		System.out.println("请输入您想修改员工的工号:");
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		for (int i = 0; i < staff.size(); i++) {
			if (num == staff.get(i).getNum()) {
				System.out.println("请输入要修改的员工信息:");
				staff.set(i, new Staff(sc.next(), sc.nextInt(), sc.nextInt(), sc.next()));
			}
		}
		System.out.println("修改成功!!!");
	}

	public void output() {
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;

		try {
			fos = new FileOutputStream("D:\\Staff.txt");
			oos = new ObjectOutputStream(fos);

			oos.writeObject(staff);

			oos.flush();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (oos != null)
					oos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (fos != null)
					fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		System.out.println("记录成功!!!");
	}

	public void input() {
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		try {
			fis = new FileInputStream("D:\\Staff.txt");
			ois = new ObjectInputStream(fis);
			ArrayList<Staff> staff = (ArrayList<Staff>) ois.readObject();

			for (int i = 0; i < staff.size(); i++) {
				System.out.print("姓名:" + staff.get(i).getName() + "  ");
				System.out.print("工号:" + staff.get(i).getNum() + "  ");
				System.out.print("年龄:" + staff.get(i).getAge() + "  ");
				System.out.println("性别:" + staff.get(i).getSex() + "  ");
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				if (ois != null)
					ois.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (fis != null)
					fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}


-------------------------------------------------------------------------------------------------


Staff类

import java.io.Serializable;

public class Staff implements Serializable {
	private String name;
	private int num;
	private int age;
	private String sex;

	public Staff(String name, int num, int age, String sex) {
		super();
		this.name = name;
		this.num = num;
		this.age = age;
		this.sex = sex;
	}

	
	public String toString() {
		return "员工信息 [姓名=" + name + ", 工号=" + num + ", 年龄=" + age + ", 性别=" + sex + "]";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值