项目任务:
某公司安装了电子门,要设计门禁系统,要求如下:
公司的雇员(employee)可以凭借密码(pa)、胸卡(ca)、指纹(fi)进入公司;
计算机根据雇员的验证方式对其身份进行验证;
管理员(admin)在监控室可以通过按钮直接开启电子门;
访客(guest)可以按门铃请求进入,由管理员为其放行;
管理员可以为新职员设置密码。

项目代码结构目录:

文章目录

  • 项目代码结构目录:
      • package sys:
          • class ControlSys
          • class InputEquip
          • class Computer
          • class Door
      • package user:
          • class Guest
          • class Employee
          • class Admin
      • package test:
          • class GuestTest
          • class EmployeeTest
          • class AdminTest
      • Test运行结果

package sys:

class ControlSys
package sys;

public class ControlSys {
	private Door door; 
	private Computer computer;
	private InputEquip inputEquip;
	private boolean ring;
	private boolean openSign;
	
	public ControlSys(){
		this.ring= false;
		this.openSign = false;
		inputEquip = new InputEquip();
		computer = new Computer();
		door = new Door();
	}
	
	public void guestSetRing(){
		if (this.ring == false){
			this.ring = true;
		}else{
			this.ring = false;
		}
	}
	
	public boolean getRing(){
		return this.ring;
	}
	
	public void setOpenSign(){
		this.openSign = true;
	}
	
	public void employeeSetInfo(String inputString, String password){
		inputEquip.inputString(inputString, password);
		String string = inputEquip.outputString();
		boolean conf = computer.confirm(string);
		this.openSign = door.ifOpenDoor(conf);
	}
	
	public void work() throws InterruptedException{
		if(this.openSign == true){
			System.out.println("Door Open.");
			java.lang.Thread.sleep(5000);
			System.out.println("Door Close.");
			this.openSign = false;
		}else{
			System.out.println("Identity Verificate Failure.");
		}
	}
}

class InputEquip
package sys;

public final class InputEquip{
	private String input;
	
	InputEquip(){
		this.input = null;
	}
	
	public void inputString(String inputString, String password){
		this.input = inputString + password;
	}
	public String outputString(){
		return this.input;
	}
}

class Computer
package sys;

public final class Computer {
	private String input;
	
	Computer(){
		this.input = null;
	}
	
	private void inputFormConfirm(String inputString){
		if (inputString.substring(0, 2).equals("pa")){
			this.input = inputString.substring(2);
		}else if (inputString.substring(0, 2).equals("ca")){
			this.input = inputString.substring(2);
		}else if (inputString.substring(0, 2).equals("fi")){
			this.input = inputString.substring(2);
		}else{
			this.input = null;
		}
	}
	
	public boolean confirm(String inputString){
		inputFormConfirm(inputString);
		if (this.input == null){
			return false;
		}else{
			return true;
		}
	}
}

class Door
package sys;

public final class Door {	
	public boolean ifOpenDoor(boolean confirm){
		if (confirm == true){
			return true;
		}else{
			return false;
		}
	}
}	

package user:

class Guest
package user;
import sys.*;

public class Guest {
	public void pressRing(ControlSys controSys){
		controSys.guestSetRing();
	}
}

class Employee
package user;
import sys.*;

public class Employee extends Guest{
	private String info;
	protected String password;
	
	public Employee(String _info, String _password){
		this.info = _info;
		this.password = _password;
	}
	
	public void InputInfo(ControlSys controlSys){
		controlSys.employeeSetInfo(this.info, this.password);
	}
}

class Admin
package user;
import sys.*;

public class Admin extends Employee{
	public Admin(String _info, String _password){
		super(_info, _password);
	}
	
	public void setEmployeePassword(String newPassword){
		super.password = newPassword;
	}
	
	public void workDoor(ControlSys controlSys, Guest guest){
		if (controlSys.getRing()){
			controlSys.setOpenSign();
			controlSys.guestSetRing();
		}
	}
}

package test:

class GuestTest
package test;
import sys.*;
import user.*;

public class GuestTest {
	public static void main(String args[]){
		Guest guest = new Guest();
		Admin admin = new Admin("pa", "12345");
		ControlSys sys = new ControlSys();
		System.out.println("门禁系统_管理员测试");
		
		guest.pressRing(sys);
		admin.workDoor(sys, guest);
		try{
			sys.work();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

class EmployeeTest
package test;
import sys.*;
import user.*;

public class EmployeeTest {
	public static void main(String[] args) {
		Employee employee = new Employee("pa", "12345"); // 雇员
		ControlSys sys = new ControlSys(); // 门禁系统
		System.out.println("门禁系统_雇员测试");
		
		employee.InputInfo(sys);
		try{
			sys.work();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

class AdminTest
package test;
import sys.*;
import user.*;

public class AdminTest {
	public static void main(String[] args) {
		Admin admin = new Admin("pa", "12345"); // 管理员
		ControlSys sys = new ControlSys(); // 门禁系统
		System.out.println("门禁系统_管理员测试");
		
		admin.InputInfo(sys);
		try{
			sys.work();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

Test运行结果

GuestTest:
	门禁系统_访客测试
	Door Open.
	Door Close.

EmployeeTest:
	门禁系统_雇员测试
	Door Open.
	Door Close.

AdminTest:
	门禁系统_管理员测试
	Door Open.
	Door Close.

更多推荐

Java入门典题门禁系统项目