package com.shujia.day18.logindemo;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
/*
登录注册java版:
思考1:将用户名和密码写在记事本中,编写登录和注册的方法,要求必须先注册才可以登录
思考2:修改用户的信息,记事本实现
思考3:加入手机短信验证码功能(放到maven之后再说)
思考4:使用mysql存储用户的信息
*/
public class IOTest2 {
private static Scanner sc = new Scanner(System.in);
public static void init() {
System.out.println("=============== ^_^ 欢迎进入魏一民婚恋介绍网站 ^_^ ==================");
System.out.println("1.登录 2.注册 3.修改 4.注销");
System.out.print("请输入您的操作编号:");
int choice = sc.nextInt();
switch (choice) {
case 1:
login();
break;
case 2:
regeist();
break;
case 3:
update();
break;
default:
System.out.println("您的输入有误!");
break;
}
}
public static void update() {
//查询所有的用户
ArrayList<User> allUsers = getAllUsers();
System.out.println("请输入您需要修改的用户名:");
String name = sc.next();
//查询用户是否存在
User user = getUserByName(name);
if(user!=null){
for(int i=0;i<allUsers.size();i++){
User u = allUsers.get(i);
if(u.getName().equals(name)){
allUsers.remove(u);
}
}
System.out.println("请选择您要修改的内容:1.姓名 2.密码");
int choice = sc.nextInt();
switch (choice){
case 1:
System.out.println("请输入新名称:");
String newName = sc.next();
user.setName(newName);
break;
case 2:
System.out.println("请输入新密码:");
String newPwd = sc.next();
user.setPassword(newPwd);
break;
default:
System.out.println("无效输入!");
break;
}
}else {
return;
}
//将修改后用户添加到集合中
allUsers.add(user);
BufferedWriter bw= null;
try {
//创建字符缓冲输出流
bw = new BufferedWriter(new FileWriter("src/com/shujia/day17/data/users.txt"));
for (User allUser : allUsers) {
bw.write(allUser.getName()+","+allUser.getPassword());
bw.newLine();
bw.flush();
}
System.out.println("用户修改成功!");
}catch (Exception e){
e.printStackTrace();
}finally {
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static User getUserByName(String name) {
BufferedReader br = null;
boolean flag = true;
User user = null;
try {
//创建字符缓冲输入流对象
//public FileReader(File file)
File file = new File("src/com/shujia/day17/data/users.txt");
//判断用户文件中是否用用户
if (file.length() > 0) {
br = new BufferedReader(new FileReader(file));
String line = null;
while ((line = br.readLine()) != null) {
//魏一民,123456
String name1 = line.split(",")[0];
String password1 = line.split(",")[1];
//判断用户是否与登录的用户一样
if (name1.equals(name)) {
flag = false;
user = new User(name1,password1);
break;
}
}
if (flag) {
System.out.println("该用户未进行注册!");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return user;
}
public static void regeist() {
System.out.println("----------------- 欢迎注册魏一民婚恋介绍网站 --------------------");
ArrayList<User> allUsers = getAllUsers();
String password = null;
BufferedWriter bw = null;
System.out.print("请输入您的用户名:");
String name = sc.next();
for (User user : allUsers) {
if (user.getName().equals(name)) {
System.out.println("该用户名已被使用!");
return; // 方法已经结束了
}
}
System.out.println("请设置账户密码:");
password = sc.next();
try {
//将用户名和密码写到文件中
bw = new BufferedWriter(new FileWriter("src/com/shujia/day17/data/users.txt", true));
bw.newLine();
bw.write(name + "," + password);
bw.flush();
System.out.println("用户注册成功!");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static ArrayList<User> getAllUsers() {
BufferedReader br = null;
//创建一个ArrList集合,用户存储读取到用户
ArrayList<User> users = new ArrayList<>(); // 新创建的集合中元素个数是0
try {
//创建字符缓冲输入流对象
//public FileReader(File file)
File file = new File("src/com/shujia/day17/data/users.txt");
//判断用户文件中是否用用户
if (file.length() > 0) {
br = new BufferedReader(new FileReader(file));
String line = null;
while ((line = br.readLine()) != null) {
//魏一民,123456
String name1 = line.split(",")[0];
String password1 = line.split(",")[1];
//判断用户是否与登录的用户一样
users.add(new User(name1, password1));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return users;
}
public static void login() {
System.out.print("请输入您的用户名:");
String name = sc.next();
System.out.print("请输入您的密码:");
String password = sc.next();
ArrayList<User> user = getOneUser(name, password);
if (user.size() == 1) {
System.out.println("登录成功!开始找寻另一半!");
} else {
System.out.println("用户登录失败!");
}
//1、去存储信息文件中查询该用户是否存在,
//2、若存在,再判断密码是否正确。
//思考:如果有多个用户注册怎么存储呢?怎么判断呢?
//将每个用户封装成一个User对象
}
public static ArrayList<User> getOneUser(String name, String password) {
BufferedReader br = null;
//创建一个ArrList集合,用户存储读取到用户
ArrayList<User> users = new ArrayList<>(); // 新创建的集合中元素个数是0
boolean flag = true;
try {
//创建字符缓冲输入流对象
//public FileReader(File file)
File file = new File("src/com/shujia/day17/data/users.txt");
//判断用户文件中是否用用户
if (file.length() > 0) {
br = new BufferedReader(new FileReader(file));
String line = null;
while ((line = br.readLine()) != null) {
//魏一民,123456
String name1 = line.split(",")[0];
String password1 = line.split(",")[1];
//判断用户是否与登录的用户一样
if (name1.equals(name)) {
flag = false;
if (password1.equals(password)) {
users.add(new User(name1, password1));
} else {
System.out.println("用户密码输入错误!");
}
break;
}
}
if (flag) {
System.out.println("该用户未进行注册!");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return users;
}
public static void main(String[] args) {
init();
}
}