1. 程式人生 > >java 簡單員工管理系統

java 簡單員工管理系統

package cn.test;

import java.io.*;
import java.util.*;

class Emp {
	@Override
	public String toString() {
		return "Emp [empNo=" + empNo + ", name=" + name + ", salary=" + salary + "]";
	}

	private String empNo;
	private String name;
	private float salary;

	public Emp(String empNo, String name, float salary) {
		this.empNo = empNo;
		this.name = name;
		this.salary = salary;
	}

	public String getEmpNo() {
		return empNo;
	}

	public void setEmpNo(String empNo) {
		this.empNo = empNo;
	}

	public String getName() {
		return name;
	}

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

	public float getSalary() {
		return salary;
	}

	public void setSalary(float salary) {
		this.salary = salary;
	}
}

//僱員管理類
class EmpManage {
	private ArrayList al = null;

	// 構造
	EmpManage() {
		al = new ArrayList();
	}

	// 新增員工
	public void AddEmp(Emp emp) {
		al.add(emp);
	}

	// 刪除員工
	public void removeEmp(String empNo) {
		for (int i = 0; i < al.size(); i++) {
			Emp emp = (Emp) al.get(i);
			if (emp.getEmpNo().equals(empNo)) {
				al.remove(emp);
			}
		}
	}

	// 顯示員工資訊
	public void showEmpInfo(String empNo) {
		for (int i = 0; i < al.size(); i++) {
			Emp emp = (Emp) al.get(i);
			// java 中字串雙等是比較地址等不等,因為String也是個引用型別
			if (emp.getEmpNo().equals(empNo)) {
				System.out.println("get emp info" + emp.toString());
			}
		}

	}

	// 修改工資
	public void updateEmpSalary(String empNo, float newSalary) {
		for (int i = 0; i < al.size(); i++) {
			Emp emp = (Emp) al.get(i);
			if (emp.getEmpNo().equals(empNo)) {
				emp.setSalary(newSalary);
			}
		}
	}

}

public class TestMain {
	public static void main(String[] str) throws Exception {
		EmpManage empManager = new EmpManage();

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		while (true) {
			System.out.println("請選擇你要進行的操作");
			System.out.println("1.新增員工僱員");
			System.out.println("2.查詢僱員");
			System.out.println("3.修改僱員工資");
			System.out.println("4.刪除僱員");
			System.out.println("5.退出系統");
			String type = br.readLine();
			String empNo;
			String name;
			float salary;
			switch (Integer.parseInt(type)) {
			case 1:
				System.out.println("請輸入員工編號");
				empNo = br.readLine();
				System.out.println("請輸入員工姓名");
				name = br.readLine();
				System.out.println("請輸入員工工資");
				salary = Float.parseFloat(br.readLine());

				Emp emp = new Emp(empNo, name, salary);
				empManager.AddEmp(emp);
				break;
			case 2:
				System.out.println("請輸入員工編號");
				empNo = br.readLine();
				empManager.showEmpInfo(empNo);
				break;
			case 3:
				System.out.println("請輸入員工編號");
				empNo = br.readLine();
				System.out.println("請輸入員工工資");
				salary = Float.parseFloat(br.readLine());
				empManager.updateEmpSalary(empNo, salary);
				break;
			case 4:
				System.out.println("請輸入員工編號");
				empNo = br.readLine();
				empManager.removeEmp(empNo);
				break;
			case 5:
				System.out.println("退出系統");
				System.exit(0);;
			default:
				break;
			}
		}
	}
}