從零開始學Java之 出入門衛管理系統(一)

    這個系統包含了四塊,第一塊就是人員管理,經理分配三位分別有“ABC"權限的人,分別管理請假申請,請假審批,門衛登記管理。同時不屬於本公司的內部人員,如別的公司的人員如果想到本公司訪問,也是先通過這”ABC“權限的三個人代爲寫來訪申請,來訪審批,來訪門衛登記管理。

    具體步驟如下:

    因爲後面都會用到很多相同的方法,和相同的屬性,所以我先建立了兩個接口讓後面的方法類和屬性類都實現這兩個接口,這樣就降低了耦合度。

 屬性接口:


package com.jereh.discrepancy;

public interface Attribute {

}
   

方法類接口:

package com.jereh.discrepancy;

public interface Father {

	public abstract boolean add(Attribute at);
	
	public abstract boolean update(int num);
	
	public abstract boolean updateJu(int num);
	
	public abstract boolean delete(int num);
	
	public abstract void show();
	
	public abstract void  read();
	
	public abstract void write();
	
}



  


  首先確定人員管理:賦予ABC三個等級權利,互不干涉,各自執行各自功能。

    1.建立人員管理的屬性類:

 

<span style="font-size:18px;">public class Employeer implements java.io.Serializable{
	
	private String name;
	private String emp_no;
	private String pwd;
	private String lev;
	
	
	public Employeer(String name, String empNo, String pwd, String lev) {
		super();
		this.name = name;
		this.emp_no = empNo;
		this.pwd = pwd;
		this.lev = lev;
	}
	public Employeer() {
		
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmp_no() {
		return emp_no;
	}
	public void setEmp_no(String empNo) {
		emp_no = empNo;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getLev() {
		return lev;
	}
	public void setLev(String lev) {
		this.lev = lev;
	}
	

}</span>

2.人員管理的方法類:方法後面都有介紹介紹方法的用途

<span style="font-size:18px;">package com.jereh.discrepancy;

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

public class EmployeerBiz {
	
	static List<Employeer> list = new ArrayList<Employeer>();
	Employeer  emp       = new Employeer();
	public void as(){
		list.add(new Employeer("李忠峯","1001","1314","A"));
		list.add(new Employeer("李四","1002","1314","B"));
		list.add(new Employeer("管理員","admin","admin","T"));
		list.add(new Employeer("劉冰","1003","1314","C"));
		writeDb();
	}
	
	
	
	public boolean addEmp(Employeer em){     //添加信息,並判斷編號是否重複
		
		readDb();
		for(int i =0; i<list.size();i++){
			emp = (Employeer)list.get(i);
			if(em.getEmp_no().equals(emp.getEmp_no())){
				return false;
			}
		}
		list.add(em);
		writeDb();
		return true;
		
		
	}
	
	public boolean deleteEmp(String name){  //刪除員工的所有信息,找到刪除返回true,找不到返回
		readDb();
		for(int i =0; i<list.size();i++){
			emp = (Employeer)list.get(i);
			if(emp.getName().equals(name)){
				list.remove(i);
				writeDb();
				return true;
			}
			
		}
		return false;
	}
	public boolean updata(String name){     //判斷是否有此人
		
		readDb();
		for(int i = 0; i<list.size();i++){
			emp = (Employeer)list.get(i);
			if(emp.getName().equals(name)){
				return true;
			}
		}
		return false;
	}
	
     public Employeer updataDb(String name,String emp_no,String pwd,String lev){     //修改此人信息
		
		readDb();
		for(int i = 0; i<list.size();i++){
			emp = (Employeer)list.get(i);
			if(emp.getName().equals(name)){
			
			emp.setEmp_no(emp_no);
			emp.setLev(lev);
			emp.setPwd(pwd);
			writeDb();
			return emp;
		  }
		  
	    }
		return null;
     }
     public void showDb(){         //展示錄入的員工信息 包括:編號,名字密碼等級
    	 readDb();
    	 for(int i =0;i<list.size();i++){
    		 emp = (Employeer)list.get(i);
    		 System.out.println("\t"+emp.getName()+"\t\t"+emp.getEmp_no()+
    				 "\t\t"+emp.getPwd()+"\t\t"+emp.getLev());
    	 }
     }
     
     
     public Employeer deng(String no,String pwd){   //登錄方法,返回權限,根據權限判斷進入哪個界面
    	 readDb();
    	 for(int i =0;i<list.size();i++){
    		 emp = (Employeer)list.get(i);
    		 if(emp.getEmp_no().equals(no)&&emp.getPwd().equals(pwd)){
    			 return emp;
    		 }
    	 }
    	 return null;
     }
     
	public void readDb(){             //讀取123.txt中的文件
		
		ObjectInputStream ois = null;
		FileInputStream  fis  = null;
		File    file          = new File("E:\\h\\123\\employeer.txt");
		try {
			fis = new FileInputStream(file);
			ois = new ObjectInputStream(fis);
			list = (List)ois.readObject();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				ois.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	public  void writeDb(){                 //寫入123.txt文件中數據
		
		ObjectOutputStream oos = null;
		FileOutputStream  fos  = null;
		File    file          = new File("E:\\h\\123\\employeer.txt");
		try {
			if(!file.exists()){
				fos = new FileOutputStream(file);
				oos = new ObjectOutputStream(fos);
				oos.writeObject(list);
			}else{
				file.createNewFile();
				fos = new FileOutputStream(file);
				oos = new ObjectOutputStream(fos);
				oos.writeObject(list);
			}
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				oos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}</span>

3.人員管理的界面:


<span style="font-size:18px;">package com.jereh.discrepancy;

import java.util.*;

public class EmployeerView {

	Scanner input = new Scanner(System.in);
	EmployeerBiz ebiz = new EmployeerBiz();
	Employeer emp = new Employeer();
	String name;
	String num;
	String pwd;
	String lev;

	public void addView() {
		System.out.print("請輸入員工姓名:");
		name = input.next();
		System.out.print("請輸入員工工號:");
		num = input.next();
		System.out.print("請輸入員工密碼:");
		pwd = input.next();
		System.out.print("請輸入員工權限:");
		lev = input.next();
		emp.setEmp_no(num);
		emp.setLev(lev);
		emp.setName(name);
		emp.setPwd(pwd);
		if(ebiz.addEmp(emp)){
			System.out.println("錄入成功!");
		}else
			System.out.println("錄入失敗,該編號已經存在");
		
	}

	public void updataView() {
		System.out.println("請輸入您要修改的員工姓名:");
		name = input.next();
		if (ebiz.updata(name)) {
			System.out.print("請輸入員工工號:");
			num = input.next();
			System.out.print("請輸入員工密碼:");
			pwd = input.next();
			System.out.print("請輸入員工權限:");
			lev = input.next();
			emp = ebiz.updataDb(name,num, pwd, lev);
			System.out.println("修改成功!");
			System.out.println("*****************************************************");
			System.out.println("\t員工姓名\t\t員工編號\t\t員工密碼\t\t員工權限");
			System.out.println("\t" + emp.getName() + "\t\t" + emp.getEmp_no()
					+ "\t\t" + emp.getPwd() + "\t\t" + emp.getLev());
			System.out.println("*****************************************************");
		} else {
			System.out.println("沒找到您要修改的員工!");
		}

	}

	public void deleteView() {
		System.out.println("請輸入您要刪除的員工姓名:");
		name = input.next();
		if (ebiz.deleteEmp(name)) {
			System.out.println("刪除成功");
		} else {
			System.out.println("沒找到您要刪除的員工!");
		}

	}

	public void showView() {
		System.out.println("*****************************************************");
		System.out.println("\t員工姓名\t\t員工編號\t\t員工密碼\t\t員工權限");
		ebiz.showDb();
		 System.out.println("*****************************************************");
	}

	public  void main(){
		 EmployeerView ev = new EmployeerView();
		while(true){
		  
		   System.out.println("============員工信息=============");
		   System.out.println("\t\t1.信息錄入");
		   System.out.println("\t\t2.信息修改");
		   System.out.println("\t\t3.信息刪除");
		   System.out.println("\t\t4.信息查詢");
		   System.out.println("\t\t5.退出");
		   System.out.println("=================================");
		   System.out.print("請選擇您要進行的操作:");
		   int i = input.nextInt();
		   
			switch(i){
			case 1:
				ev.addView();
				break;
			case 2:
				ev.updataView();
				break;
			case 3:
				ev.deleteView();
				break;
			case 4:
				ev.showView();
				break;
			case 5:
				System.exit(0);
				System.out.println("退出成功!");
				break;
			}
		}
		
	}
}
</span>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章