Home  >  Article  >  Java  >  Java case of implementing user login and registration based on IO version

Java case of implementing user login and registration based on IO version

黄舟
黄舟Original
2017-09-18 09:29:451834browse

The following editor will bring you an example of user login registration (Java) based on the IO version. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

What we’re learning today is the user login and registration function.

4 packages:

itcast.cn.user package User.java user class, describing the user’s basic information, including member variables, none Parameter constructor, construction with parameters (optional). get and set methods


package itcast.cn.day22;
/*
 * 用户基本描述包类
 */

public class User {
 private int userName;
 private int passWord;
 public User(){
  super();
 }
 public User(int userName,int passWord){
  super();
  this.userName=userName;
  this.passWord=passWord;  
 }
 public int getUserName() {
  return userName;
 }
 public void setUserName(int userName) {
  this.userName = userName;
 }
 public int getPassWord() {
  return passWord;
 }
 public void setPassWord(int passWord) {
  this.passWord = passWord;
 }

}

tcast.cn.uerDao package UserDao.java defines an interface, Declare the login registration function


public interface UserDao { 
 2 public abstract boolean islogin( String userName, String passWord);
 3 public abstract void register(User user);
 4 }

itcast.cn.userDaoImpl package UserDaoImpl.java implements the UserDao interface and specifically describes the login registration method.

It should be noted that in the login method, the return value boolean flag = true means the login is successful.

One thing that needs to be noted during the implementation process is: the declaration of global variables br and bw must be Place it outside the try braces

The IO version implementation method code is as follows:


public class UserDaoImpl implements UserDao{

@Override
public boolean islogin(String userName, String passWord) {
boolean flag = false;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("user,txt"));
String line = null;

while((line=br.readLine())!=null){
String[] datas = line.split("=");
if(datas[0].equals(userName)&&datas[1].equals(passWord)){
flag = true;
break;
}
}
} 
catch (FileNotFoundException e) {
//e.printStackTrace();
System.out.println("用户信息获取失败");
}catch (IOException e) {
//e.printStackTrace();
System.out.println("用户登录失败");
}
if(br!=null){
try {
br.close();
} catch (IOException e) {
//e.printStackTrace();
System.out.println("用户释放资源失败");
}
}
return flag;
}

@Override
public void register(User user) {
BufferedWriter bw = null; 
try {
bw = new BufferedWriter(new FileWriter("user.txt"));
bw.write(user.getUserName()+"="+user.getPassWord());
bw.newLine();
} catch (IOException e) {
//e.printStackTrace();
System.out.println("注册失败");
}finally{
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
//e.printStackTrace();
System.out.println("注册释放资源失败");
}
}
}
57}
}

itcast.cn.usertest package UserTest class test class

In this class, there is an unresolved problem:

scanner function cannot be input one by one, and (true) in the construction method needs to be read more .


package itcast.cn.day22;

import java.util.Scanner;

public class UserTest {
public static void main(String[] args) {
//欢迎界面
while(true){
UserDao ud = new UserDaoImpl();
System.out.println("*****欢迎进入*****");
System.out.println("登录请输入1");
System.out.println("注册请输入2");
System.out.println("返回请输入3");

System.out.println("请输入你的选择:");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
switch(choice){
case 1:
System.out.println("*****登录界面*****************");
//Scanner sc1 = new Scanner(System.in);
System.out.println("请输入用户名:");
String userName = sc.nextLine();
System.out.println("请输入密码:");
String passWord = sc.nextLine();

boolean flag = ud.islogin(userName, passWord);
if(flag){
System.out.println("登录成功");
break;
}else{
System.out.println("用户名或密码错误");
}

case 2:
System.out.println("*****注册界面****************");
System.out.println("请输入用户名:");
String userName1 = sc.nextLine();
System.out.println("请输入密码:");
String passWord1 = sc.nextLine();

User user = new User();
user.getUserName();
user.getPassWord();
ud.register(user);
System.out.println("注册成功");
break;
case 3:
default:
System.out.println("谢谢使用,欢迎下次再来");
System.exit(0);
break;
}
}
}
}

The above is the detailed content of Java case of implementing user login and registration based on IO version. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn