本文实例为大家分享了java实现门禁系统的具体代码,供大家参考,具体内容如下
door.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
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( "验证失败" ); } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/mozunx/article/details/41699429