Java基础系列六之使用集合模拟用户登录注册系统

定义接口

代码示例

package org.westos.dao;

import org.westos.entity.User;

public interface UserDao {
	public abstract boolean isLogin(String username,String password);
	public abstract void regist(User user);
}

定义接口的子实现类

package org.westos.dao.impl;

import java.util.ArrayList;

import org.westos.dao.UserDao;
import org.westos.entity.User;

public class UserDaoImpl implements UserDao {
	private static ArrayList<User> arrayList = new ArrayList<User>();

	@Override
	public boolean isLogin(String username, String password) {
		// TODO Auto-generated method stub
		boolean flag = false;
		for (User user : arrayList) {
			if (user.getName().equals(username) && user.getPassword().equals(password)) {
				flag = true;
				break;
			}
		}
		return flag;
	}

	@Override
	public void regist(User user) {
		// TODO Auto-generated method stub
		arrayList.add(user);
	
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java实现模拟用户注册的代码,其中使用了HashSet集合来存储用户信息并进行排重: ```java import java.util.HashSet; import java.util.Scanner; public class UserRegister { public static void main(String[] args) { HashSet<User> userSet = new HashSet<>(); // 创建HashSet集合存储用户信息 Scanner scanner = new Scanner(System.in); while (true) { System.out.println("请输入用户名:"); String username = scanner.nextLine(); System.out.println("请输入密码:"); String password = scanner.nextLine(); System.out.println("请再次输入密码:"); String confirmPassword = scanner.nextLine(); System.out.println("请输入生日(格式为yyyy-mm-dd):"); String birthday = scanner.nextLine(); System.out.println("请输入手机号:"); String phone = scanner.nextLine(); System.out.println("请输入邮箱:"); String email = scanner.nextLine(); // 判断输入信息是否正确 if (!isValid(username, password, confirmPassword, birthday, phone, email)) { System.out.println("输入信息有误,请重新输入!"); continue; } // 判断用户是否重复 User user = new User(username, password, birthday, phone, email); if (userSet.contains(user)) { System.out.println("该用户已存在,请重新输入!"); continue; } // 注册成功,将用户信息添加到HashSet集合中 userSet.add(user); System.out.println("注册成功!"); break; } } // 判断输入信息是否正确 public static boolean isValid(String username, String password, String confirmPassword, String birthday, String phone, String email) { if (username.length() < 6 || username.length() > 20) { return false; } if (password.length() < 6 || password.length() > 20) { return false; } if (!password.equals(confirmPassword)) { return false; } if (!birthday.matches("\\d{4}-\\d{2}-\\d{2}")) { return false; } if (!phone.matches("1[3578]\\d{9}")) { return false; } if (!email.contains("@")) { return false; } return true; } } class User { private String username; private String password; private String birthday; private String phone; private String email; public User(String username, String password, String birthday, String phone, String email) { this.username = username; this.password = password; this.birthday = birthday; this.phone = phone; this.email = email; } // 重写equals方法,用于判断用户是否重复 @Override public boolean equals(Object obj) { if (obj instanceof User) { User user = (User) obj; return this.username.equals(user.username); } return false; } // 重写hashCode方法,用于HashSet集合的排重 @Override public int hashCode() { return this.username.hashCode(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值