温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java实现门禁系统

发布时间:2020-08-25 01:13:06 来源:脚本之家 阅读:257 作者:mozunx 栏目:编程语言

本文实例为大家分享了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("验证失败");
 }
 
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI