1. 登录注册IO版的Java项目框架,如下:
2. 具体代码实现:
(1)User.java(cn.itcast.game):
1 package cn.itcast.pojo; 2 3 /** 4 * 这是用户基本描述类 5 * 6 * @author 风清扬 7 * @version V1.1 8 * 9 */ 10 public class User { 11 // 用户名 12 private String username; 13 // 密码 14 private String password; 15 16 public User() { 17 } 18 19 public String getUsername() { 20 return username; 21 } 22 23 public void setUsername(String username) { 24 this.username = username; 25 } 26 27 public String getPassword() { 28 return password; 29 } 30 31 public void setPassword(String password) { 32 this.password = password; 33 } 34 }
(2)UserDao.java(cn.itcast.dao):
1 package cn.itcast.dao; 2 3 import cn.itcast.pojo.User; 4 5 /** 6 * 这是针对用户进行操作的接口 7 * 8 * @author 风清扬 9 * @version V1.1 10 * 11 */ 12 public interface UserDao { 13 /** 14 * 这是用户登录功能 15 * 16 * @param username 17 * 用户名 18 * @param password 19 * 密码 20 * @return 返回登录是否成功 21 */ 22 public abstract boolean isLogin(String username, String password); 23 24 /** 25 * 这是用户注册功能 26 * 27 * @param user 28 * 要注册的用户信息 29 */ 30 public abstract void regist(User user); 31 }
(3)UserDaoImpl.java(cn.itcast.dao.impl):
1 package cn.itcast.dao.impl; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileNotFoundException; 7 import java.io.FileReader; 8 import java.io.FileWriter; 9 import java.io.IOException; 10 11 import cn.itcast.dao.UserDao; 12 import cn.itcast.pojo.User; 13 14 /** 15 * 这是用户操作的具体实现类(IO版) 16 * 17 * @author 风清扬 18 * @version V1.1 19 * 20 */ 21 public class UserDaoImpl implements UserDao { 22 // 为了保证文件一加载就创建 23 private static File file = new File("user.txt"); 24 25 static { 26 try { 27 file.createNewFile(); 28 } catch (IOException e) { 29 System.out.println("创建文件失败"); 30 // e.printStackTrace(); 31 } 32 } 33 34 @Override 35 public boolean isLogin(String username, String password) { 36 boolean flag = false; 37 38 BufferedReader br = null; 39 try { 40 // br = new BufferedReader(new FileReader("user.txt")); 41 br = new BufferedReader(new FileReader(file)); 42 String line = null; 43 while ((line = br.readLine()) != null) { 44 // 用户名=密码 45 String[] datas = line.split("="); 46 if (datas[0].equals(username) && datas[1].equals(password)) { 47 flag = true; 48 break; 49 } 50 } 51 } catch (FileNotFoundException e) { 52 System.out.println("用户登录找不到信息所在的文件"); 53 // e.printStackTrace(); 54 } catch (IOException e) { 55 System.out.println("用户登录失败"); 56 // e.printStackTrace(); 57 } finally { 58 if (br != null) { 59 try { 60 br.close(); 61 } catch (IOException e) { 62 System.out.println("用户登录释放资源失败"); 63 // e.printStackTrace(); 64 } 65 } 66 } 67 68 return flag; 69 } 70 71 @Override 72 public void regist(User user) { 73 /* 74 * 为了让注册的数据能够有一定的规则,我就自己定义了一个规则: 用户名=密码 75 */ 76 BufferedWriter bw = null; 77 try { 78 // bw = new BufferedWriter(new FileWriter("user.txt")); 79 // bw = new BufferedWriter(new FileWriter(file)); 80 // 为了保证数据是追加写入,必须加true 81 bw = new BufferedWriter(new FileWriter(file, true)); 82 bw.write(user.getUsername() + "=" + user.getPassword()); 83 bw.newLine(); 84 bw.flush(); 85 } catch (IOException e) { 86 System.out.println("用户注册失败"); 87 // e.printStackTrace(); 88 } finally { 89 if (bw != null) { 90 try { 91 bw.close(); 92 } catch (IOException e) { 93 System.out.println("用户注册释放资源失败"); 94 // e.printStackTrace(); 95 } 96 } 97 } 98 } 99 }
(4)GuessNumber.java(cn.itcast.game):
1 package cn.itcast.game; 2 3 import java.util.Scanner; 4 5 /** 6 * 这是猜数字小游戏 7 * 8 * @author 风清扬 9 * @version V1.1 10 * 11 */ 12 public class GuessNumber { 13 private GuessNumber() { 14 } 15 16 public static void start() { 17 // 产生一个随机数 18 int number = (int) (Math.random() * 100) + 1; 19 20 // 定义一个统计变量 21 int count = 0; 22 23 while (true) { 24 // 键盘录入一个数据 25 Scanner sc = new Scanner(System.in); 26 System.out.println("请输入数据(1-100):"); 27 int guessNumber = sc.nextInt(); 28 29 count++; 30 31 // 判断 32 if (guessNumber > number) { 33 System.out.println("你猜的数据" + guessNumber + "大了"); 34 } else if (guessNumber < number) { 35 System.out.println("你猜的数据" + guessNumber + "小了"); 36 } else { 37 System.out.println("恭喜你," + count + "次就猜中了"); 38 break; 39 } 40 } 41 } 42 }
(5)测试类UserTest.java(cn.itcast.test):
1 package cn.itcast.test; 2 3 import java.util.Scanner; 4 5 import cn.itcast.dao.UserDao; 6 import cn.itcast.dao.impl.UserDaoImpl; 7 import cn.itcast.game.GuessNumber; 8 import cn.itcast.pojo.User; 9 10 /** 11 * 用户测试类 12 * 13 * @author 风清扬 14 * @version V1.1 15 * 16 */ 17 public class UserTest { 18 public static void main(String[] args) { 19 // 为了能够回来 20 while (true) { 21 // 欢迎界面,给出选择项 22 System.out.println("--------------欢迎光临--------------"); 23 System.out.println("1 登录"); 24 System.out.println("2 注册"); 25 System.out.println("3 退出"); 26 System.out.println("请输入你的选择:"); 27 // 键盘录入选择,根据选择做不同的操作 28 Scanner sc = new Scanner(System.in); 29 // 为了后面的录入信息的方便,我所有的数据录入全部用字符接收 30 String choiceString = sc.nextLine(); 31 32 // switch语句的多个地方要使用,我就定义到外面 33 UserDao ud = new UserDaoImpl(); 34 35 // 经过简单的思考,我选择了switch 36 switch (choiceString) { 37 case "1": 38 // 登录界面,请输入用户名和密码 39 System.out.println("--------------登录界面--------------"); 40 System.out.println("请输入用户名:"); 41 String username = sc.nextLine(); 42 System.out.println("请输入密码:"); 43 String password = sc.nextLine(); 44 45 // 调用登录功能 46 // UserDao ud = new UserDaomImpl(); 47 48 boolean flag = ud.isLogin(username, password); 49 if (flag) { 50 System.out.println("登录成功,可以开始玩游戏了"); 51 52 System.out.println("你玩吗?y/n"); 53 while (true) { 54 String resultString = sc.nextLine(); 55 if (resultString.equalsIgnoreCase("y")) { 56 // 玩游戏 57 GuessNumber.start(); 58 System.out.println("你还玩吗?y/n"); 59 } else { 60 break; 61 } 62 } 63 System.out.println("谢谢使用,欢迎下次再来"); 64 System.exit(0); 65 // break; //这里写break,结束的是switch 66 } else { 67 System.out.println("用户名或者密码有误,登录失败"); 68 } 69 break; 70 case "2": 71 // 欢迎界面,请输入用户名和密码 72 System.out.println("--------------注册界面--------------"); 73 System.out.println("请输入用户名:"); 74 String newUsername = sc.nextLine(); 75 System.out.println("请输入密码:"); 76 String newPassword = sc.nextLine(); 77 78 // 把用户名和密码封装到一个对象中 79 User user = new User(); 80 user.setUsername(newUsername); 81 user.setPassword(newPassword); 82 83 // 调用注册功能 84 // 多态 85 // UserDao ud = new UserDaoImpl(); 86 // 具体类使用 87 // UserDaoImpl udi = new UserDaoImpl(); 88 89 ud.regist(user); 90 System.out.println("注册成功"); 91 break; 92 case "3": 93 default: 94 System.out.println("谢谢使用,欢迎下次再来"); 95 System.exit(0); 96 break; 97 } 98 } 99 } 100 }
运行效果,如下: