Java中简单的注册、登陆实例

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、程序实例
 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);
	
}

 c、用户操作类

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);

	}

}

 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、输出结果显示
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/zfliu96/article/details/83587975