完成系统登录程序,从命令行输入用户名密码。如果没有输入用户名密码,则提示输入用户名和密码;如果输入了用户名提示用户输入密码,然后判断用户名是否为mldn,密码是否为hello,如果正确,则提示登录成功;如果错误显示登录失败信息,用户再次输入用户名和密码,连续3次输入错误后系统退出。
对于此时的程序发现,可以将用户名和密码同时输入,也可以先输入用户名,后输入密码,如果超过3次就表示登录结束,对于用户名和密码的使用可以采用“用户名/密码”的形式完成,如果没有发现“/”表示没有输入密码。
1.定义用户的操作接口
1 package com.alibaba.demo.service; 2 3 public interface IUserService { 4 public boolean isExit(); 5 public boolean login(String username, String password); 6 }
2.定义用户操作接口的子类
1 package com.alibaba.demo.service.impl; 2 3 import com.alibaba.demo.service.IUserService; 4 5 public class UserServiceImpl implements IUserService { 6 private int count=0; 7 @Override 8 public boolean isExit() { 9 return this.count>=3; 10 } 11 @Override 12 public boolean login(String username, String password) { 13 this.count++; 14 return "mldn".equals(username) && "hello".equals(password); 15 } 16 }
3.对于用户的登录失败检测处理操作,应该单独定义一个用户的代理操作类
1 package com.alibaba.demo.service.proxy; 2 import com.alibaba.demo.service.IUserService; 3 import cn.com.sina.demo.util.InputUtil; 4 5 public class UserServiceProxy implements IUserService { 6 private IUserService userService; 7 public UserServiceProxy(IUserService userService) { 8 this.userService = userService; 9 } 10 @Override 11 public boolean isExit() { 12 return this.userService.isExit(); 13 } 14 @Override 15 public boolean login(String username,String password) { 16 while (!this.isExit()) { 17 String inputData = InputUtil.getString("请输入登录信息:"); 18 if(inputData.contains("/")) { 19 String tmp[] = inputData.split("/"); 20 if(this.userService.login(tmp[0], tmp[1])) { 21 return true; 22 }else { 23 System.out.println("登录失败,错误的用户名或密码!"); 24 } 25 }else { 26 String pwd = InputUtil.getString("请输入密码:"); 27 if(this.userService.login(inputData, pwd)) { 28 return true; 29 }else { 30 System.out.println("登录失败,错误的用户名或密码!"); 31 } 32 } 33 } 34 return false; 35 } 36 }
4.定义工厂类
1 package com.alibaba.demo.service.factory; 2 3 import com.alibaba.demo.service.IUserService; 4 import com.alibaba.demo.service.impl.UserServiceImpl; 5 import com.alibaba.demo.service.proxy.UserServiceProxy; 6 7 public class Factory { 8 private Factory() {} 9 public static IUserService getInstance() { 10 return new UserServiceProxy(new UserServiceImpl()); 11 } 12 }
5.信息输入处理类
1 package cn.com.sina.demo.util; 2 3 import java.io.BufferedReader; 4 import java.io.InputStreamReader; 5 6 public class InputUtil { 7 private static final BufferedReader INPUT = new BufferedReader(new InputStreamReader(System.in)); 8 private InputUtil() {} 9 public static String getString(String prompt) { 10 String str = null; 11 boolean flag = true; 12 while(flag) { 13 System.out.println(prompt); 14 try { 15 str = INPUT.readLine().trim(); 16 if(!"".equals(str)) { 17 flag = false; 18 }else { 19 System.out.println("输入的内容不能为空!"); 20 } 21 } catch (Exception e) { 22 System.out.println("输入的内容不能为空!"); 23 } 24 } 25 return str; 26 } 27 }
6.定义测试处理类
1 package com.alibaba.demo; 2 3 import com.alibaba.demo.service.factory.Factory; 4 5 public class IOCaseDemo { 6 public static void main(String[] args) { 7 System.out.println(Factory.getInstance().login(null, null)); 8 } 9 }