1、功能: 实现用户的注册,并能根据注册的信息正常登陆。
2、分析:
 a) 具体类
  i. 用户类
   1. 用户基本类
   2. 用户操作类
  ii. 测试类
 b) 每个具体类的内容
  i. 用户基本类
   1. 成员变量:用户名、密码
   2. 构造方法:无参构造方法
   3. 成员方法:getXxx()/setXxx()
  ii. 用户操作类
   1. 登陆:对存储用户名和密码的集合进行遍历。
   2. 注册:将注册用户名密码存入到集合中。
  iii. 测试类
   1. main方法
 c) 类与类之间的关系:测试类创建用户对象,并使用用户操作类的功能
 d) 分包(按模块划分)
  i. 用户基本类包
  ii. 用户操作类接口包
  iii. 用户操作类包
  iv. 测试类包
4、 程序编写顺序: 用户基本类、用户操作类接口、用户操作类、测试类
5、程序实例
下面给出两种实现方法:
 1、集合实现:退出程序注册的信息全部消失
 2、IO实现:注册信息存入到文件中,登陆时从文件中读取信息,退出程序之前的注册信息不丢失
 以上两种方法只有用户操作类不一样,其余的代码块都相同

a、用户基本类

package cn.itcast.pojo;
//用户基本类
public class UserInformation {
	private String username;
	
	private String passworld;
	
	public UserInformation(){
		
	}
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassworld() {
		return passworld;
	}

	public void setPassworld(String passworld) {
		this.passworld = passworld;
	}
	
}

 b、用户操作类接口

package cn.itcast.dao;

import cn.itcast.pojo.UserInformation;

//对用户进行操作的接口
public interface UserInformationDao {
	//登陆是否成功
	public abstract boolean isLogin(String username,String passworld);
	
	//用户注册功能
	public abstract void regist(UserInformation userinformation);
	
}

 c1、用户操作类(集合实现)

package cn.itcast.dao.impl;

import java.util.ArrayList;

import cn.itcast.dao.UserInformationDao;
import cn.itcast.pojo.UserInformation;

//用户操作的具体类
public class UserInformationDaoImpl implements UserInformationDao {

	//注意此处的变量必须是静态的,
	//因为成员变量是随着对象而存在的,而我们注册和登陆时建立并使用了两个对象,
	//登陆和注册必须要使用同一个集合才有意义(这样注册后的账号才能被查找到)
	private static ArrayList<UserInformation> array=new ArrayList<UserInformation>();
	
	public boolean isLogin(String username, String passworld) {
		
		boolean flag=false;
		
		for (UserInformation ui:array){
			if(ui.getUsername().equals(username)&&
					ui.getPassworld().equals(passworld)){
				flag=true;
				break;
			}
		}
		
		return flag;
	}

	
	public void regist(UserInformation userinformation) {
		
		array.add(userinformation);

	}

}

 c2、用户操作类(IO实现)

package cn.itcast.dao.impl;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter ;
import java.io.IOException;


import cn.itcast.dao.UserInformationDao;
import cn.itcast.pojo.UserInformation;

//用户操作的具体类(IO版)
//登陆类,遍历并校验信息
public class UserInformationDaoImpl implements UserInformationDao {
	private static File file=new File("user.txt");
	//静态代码块,保证程序加载时文件便被创建
	static {
		try {
			file.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("创建文件失败");
		}
	};
	public boolean isLogin(String username, String passworld) {
		BufferedReader br=null;
		boolean flag=false;
		try {
			br=new BufferedReader(new FileReader(file));
			String s=null;
			while((s=br.readLine())!=null){
				String []data=s.split("=");
				if((data[0].equals(username))&&(data[1].equals(passworld))) {
					flag=true;
				}
			}			
		} catch (FileNotFoundException e) {
//			e.printStackTrace();
			System.out.println("信息文件尚未创建");
		}catch (IOException e) {
			System.out.println("用户登陆失败");
//			e.printStackTrace();
		}finally {
			if(br!=null) {
				try {
					br.close();
				} catch (IOException e) {
//					e.printStackTrace();
					System.out.println("用户登陆资源释放失败");
				}
			}		
		}
		
		
		return flag;
	}

	//注册类,存储信息
	public void regist(UserInformation userinformation) {
		//存储规则:一行存一个用户,格式:账号+密码
		BufferedWriter bw=null;
		try {
			//需要加true,避免之前的注册信息被重写覆盖
			bw=new BufferedWriter(new FileWriter(file,true));
			bw.write(userinformation.getUsername()+"="+userinformation.getPassworld());
			bw.newLine();
			bw.flush();
		} catch (IOException e) {
			System.out.println("用户注册失败");
//			e.printStackTrace();
		}finally {
			if(bw!=null) {
				try {
					bw.close();
				} catch (IOException e) {
//					e.printStackTrace();
					System.out.println("用户注册资源释放失败");
				}
			}
		}

	}

}

 d、测试类

package cn.itcast.test;

import java.util.Scanner;

import cn.itcast.dao.UserInformationDao;
import cn.itcast.dao.impl.UserInformationDaoImpl;
import cn.itcast.pojo.UserInformation;

public class UserInformationTest {
	public static void main(String[] args) {
		while(true) {
			System.out.println("----------欢迎使用----------");
			System.out.println("1、登陆	  2、注册	   3、退出");	
			System.out.println("请输入您的选择");
			Scanner sc=new Scanner(System.in);
			UserInformationDao uid=new UserInformationDaoImpl();
			String choice = sc.nextLine();
			switch (choice) {
			
			//登陆
			case "1":
				System.out.println("----------登陆----------");
				
				System.out.println("请输入登陆的用户名");
				String name=sc.nextLine();
				System.out.println("请输入登陆的密码");
				String password=sc.nextLine();
				
				boolean flag=uid.isLogin(name, password);
				if(flag) {
					System.out.println("登陆成功");
					System.exit(0);
				}else {
					System.out.println("用户名或密码有误,登陆失败");
				}
				break;
				
			//注册
			case "2":
				System.out.println("----------注册----------");
				
				System.out.println("请输入注册的用户名");
				String newname=sc.nextLine();
				System.out.println("请输入注册的密码");
				String newpassword=sc.nextLine();
				
				UserInformation u=new UserInformation();
				u.setPassworld(newpassword);
				u.setUsername(newname);
			
				uid.regist(u);
				
				System.out.println("注册成功");
				break;
					
			//退出	
			case "3":
				System.out.println("感谢您的使用");
				sc.close();
				System.exit(0);
				break;
			default:
				System.out.println("输入有误,请重新输入");
				break;
			
			}
			
		}
		
	}
	
	
}

 e、输出结果显示

java实现登入注册 java实现简单的登录注册_java