当前位置:   article > 正文

JavaSE----模拟用户登录注册_模拟登录,编写用户类,测试类

模拟登录,编写用户类,测试类

目录

一、String类模拟用户登录

需求

分析

代码实现

猜数字类

模拟登录类

效果

二、模拟用户登录注册集合版

分析

代码实现

用户基本描述类

用户操作类接口

用户操作实现类

测试类

效果

三、模拟用户登录注册IO版

分析

代码实现

IO流实现用户操作接口


一、String类模拟用户登录

  • 需求

        A:模拟用户登录,给3次机会,并提示还有几次;
        B:登录成功后,可选择是否玩猜数字小游戏;

  • 分析

        A:定义用户名和密码(假设已经存在);
        B:键盘录入用户名和密码;
        C:比较A,B中的用户名密码,若匹配则登录成功,并选择是否开始游戏,否则登录失败;
        D:给3次机会,用循环改进;

  • 代码实现

  • 猜数字类

  1. package String_Realize;
  2. import java.util.Scanner;
  3. /*
  4. * 需求:实现猜数字小游戏
  5. *
  6. * 分析:
  7. * A:由系统随机给定数字,范围是1-100;
  8. * B:键盘录入要猜的数字,给10次机会;
  9. */
  10. public class GuessNumber {
  11. // 猜数字方法
  12. public static void start() {
  13. // 记录系统随机给出的数字
  14. int number = (int) (Math.random() * 100 + 1);
  15. // 键盘录入要猜的数字,给10次机会
  16. Scanner sc = new Scanner(System.in);
  17. for (int i = 0; i < 10; i++) {
  18. System.out.println("请输入你猜的数字(1-100):");
  19. int guess = sc.nextInt();
  20. if (guess == number) {
  21. System.out.println("恭喜你第" + (i + 1) + "次猜中了!");
  22. break;
  23. } else {
  24. if (i == 9) {
  25. System.out.println("珍爱生命,告别猜谜!");
  26. break;
  27. }
  28. if (guess > number) {
  29. System.out.println("你猜的数字大了!,还有" + (9 - i) + "次机会");
  30. } else if (guess < number) {
  31. System.out.println("你猜的数字小了!,还有" + (9 - i) + "次机会");
  32. }
  33. }
  34. }
  35. }
  36. }
  • 模拟登录类

  1. package String_Realize;
  2. import java.util.Scanner;
  3. /*
  4. * 需求:
  5. * A:模拟用户登录,给3次机会,并提示还有几次;
  6. * B:登录成功后,可选择是否玩猜数字小游戏;
  7. * 分析:
  8. * A:定义用户名和密码(假设已经存在);
  9. * B:键盘录入用户名和密码;
  10. * C:比较A,B中的用户名密码,若匹配则登录成功,否则登录失败
  11. * D:给3次机会,用循环改进;
  12. */
  13. public class StringTest {
  14. public static void main(String[] args) {
  15. // 定义用户名和密码
  16. String username = "admin";
  17. String password = "admin";
  18. // 键盘录入用户名和密码
  19. Scanner sc = new Scanner(System.in);
  20. // 比较用户名和密码,只给3次机会
  21. for (int i = 0; i < 3; i++) {
  22. System.out.println("请输入用户名:");
  23. String name = sc.nextLine();
  24. System.out.println("请输入密码:");
  25. String words = sc.nextLine();
  26. if (name.equals(username) && words.equals(password)) {
  27. System.out.println("登录成功!是否进行猜数字小游戏?(Y/N)");
  28. while (true) {
  29. String choice = sc.nextLine();
  30. if (choice.equals("Y") || choice.equals("y")) {
  31. GuessNumber.start();
  32. System.out.println("你还要玩吗?(Y/N)");
  33. } else {
  34. break;
  35. }
  36. }
  37. break;
  38. } else {
  39. if (i == 2) {
  40. System.out.println("账号已冻结!");
  41. break;
  42. }
  43. System.out.println("用户名或密码错误,你还有" + (2 - i) + "次机会!");
  44. }
  45. }
  46. }
  47. }
  • 效果

二、模拟用户登录注册集合版

  • 分析

       A:有哪些类呢?
             用户类
             测试类
       B:每个类有哪些东西呢?
           a:用户类:
                 成员变量:用户名 ,密码
                 构造方法:无参构造
                 成员方法:getXxx()/setXxx()
                                登录、注册
           b:测试类:main方法    
           c:为了更清晰的分类,把用户又划分成两类
                 用户基本描述类
                      成员变量:用户名 ,密码
                      构造方法:无参构造
                      成员方法:getXxx()/setXxx()
                 用户操作类
                      登录、注册
       C:类与类之间的关系是什么呢?
          在测试类中创建用户操作类和基本描述类的对象,并使用其功能;
       D:分包按照功能划分
           a:用户基本描述类包cn.itcast.pojo
           b:用户操作接口cn.itcast.dao
           c:用户操作类包cn.itcast.dao.impl
           d:用户测试类cn.itcast.test

注意:由于猜数字类已在上方给出,下面将不再给出;

  • 代码实现

  • 用户基本描述类

  1. package cn.itcast.pojo;
  2. /**
  3. * 这是用户基本描述类
  4. *
  5. * @author Administrator
  6. * @version V1.0
  7. */
  8. public class User {
  9. // 用户名和密码
  10. private String username;
  11. private String password;
  12. // 无参构造
  13. public User() {
  14. }
  15. // 成员方法
  16. public String getUsername() {
  17. return username;
  18. }
  19. public void setUsername(String username) {
  20. this.username = username;
  21. }
  22. public String getPassword() {
  23. return password;
  24. }
  25. public void setPassword(String password) {
  26. this.password = password;
  27. }
  28. }
  • 用户操作类接口

  1. package cn.itcast.dao;
  2. import cn.itcast.pojo.User;
  3. /**
  4. * 这是用户操作类接口
  5. * @author Administrator
  6. * @version V1.0
  7. */
  8. public interface UserDao {
  9. //登录功能
  10. public abstract boolean islogin(String username,String password);
  11. //注册功能,直接传对象过来
  12. public abstract void regist(User user);
  13. }
  • 用户操作实现类

  1. package cn.itcast.dao.impl;
  2. import java.util.ArrayList;
  3. import cn.itcast.dao.UserDao;
  4. import cn.itcast.pojo.User;
  5. /**
  6. * 这是用户操作的具体实现类(集合版)
  7. *
  8. * @author Administrator
  9. * @version V1.0
  10. */
  11. public class UserDaoImpl implements UserDao {
  12. // 将集合定义为成员变量
  13. private static ArrayList<User> array = new ArrayList<User>();
  14. public boolean islogin(String username, String password) {
  15. // 遍历集合,获取每个用户,并判断用户名和密码是否匹配
  16. boolean flag = false;
  17. for (User user : array) {
  18. if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
  19. flag = true;
  20. break;
  21. }
  22. }
  23. return flag;
  24. }
  25. public void regist(User user) {
  26. // 将注册的信息存入集合,将集合看作服务器
  27. array.add(user);
  28. }
  29. }
  • 测试类

  1. package cn.itcast.test;
  2. import java.util.Scanner;
  3. import cn.itcast.dao.UserDao;
  4. import cn.itcast.dao.impl.UserDaoImpl;
  5. import cn.itcast.pojo.User;
  6. public class TestDemo {
  7. public static void main(String[] args) {
  8. while (true) {
  9. // 欢迎界面,给出选项
  10. System.out.println("--------------欢迎光临--------------");
  11. System.out.println("1登录");
  12. System.out.println("2注册");
  13. System.out.println("3退出");
  14. System.out.println("请输入你的选择:");
  15. // 键盘录入选择,为了后面录入信息方便,所有数据录入全部用字符串接受
  16. Scanner sc = new Scanner(System.in);
  17. String choiceString = sc.nextLine();
  18. // 多个地方要使用,就定义到外面
  19. UserDao ud = new UserDaoImpl();
  20. // 经过思考选择了switch
  21. switch (choiceString) {
  22. case "1":
  23. // 登录界面
  24. System.out.println("--------------登录界面--------------");
  25. System.out.println("请输入用户名:");
  26. String username = sc.nextLine();
  27. System.out.println("请输入密码:");
  28. String password = sc.nextLine();
  29. // 调用登录功能
  30. boolean flag = ud.islogin(username, password);
  31. if (flag) {
  32. System.out.println("登录成功,可以玩游戏了!");
  33. System.out.println("你玩游戏吗?y/n");
  34. while (true) {
  35. String resultString = sc.nextLine();
  36. if (resultString.equalsIgnoreCase("y")) {
  37. GuessNumber.start();
  38. System.out.println("你还玩吗?y/n");
  39. } else {
  40. break;
  41. }
  42. }
  43. System.out.println("谢谢使用,欢迎下次再来");
  44. System.exit(0);
  45. } else {
  46. System.out.println("用户名或者密码有误,登录失败");
  47. }
  48. break;
  49. case "2":
  50. // 注册界面
  51. System.out.println("--------------注册界面--------------");
  52. System.out.println("请输入用户名:");
  53. String newUsername = sc.nextLine();
  54. System.out.println("请输入密码:");
  55. String newPassword = sc.nextLine();
  56. // 将用户名与密码封装到对象中
  57. User user = new User();
  58. user.setUsername(newUsername);
  59. user.setPassword(newPassword);
  60. // 调用注册功能,多态用法
  61. ud.regist(user);
  62. System.out.println("注册成功!");
  63. break;
  64. case "3":
  65. default:
  66. System.out.println("谢谢使用,欢迎下次再来");
  67. System.exit(0);
  68. break;
  69. }
  70. }
  71. }
  72. }
  • 效果

三、模拟用户登录注册IO版

  • 分析

       A:集合版的问题是不能记忆注册后的账号和密码,每次运行程序,都会导致重置;

       B:使用IO流,将注册的信息写入到文件中,每次登录的时候,取出文件的信息进行匹配;

       C:关于测试类,猜数字类,用户基本信息类和用户操作类接口无须更改,只需在实现用户操作类接口时,

          将登录和注册操作用IO流实现;

  • 代码实现

  • IO流实现用户操作接口

  1. package cn.itcast.dao.impl;
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import cn.itcast.dao.UserDao;
  10. import cn.itcast.pojo.User;
  11. /**
  12. * 这是用户操作的具体实现类(IO版)
  13. *
  14. * @author 风清扬
  15. * @version V1.1
  16. *
  17. */
  18. public class UserDaoImpl implements UserDao {
  19. // 程序加载就创建文件,使用静态代码块
  20. private static File file = new File("user.txt");
  21. static {
  22. try {
  23. file.createNewFile();
  24. } catch (IOException e) {
  25. System.out.println("创建文件失败");
  26. // e.printStackTrace();
  27. }
  28. }
  29. @Override
  30. public boolean isLogin(String username, String password) {
  31. boolean flag = false;
  32. BufferedReader br = null;
  33. try {
  34. br = new BufferedReader(new FileReader(file));
  35. String line = null;
  36. while ((line = br.readLine()) != null) {
  37. // 得到用户名=密码
  38. String[] datas = line.split("=");
  39. if (datas[0].equals(username) && datas[1].equals(password)) {
  40. flag = true;
  41. break;
  42. }
  43. }
  44. } catch (FileNotFoundException e) {
  45. System.out.println("找不到信息所在的文件");
  46. } catch (IOException e) {
  47. System.out.println("用户登录失败");
  48. // e.printStackTrace();
  49. } finally {
  50. if (br != null) {
  51. try {
  52. br.close();
  53. } catch (IOException e) {
  54. System.out.println("用户登录释放资源失败");
  55. }
  56. }
  57. }
  58. return flag;
  59. }
  60. @Override
  61. public void regist(User user) {
  62. /*
  63. * 为了让注册的数据能够有一定的规则,自己定义一个规则: 用户名=密码(用等号隔开,目的是一次读一行,然后分割)
  64. */
  65. BufferedWriter bw = null;
  66. try {
  67. // 为了保证数据追加写入
  68. bw = new BufferedWriter(new FileWriter(file, true));
  69. bw.write(user.getUsername() + "=" + user.getPassword());
  70. bw.newLine();
  71. bw.flush();
  72. } catch (IOException e) {
  73. System.out.println("用户注册失败");
  74. // e.printStackTrace();
  75. } finally {
  76. // 防止空指针异常
  77. if (bw != null) {
  78. try {
  79. bw.close();
  80. } catch (IOException e) {
  81. System.out.println("用户注册释放资源失败");
  82. // e.printStackTrace();
  83. }
  84. }
  85. }
  86. }
  87. }

        

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/67563
推荐阅读
相关标签
  

闽ICP备14008679号