简易门禁系统

一.监测

 

 1 public class Check implements Validate
 2 {
 3     //预先在常量池储存密码
 4     private static final String[]password= {"3625","299","zjm"};
 5     int count=0;
 6     @Override
 7     //密码对比
 8     public boolean check(Staff staff)
 9      {
10         /*
11          * String有final性质,当录入员工密码会到常量池寻找对比
12          */
13         String string=staff.input();//录入员工输入的密码
14         for(String str:password)//将常量池的字符串数组遍历给str
15         {
16             if(str.equals(string))
17             {
18                 count++;
19             }
20         }
21         
22         if(0!=count)
23             return true;        
24         else        
25             return false;
26      }
27 
28 }

 

二.开关门的功能

 

 1 public class Door
 2 {   
 3     //开门功能
 4     public void openDoor()
 5     {
 6         System.out.println("门开了!");
 7     }
 8     //关门功能
 9     public void closeDoor()
10     {
11         System.out.println("门关了!");
12     }
13 }

 

三. 员工实例

 1 /*
 2  * 员工实例
 3  */
 4 public class Jason extends Staff
 5 {     //有参构造:构造函数就是用来初始化属性值得,不可在主函数中初始化
 6       public Jason()
 7       {
 8           this.name="巫妖果子!";
 9           this.gender="男";
10           this.department="直男癌部门";
11           this.position="软件工程开发师";
12       }
13 
14     @Override
15     public String input() 
16     {
17         System.out.println("请输入密码:");
18         //按钮获取
19         Scanner sc=new Scanner(System.in);
20         String input=sc.next();//String类型接收
21         return input;
22     }
23       
24 }

四.打印员工信息

 

 1 public class Output
 2 {
 3    //有参构造
 4     public Output(Staff staff)//将类对象做参数
 5     {
 6         System.out.println("**************");
 7         System.out.println("姓名:"+staff.name);
 8         System.out.println("性别:"+staff.gender);
 9         System.out.println("所属部门:"+staff.department);
10         System.out.println("岗位:"+staff.position);
11         System.out.println("**************");
12     }
13 }

 

五.

 

 1 /*
 2  * 员工类
 3  */
 4 public class Staff 
 5 {
 6    public String name;//姓名
 7    public String gender;//性别
 8    public  String department;//所属部门
 9    public String position;//岗位
10    //输入密码
11    public String input()
12    {
13        return "1";
14    }
15 }

 

六.模拟验证

1 /*
2  * 密码验证接口
3  */
4 public interface Validate
5 {
6    //抽象方法
7     public abstract boolean check(Staff member); 
8 }

七. 监测

 1 public class TestTemp
 2 {
 3    public static void main(String[] args) throws InterruptedException 
 4    {
 5     Door door=new Door();//创建门对象
 6     //父类引用指向子类对象:意味着可以调用Jason员工信息
 7     Staff staff=new Jason();//创建员工对象
 8     Check check=new Check();//创建检测系统对象
 9     boolean result=check.check(staff);//获得检测结果
10     if(result)//密码核对真确
11     {
12         door.openDoor();//打开门
13         Output output=new Output(staff);//调用构造方法打印员工信息
14         Thread.sleep(3000);//调用静态方法,设置开门时间
15         door.closeDoor();//超过开门时间,关闭门
16     }
17     else
18     {
19         System.out.println("输入密码有误,请重新输入!");
20         System.exit(0);
21     }
22    }
23 }

      

 

posted @ 2018-12-13 20:26  静心*尽力  阅读(335)  评论(0编辑  收藏  举报