设计题目:门禁管理系统(DOS)设计目的:某公司安装了电子门,要设计门禁系统。设计要求:(1) 公司的雇员可以凭借密码、卡号和指纹进入公司(2) 计算机根据雇员的验证方式对其身份进行验证(3) 管理员在监控室可以通过按钮直接开启电子门 (4) 访客可以按门铃请求进入,由管理员为其放行。按照上述要求用Java语言规范化编写代码
时间: 2023-12-31 10:03:44 浏览: 60
以下是门禁管理系统(DOS)的Java代码实现,其中包括员工类、管理员类、访客类和门禁系统类:
```java
import java.util.HashMap;
import java.util.Scanner;
// 员工类
class Employee {
private String name;
private String password;
private String cardNumber;
private String fingerprint;
public Employee(String name, String password, String cardNumber, String fingerprint) {
this.name = name;
this.password = password;
this.cardNumber = cardNumber;
this.fingerprint = fingerprint;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public String getCardNumber() {
return cardNumber;
}
public String getFingerprint() {
return fingerprint;
}
}
// 管理员类
class Admin {
private String password;
public Admin(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
}
// 访客类
class Visitor {
private String name;
public Visitor(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
// 门禁系统类
class AccessControlSystem {
private HashMap<String, Employee> employees = new HashMap<>();
private Admin admin;
private Visitor visitor;
public void addEmployee(Employee employee) {
employees.put(employee.getCardNumber(), employee);
}
public void setAdmin(Admin admin) {
this.admin = admin;
}
public void setVisitor(Visitor visitor) {
this.visitor = visitor;
}
public void access(String verificationMethod) {
if (verificationMethod.equals("password")) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入密码:");
String password = scanner.next();
for (Employee employee : employees.values()) {
if (employee.getPassword().equals(password)) {
System.out.println("欢迎回来," + employee.getName() + "!");
return;
}
}
System.out.println("密码错误,无法进入公司!");
} else if (verificationMethod.equals("cardNumber")) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入卡号:");
String cardNumber = scanner.next();
if (employees.containsKey(cardNumber)) {
System.out.println("欢迎回来," + employees.get(cardNumber).getName() + "!");
} else {
System.out.println("卡号错误,无法进入公司!");
}
} else if (verificationMethod.equals("fingerprint")) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入指纹:");
String fingerprint = scanner.next();
for (Employee employee : employees.values()) {
if (employee.getFingerprint().equals(fingerprint)) {
System.out.println("欢迎回来," + employee.getName() + "!");
return;
}
}
System.out.println("指纹错误,无法进入公司!");
} else {
System.out.println("验证方式错误!");
}
}
public void openDoor() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入管理员密码:");
String password = scanner.next();
if (admin.getPassword().equals(password)) {
System.out.println("管理员已开启电子门!");
} else {
System.out.println("管理员密码错误,无法开启电子门!");
}
}
public void letVisitorIn() {
System.out.println("访客已进入公司!");
}
}
// 测试代码
public class Main {
public static void main(String[] args) {
AccessControlSystem accessControlSystem = new AccessControlSystem();
Employee employee1 = new Employee("张三", "123456", "0001", "001");
Employee employee2 = new Employee("李四", "654321", "0002", "002");
accessControlSystem.addEmployee(employee1);
accessControlSystem.addEmployee(employee2);
Admin admin = new Admin("admin");
accessControlSystem.setAdmin(admin);
Visitor visitor = new Visitor("王五");
accessControlSystem.setVisitor(visitor);
System.out.println("请选择验证方式:1.密码 2.卡号 3.指纹");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
switch (choice) {
case 1:
accessControlSystem.access("password");
break;
case 2:
accessControlSystem.access("cardNumber");
break;
case 3:
accessControlSystem.access("fingerprint");
break;
default:
System.out.println("输入错误!");
}
System.out.println("管理员是否开启电子门?1.是 2.否");
int choice2 = scanner.nextInt();
if (choice2 == 1) {
accessControlSystem.openDoor();
}
System.out.println("是否有访客请求进入?1.是 2.否");
int choice3 = scanner.nextInt();
if (choice3 == 1) {
accessControlSystem.letVisitorIn();
}
}
}
```
这是一个基本的门禁管理系统,可以根据不同的验证方式对员工进行身份验证,管理员可以通过密码开启电子门,访客需要管理员放行才能进入公司。注意,这个系统还有很多可以改进的地方,比如可以增加数据库存储员工信息、管理员密码加密等功能,读者可以根据自己的实际情况进行改进。
阅读全文