需求
- 用户手误没有输入数字,程序不能报错,提示用户输入数字,直到输入数字为止
- 登录机会给三次,三次登录不上,退出系统
- 注册是对用户名和密码进行规则校验,用户名6-16位大小写字母,密码6-16位数字
- 注册时判断该用户名是否已被注册
- 登录注册,配合IO流把用户名和密码存到文件里
运行流程
-
进入系统
选择需求(1.注册、2.登录、3.修改密码、4.退出)
对输入内容进行限制(1-4)
-
注册
输入用户名(对用户名规则进行校验)(对账号唯一性进行校验)
输入密码(对密码规则进行校验)
注册成功,将账号密码存储到文件中
-
登录
输入用户名(对用户名是否存在进行校验)
输入密码(对密码是否正确进行校验)(限定密码输入次数)
密码正确,是否进入游戏内容
密码错误,退出系统
-
修改密码
输入用户名(对用户名是否存在进行校验)
输入旧密码(对密码是否正确进行校验)(限定密码输入次数)
输入新密码(对密码规则进行校验)(新密码不能与旧密码一致)
修改成功,将新密码存储到文件中
-
退出
关闭程序
代码实现
创建一个User类
要求:包含账号、密码这两个成员变量
package study.luiuer.demo.bean;
public class User {
//成员变量:账号名
private String username;
//成员变量:密码
private String password;
//空参构造
public User() {
}
//有参构造
public User(String username, String password) {
this.username = username;
this.password = password;
}
//get/set方法
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
要求一个UserDao接口类
package study.luiuer.demo.dao;
import study.luiuer.demo.bean.User;
import java.io.IOException;
public interface UserDao {
//注册方法
public abstract void registerUser(User user) throws IOException;
//登录方法
public abstract boolean login(String username, String password) throws IOException;
}
实现类
package study.luiuer.demo.dao;
import study.luiuer.demo.bean.User;
import java.io.*;
import java.util.Properties;
public class IODao implements UserDao{
//封装一个properties文件
File file=new File("User.properties");
//创建这个properties流
Properties properties = new Properties();
//重写注册方法
@Override
public void registerUser(User user) throws IOException {
//将文件内容缓存进流中
properties.load(new FileInputStream(file));
//判断账户名是否有重复
if (properties.containsKey(user.getUsername())){
/*System.out.println("账户已被注册");*/
}else{
//将账号名、密码输入到流
properties.setProperty(user.getUsername(), user.getPassword());
//存储
properties.store(new FileOutputStream(file), null);
}
}
//重写登录方法
@Override
public boolean login(String username, String password) throws IOException {
boolean flag=false;
//将文件内容缓存进流中
properties.load(new FileInputStream(file));
//判断是否有这个用户名
if (properties.containsKey(username)){
//判断密码是否对应用户名
if (password.equals(properties.getProperty(username))){
flag=true;
}else{
System.out.print("密码输入错误");
}
}else{
System.out.println("用户名输入错误");
}
return flag;
}
//写一个修改密码方法
public void changeUser(String username,String password) throws IOException {
//将文件内容缓存进流中
properties.load(new FileInputStream(file));
//将账户名和新密码输入到流
properties.setProperty(username, password);
//存储
properties.store(new FileOutputStream(file), null);
}
}
抽取方法
校验用户名是否已被使用(用在了注册过程中)
package study.luiuer.demo.service;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class chachong {
public static boolean chaChong(String userName) throws IOException {
//封装一个properties文件
File file = new File("User.properties");
//创建这个properties流
Properties properties = new Properties();
//将文件缓存到流中
properties.load(new FileInputStream(file));
boolean b=false;
//判断新输入的用户名是否已经存在
if (properties.containsKey(userName)) {
/* System.out.println("账户已被注册");*/
b=false;
}else {
b=true;
}
return b;
}
}
校验用户名是否存在(用在了登录、修改密码过程中)
package study.luiuer.demo.service;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class chazhaoyhm {
public static boolean yhm(String userName) throws IOException {
File file = new File("User.properties");
Properties properties = new Properties();
properties.load(new FileInputStream(file));
boolean b=true;
if (!properties.containsKey(userName)){
b=false;
}
return b;
}
}
校验新密码是否与旧密码相同
package study.luiuer.demo.service;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class chachongmm {
public static boolean mm(String password) throws IOException {
File file = new File("User.properties");
Properties properties = new Properties();
properties.load(new FileInputStream(file));
boolean b=false;
//判断新密码是否与上次密码一致
if (properties.containsValue(password)) {
b=false;
}else {
b=true;
}
return b;
}
}
写一个小游戏(猜数字)
package study.luiuer.demo.game;
import java.util.Random;
import java.util.Scanner;
public class GuessNumber {
public static void playGame() {
Random random = new Random();
int num = random.nextInt(100) + 1;
int max, min;
int m = 0;
int n = 100;
for (int i = 10; i > 0; i--) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数(1-100)");
int userNum = sc.nextInt();
if(userNum>=0&&userNum<=100) {
if (userNum > num) {
System.out.println("猜大啦");
max = userNum - 1;
min = m;
System.out.print("提示:" + min + "~" + max + ",");
n = userNum - 1;
} else if (userNum < num) {
System.out.println("猜小啦");
min = userNum + 1;
max = n;
System.out.print("提示:" + min + "~" + max + ",");
m = userNum + 1;
} else {
System.out.println("恭喜你,猜对啦!");
break;
}
if (i > 1) {
System.out.println("你还有" + (i - 1) + "次机会");
} else if (i == 1) {
System.out.println("机会用尽,游戏结束");
break;
}
}else {
System.out.println("输入的数字不在范围内");
i++;
}
}
}
}
主方法
package study.luiuer.demo.ui;
import study.luiuer.demo.bean.User;
import study.luiuer.demo.dao.IODao;
import study.luiuer.demo.dao.UserDao;
import study.luiuer.demo.game.GuessNumber;
import study.luiuer.demo.service.chachong;
import study.luiuer.demo.service.chachongmm;
import study.luiuer.demo.service.chazhaoyhm;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class LoginUI {
public static void main(String[] args) throws IOException {
File file = new File("User.properties");
if (!file.exists()) {
file.createNewFile();
}
UserDao userDao = new IODao();
while (true) {
System.out.println("---欢迎使用本系统 1.注册 2.登录 3.修改密码 4.退出 ---");
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
String s2 = "[1-4]";
if (!s1.matches(s2)) {
System.out.println("输入有误,请重新输入");
continue;
} else {
switch (s1) {
case "1":
sc = new Scanner(System.in);
while (true) {
System.out.println("注册:请输入用户名");
String username = sc.nextLine();
if (username.matches("[a-zA-Z]{6,16}") && chachong.chaChong(username)) {
while (true) {
System.out.println("注册:请输入密码");
String password = sc.nextLine();
if (password.matches("[0-9]{6,16}")) {
User user = new User(username, password);
userDao.registerUser(user);
System.out.println("注册成功");
} else {
System.out.println("密码格式错误,请重新输入密码");
continue;
}
break;
}
} else if (!chachong.chaChong(username)) {
System.out.println("账户已被注册");
continue;
} else if (!username.matches("[a-zA-Z]{6,16}")) {
System.out.println("账号格式错误,请重新输入账号");
continue;
}
break;
}
break;
case "2":
while (true) {
sc = new Scanner(System.in);
System.out.println("登录:请输入用户名");
String username2 = sc.nextLine();
if (!chazhaoyhm.yhm(username2)) {
System.out.println("用户名错误,请重试");
continue;
} else {
for (int i = 2; i >= 0; i--) {
System.out.println("登录:请输入密码");
String password2 = sc.nextLine();
boolean b = userDao.login(username2, password2);
if (b == false) {
while (i == 0) {
System.out.println("登录失败,再见!");
System.exit(0);
}
System.out.println("请重试(还有" + i + "次机会)");
continue;
} else {
System.out.println("登录成功 要玩游戏吗?y/n");
sc = new Scanner(System.in);
while (true) {
String s = sc.nextLine();
if (s.equalsIgnoreCase("y")) {
GuessNumber.playGame();
System.out.println("还要玩吗?y/n");
} else {
System.out.println("谢谢使用,再见!");
System.exit(0);
}
}
}
}
}
break;
}
break;
case "3":
while (true) {
sc = new Scanner(System.in);
System.out.println("修改:请输入用户名");
String username3 = sc.nextLine();
if (!chazhaoyhm.yhm(username3)) {
System.out.println("用户名错误,请重试");
} else {
for (int i = 2; i >= 0; i--) {
System.out.println("请输入旧密码");
String password4 = sc.nextLine();
boolean b = userDao.login(username3, password4);
if (b == false) {
System.out.println("密码错误,请重试(还有" + i + "次机会)");
while (i == 0) {
System.out.println("登录失败,无法修改密码,再见!");
System.exit(0);
}
continue;
} else {
while (true) {
System.out.println("修改:请输入新密码");
String password3 = sc.nextLine();
if (password3.matches("[0-9]{6,16}") && chachongmm.mm(password3)) {
((IODao) userDao).changeUser(username3, password3);
System.out.println("修改成功");
break;
} else if (password3.matches("[0-9]{6,16}") && !chachongmm.mm(password3)) {
System.out.println("新密码与上一次密码一致,请重试");
} else if (!password3.matches("[0-9]{6,16}")) {
System.out.println("新密码格式错误,请重新输入密码");
}
}
break;
}
}
break;
}
}
break;
case "4":
default:
System.out.println("谢谢使用,再见!");
System.exit(0);
break;
}
}
}
}
}
完