JAVASE 用集合实现员工管理系统

JAVASE
用集合实现员工管理系统
要求:
实现基本的增删改查

1.实体类:Employee

public class Employee {
	private long id;
	private String ename;
	private double sal;
	private String dept;
	private String job;

	//无参构造方法
	public Employee() {
	}
    //有参构造
	public Employee(long id, String ename, double sal, String dept, String job) {
		this.id = id;
		this.ename = ename;
		this.sal = sal;
		this.dept = dept;
		this.job = job;
	}

	public long getId() {
		return id;
	}

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

	public String getEname() {
		return ename;
	}

	public void setEname(String ename) {
		this.ename = ename;
	}

	public double getSal() {
		return sal;
	}

	public void setSal(double sal) {
		this.sal = sal;
	}

	public String getDept() {
		return dept;
	}

	public void setDept(String dept) {
		this.dept = dept;
	}

	public String getJob() {
		return job;
	}

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

	@Override
	public String toString() {
		return "Employee{" +
				"id=" + id +
				", ename='" + ename + '\'' +
				", sal=" + sal +
				", dept='" + dept + '\'' +
				", job='" + job + '\'' +
				'}';
	}
}

2.业务类:EmployeeService

public class EmployeeService {
	private List<Employee> employees;
	private Scanner input=new Scanner(System.in);
	public EmployeeService() {
		employees=new ArrayList<Employee>();
		//准备数据
		Employee emp1=new Employee(1, "张三", 8000, "研发部", "职员");
		Employee emp2=new Employee(2, "张三丰", 10000, "研发部", "经理");
		Employee emp3=new Employee(3, "张杰", 7000, "设计部", "主管");
		Employee emp4=new Employee(4, "张靓颖", 7500, "研发部", "Java开发");
		Employee emp5=new Employee(5, "张萌", 8000, "后勤部", "部长");
		Employee emp6=new Employee(6, "张大大", 8000, "研发部", "职员");
		Employee emp7=new Employee(7, "张飞", 9000, "研发部", "职员");
		Employee emp8=new Employee(8, "张若昀", 6000, "研发部", "职员");
		Employee emp9=new Employee(9, "张伟", 7000, "研发部", "职员");
		employees.add(emp1);
		employees.add(emp2);
		employees.add(emp3);
		employees.add(emp4);
		employees.add(emp5);
		employees.add(emp6);
		employees.add(emp7);
		employees.add(emp8);
		employees.add(emp9)}
     //菜单方法
	public void printMenu() {
		System.out.println("********欢迎进入员工管理系统********");
		System.out.println("1.浏览");
		System.out.println("2.查找");
		System.out.println("3.新增");
		System.out.println("4.修改");
		System.out.println("5.离职");
		System.out.println("6.退出");
		System.out.println("请选择...");
	}
   private void title() {
		System.out.println("姓名\t薪资\t部门\t职位");
	}
	/**
	 * 浏览所有
	 */
	public void findAll() {
		// TODO Auto-generated method stub
		//遍历集合
		if(employees!=null&&employees.size()>0) {
			title();
			for (Employee employee : employees) {
				System.out.println(employee);
			}
		}
		
	}
	/**
	 * 按名字查询
	 * @param name
	 * @return
	 */
	private List<Employee>  getEmployeesByName(String name) {
		List<Employee> list=new ArrayList<Employee>();
		if(employees!=null&&employees.size()>0) {
			for(int i=0;i<employees.size();i++) {
				if(name.equals(employees.get(i).getEname())) {
					list.add(employees.get(i));
				}
			}
			return list;
		}else {
			return null;
		}
	}
	
	public void findByName() {
		System.out.println("请输入员工姓名");
		String name=input.next();
		List<Employee> list=getEmployeesByName(name);
		if(list!=null&&list.size()>0) {
			title();
			for (Employee employee : list) {
				System.out.println(employee);
			}
		}else {
			System.out.println("没有该员工");
		}
	}
	/**
	 * 添加员工
	 */
	public void insertEmployee() {
		System.out.println("请输入id:");
		long id=input.nextLong();
		input.next();
		
		System.out.println("请输入姓名:");
		String ename=input.next();
		System.out.println("请输入薪资:");
		double sal=input.nextDouble();
		input.next();
		
		System.out.println("请输入部门:");
		String dept=input.next();
		
		System.out.println("请输入职位:");
		String job=input.next();
		Employee emp=new Employee(id, ename, sal, dept, job);
		employees.add(emp);
		System.out.println("新增成功!!");
	}
	//根据编号修改
	private Employee getById(long id) {
		if(employees!=null&&employees.size()>0) {
			for(int i=0;i<employees.size();i++) {
				if(id==employees.get(i).getId()) {
					return employees.get(i);
				}
			}
		}
		System.out.println("没有该员工");
		return null;
	} 
	public void updateEmp() {
		System.out.println("请输入id:");
		long id=input.nextLong();
		//调用getById方法
		Employee emp=getById(id);
		if(emp!=null) {
			//修改信息
			System.out.println("请输入新的薪资:");
			emp.setSal(input.nextDouble());
			System.out.println("请输入新的部门:");
			emp.setDept(input.next());
			System.out.println("请输入新的职位");
			emp.setJob(input.next());
			System.out.println("修改成功");
		}
	}
	/**
	 * 删除
	 */
	public void delEmp() {
		System.out.println("请输入id:");
		long id=input.nextLong();
		if(employees!=null&&employees.size()>0) {
			for(int i=0;i<employees.size();i++) {
				if(id==employees.get(i).getId()) {
					//找到了   可以删除
					employees.remove(i);
					return ;
				}
			}
		}
	}

3.测试类:Client

public class Client {
	public static void main(String[] args) {
		EmployeeService service=new EmployeeService();
		Scanner input=new Scanner(System.in);
		boolean flag=true;
		while(flag) {
			service.printMenu();
			int op=input.nextInt();
			switch (op) {
			case 1:
				service.findAll();
				break;
			case 2:
				service.findByName();
				break;
			case 3:
				service.insertEmployee();
				break;
			case 4:
				service.updateEmp();
				break;
			case 5:
				service.delEmp();
				break;
				
			case 6:
				System.out.println("确认退出?Y/N");
				String result=input.next();
				if("Y".equalsIgnoreCase(result)) {
					flag=false;
				}
				break;
			default:
				System.out.println("请重新选择!!");
				break;
			}
		}
		
		System.out.println("谢谢使用!");
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值