您好,登錄后才能下訂單哦!
本文實例為大家分享了java實現門禁系統的具體代碼,供大家參考,具體內容如下
Door.java
package com.handson.sys; public class Door { public final static String OPEN = "門已開啟"; public final static String CLOSE = "門已關閉"; private String state = CLOSE; // 電子門狀態 private boolean bellState = false; // 門鈴狀態 // 構造方法 public Door() { this.setState(CLOSE); } // 開啟電子門 public void open() { this.setState(OPEN); } // 關閉電子門 public void close() { this.setState(CLOSE); } // setter/getter public String getState() { return state; } public void setState(String state) { this.state = state; } public boolean isBellState() { return bellState; } public void setBellState(boolean bellState) { this.bellState = bellState; } }
Computer.java
package com.handson.sys; import java.util.ArrayList; import java.util.HashMap; import java.util.Scanner; import com.handson.validate.CardVId; import com.handson.validate.FingVId; import com.handson.validate.PassWordVId; import com.handson.validate.Validate; public class Computer { HashMap Card = new HashMap(); ArrayList Fing = new ArrayList(); String[] Pass = new String[10]; public Computer() { Card.put(0, "car000"); Card.put(1, "car001"); Card.put(2, "car002"); Fing.add("fing000"); Fing.add("fing001"); Fing.add("fing002"); Pass[0] = "pas000"; Pass[1] = "pas001"; Pass[2] = "pas002"; Pass[3] = ""; } public boolean validate(String imputMsg) { boolean ban = false; Scanner in = new Scanner(System.in); System.out.println("請選擇驗證方式:1-卡號 2-指紋 3-密碼"); int n = in.nextInt(); Validate val; switch (n) { case 1: val = new CardVId(); ban = val.check(Card, imputMsg); break; case 2: val = new FingVId(); ban = val.check(Fing, imputMsg); break; case 3: val = new PassWordVId(); ban = val.check(Pass, imputMsg); break; default: System.out.println("選擇錯誤!"); } return ban; } }
Amind.java
package com.handson.user; import java.util.Scanner; import com.handson.sys.Door; public class Admin extends Employee { // 構造方法 public Admin() { super(); } public Admin(String name) { super(name); } // 管理員的工作方法1,通過控制室的按鈕,開啟電子門 public boolean work(Door door) { boolean bn = false; Scanner scanner = new Scanner(System.in); System.out.print("管理員(" + this.getName() + ")按開門按鈕?[y/n]:"); String btn = scanner.nextLine(); if (btn.equalsIgnoreCase("y")) { System.out.println("管理員(" + this.getName() + ")按下開門按鈕"); bn = true; } else { System.out.println("---管理員(" + this.getName() + ")拒絕開門"); } return bn; } // 管理員的工作方法2,監聽門鈴 public boolean work(Door door, Guest guest) { boolean bn = false; if (door.isBellState()) { // 門鈴響 bn = this.work(door); door.setBellState(false); } return bn; } }
Employee.java
package com.handson.user; import java.util.Scanner; import com.handson.sys.Door; public class Employee extends Guest { //構造方法 public Employee() { super(); } public Employee(String name) { super(name); } //輸入身份信息 public String input() { Scanner scanner = new Scanner(System.in); System.out.print("身份驗證:" ); String passStr = scanner.nextLine(); return passStr; } }
Guest.java
package com.handson.user; import java.util.Scanner; import com.handson.sys.Door; public class Guest { private String name; // 姓名 // 構造方法 public Guest() { } public Guest(String name) { this.name = name; } // 訪客按門鈴 public void pressRing(Door door) { Scanner scanner = new Scanner(System.in); System.out.print("訪客(" + this.getName() + ")按門鈴?[y/n]:"); String btn = scanner.nextLine(); if (btn.equals("y")) { door.setBellState(true); // 門鈴響起 System.out.println("訪客(" + this.getName() + ")按下門鈴"); } else if (btn.equals("n")) { return; } } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Validate.java
package com.handson.validate; public interface Validate { public abstract boolean check(Object ob, String inputMsg); } CardVId.java package com.handson.validate; import java.util.HashMap; public class CardVId implements Validate { public boolean check(Object ob, String inputMsg) { boolean ban = false; HashMap srContent = (HashMap) ob; for (int i = 0; i < srContent.size(); i++) { if (srContent.get(i).equals(inputMsg)) { ban = true; break; } } return ban; } }
FingVId.java
package com.handson.validate; import java.util.ArrayList; public class FingVId implements Validate { public boolean check(Object ob, String inputMsg) { boolean ban = false; ArrayList srContent = (ArrayList) ob; for (int i = 0; i < srContent.size(); i++) { if (srContent.get(i).equals(inputMsg)) { ban = true; break; } } return ban; } }
PasswordVId.java
package com.handson.validate; public class PassWordVId implements Validate { @Override public boolean check(Object ob, String inputMsg) { boolean ban = false; String[] srContent = (String[]) ob; for (int i = 0; i < srContent.length; i++) { if (srContent[i].equals("")) { break; } else { if (srContent[i].equals(inputMsg)) ban = true; } } return ban; } }
Test.java
package com.handson.test; import java.util.Scanner; import com.handson.sys.Computer; import com.handson.sys.Door; import com.handson.user.Admin; import com.handson.user.Guest; public class Test { public static void main(String[] args) { System.out.println("門禁系統的使用方法\n" + " 1.模擬輸入密碼:以“pa”開頭,后跟密碼\n" + " 2.模擬刷卡:以“ca”開頭,后跟卡號\n" + " 3.模擬取指紋:以“fi”開頭,后跟表示指紋的字符串\n" + " 4.模擬管理員按下開門按鈕:輸入“y”"); System.out .println("***************************************************"); System.out.println("門禁系統啟動"); System.out.println("門禁系統---訪客測試"); System.out .println("***************************************************"); Scanner in = new Scanner(System.in); Door door = new Door(); boolean ban = true; boolean bn = false; Computer coputer = new Computer(); Admin admin = new Admin("王石頭"); while (ban) { System.out.println("請選擇扮演的角色:1.來客 2.管理員 3.員工 其它:結束"); String n = in.next(); switch (Integer.parseInt(n)) { case 1: Guest guest = new Guest("趙強"); guest.pressRing(door); // 按門鈴 bn = admin.work(door, guest); // 管理員監聽門鈴 /* 檢查開門信號(監聽門鈴,響) */ break; case 2: case 3: System.out.println("請輸入驗證信息:"); bn = coputer.validate(in.next()); break; default: ban = false; } if (bn) { door.open(); // 開啟電子門 System.out.println("-------^_^管理員 " + admin.getName() + " 開啟了電子門"); /* 電子門開啟狀態將維持5秒后關閉 */ try { Thread.sleep(5000); // 讓電子門開啟狀態維持5秒 } catch (InterruptedException e) { System.out.println(e.getMessage()); } door.close(); // 關閉電子門 System.out.println("---電子門5秒后自動關閉---"); } else System.out.println("驗證失敗"); } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。