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 String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void pressRing(Door door){
Scanner in = new Scanner(System.in);
System.out.println("访客"+getName()+"按下门铃吗?"+"y/n");
String press = in.next();
if(press.equalsIgnoreCase("y")){
System.out.println("访客"+getName()+"按下门铃了!!!");
door.setBellState(true);
}
}
}
package com.handson.user;
import java.util.Scanner;
public class Employee extends Guest {
public Employee(){
}
public Employee(String name){
super(name);
}
public String input(){
System.out.println("请输入验证信息:");
Scanner in = new Scanner(System.in);
String information = in.next();
return information;
}
}
package com.handson.user;
import java.util.Scanner;
import com.handson.sys.Door;
public class Admin extends Employee {
public Admin() {
}
public Admin(String name) {
super(name);
}
public boolean work(Door door) {
boolean isOpenDoor = false;
if (door.isBellState()) {
System.out.println("管理员" + getName() + "是否开门?y/n");
Scanner in = new Scanner(System.in);
String input = in.next();
if (input.equalsIgnoreCase("y")) {
door.open();
System.out.println(door.getDoorState());
door.setBellState(false);
isOpenDoor = true;
} else {
System.out.println("管理员拒绝开门!");
}
}
return isOpenDoor;
}
public boolean work(Door door, Guest guest) {
boolean isOpenDoor = false;
isOpenDoor = work(door);
return isOpenDoor;
}
}
package com.handson.sys;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import com.handson.validate.CardVld;
import com.handson.validate.FingerVld;
import com.handson.validate.PasswordVld;
public class Computer {
private boolean flag = false;
String[] srcContent = new String[100];
ArrayList list = new ArrayList();
HashMap map = new HashMap();
public Computer(){
srcContent [0]="100001";
list.add("100002");
map.put("1","100003");
}
public boolean validate(String inputMsg){
System.out.println("请选择验证方式:");
System.out.println("1.卡号验证 2.密码验证 3.指纹验证");
Scanner in = new Scanner(System.in);
int choose = in.nextInt();
switch(choose){
case 1:
CardVld cardNumber = new CardVld();
flag=cardNumber.check(srcContent, inputMsg);
break;
case 2:
PasswordVld password = new PasswordVld();
flag = password.check(list, inputMsg);
break;
case 3:
FingerVld finger = new FingerVld();
flag = finger.check(map, inputMsg);
break;
default:
System.out.println("输入有误!");
}
return flag;
}
}
package com.handson.sys;
public class Door {
private String doorState;//门的状态
private boolean bellState;//门铃状态
private static String OPEN = "门已开启.";
private static String ClOSE = "门已关闭.";
public Door(){
}
public Door(String doorState,boolean bellState){
this.doorState = doorState;
this.bellState = bellState;
}
public boolean isBellState() {
return bellState;
}
public void setBellState(boolean bellState) {
this.bellState = bellState;
}
public String getDoorState() {
return doorState;
}
public void open(){
doorState = OPEN;
}
public void close(){
doorState = ClOSE;
}
}
package com.handson.validate;
public interface Validate {
boolean check(Object obj,String inputMsg);
}
package com.handson.validate;
public class CardVld implements Validate {
private boolean flag = false;
public boolean check(Object obj,String inputMsg) {
String []srcContent = (String [])obj;
for(int i=0;i<srcContent.length;i++){
if(srcContent[i].equals(inputMsg)){
flag = true;
break;
}
}
return flag;
}
}
package com.handson.validate;
import java.util.HashMap;
public class FingerVld implements Validate{
private boolean flag = false;
@Override
public boolean check(Object obj, String inputMsg) {
HashMap map = (HashMap)obj;
for(int i=1;i<map.size();i++){
if(map.get(i).equals(inputMsg)){
flag = true;
break;
}
}
return flag;
}
}
package com.handson.validate;
import java.util.ArrayList;
public class PasswordVld implements Validate{
private boolean flag = false;
@Override
public boolean check(Object obj, String inputMsg) {
ArrayList list = (ArrayList)obj;
for(int i=0;i<list.size();i++){
if(list.get(i).equals(inputMsg)){
flag = true;
break;
}
}
return flag;
}
}
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.Employee;
import com.handson.user.Guest;
public class Test {
public static void main(String[] args) {
int choose = 0;
Door door = new Door();
while (choose != 4) {
System.out.println("请选择身份:1--访客.2--雇员.3--管理员.4--退出");
Scanner in = new Scanner(System.in);
choose = in.nextInt();
switch (choose) {
case 1:
Guest guest = new Guest("G");
guest.pressRing(door);
Admin admin = new Admin("TianChengLin");
admin.work(door, guest);
break;
case 2:
Employee employee = new Employee("E");
String inputMsg = employee.input();
Computer PC = new Computer();
if (PC.validate(inputMsg)) {
door.open();
System.out.println(door.getDoorState());
} else {
System.out.println("信息不匹配,开门失败!");
}
break;
case 3:
Admin ad = new Admin("TCL");
door.setBellState(true);
ad.work(door);
break;
case 4:
System.exit(0);
default:
System.out.println("输入有误!");
}
}
}
}
运行结果:
心得体会:简单的实现了大体功能,验证的方面都是通过输入实现,通过这个项目的作业充分了解了JAVA各种类之间继承组合使用问题。