使用面向对象的思想实现登录和注册修改密码,使用封装的特性

User,  username, password, sex,age --- , login(用户名和密码), register(用户名和密码),拓展 : changePwd(index,pwd)

使用数组作为容器,存储对象,User[] users = {user1, user2 ,user3};

程序初始化的时候,在容器存储2个对象,

输入用户名和密码:

请选择操作:1注册(用户名,密码格式校验),2登录

成功之后:

1:个人中心: 展示自己的个人信息

2:修改密码

package javaHome3;

public class User2 {
	private String username;
	private String password;
	private String sex="女";
	private int age=18;
	int flag1=0;
	int flag2=0;
	int flagUs2=0;
	int flagPa2=0;
	int flagZc=0;
	int yhi;
	int mmi;
	/**
	 * @return the age
	 */
	public int getAge() {
		return age;
	}
	/**
	 * @param age the age to set
	 */
	public void setAge(int age) {
		this.age = age;
		
	}
	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}
	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}
	/**
	 * @return the sex
	 */
	public String getSex() {
		return sex;
	}
	/**
	 * @param sex the sex to set
	 */
	public void setSex(String sex) {
		this.sex = sex;
	}
	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}
	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}
	public void checkUs(String yhm,User2[] us){
		for (int i = 0; i < us.length; i++) {
			if(us[i].getUsername().equals(yhm)){
				this.flag1=1;
				this.flagUs2=1;
				this.flagZc=1;
				 yhi=i;
				 
				break;
				
			}
		}
		
	}
	public void checkPa2(String mm,User2[] us){
		for (int i = 0; i < us.length; i++) {
			if(us[i].getPassword().equals(mm)){
				this.flagPa2=1;
				int mmi=i;
				if(flagUs2==1&&flagPa2==1&&yhi==mmi){					
					break;
				}
			}
		}
		
	}
	public void pd2(){
		if(flagUs2==1){
			System.out.println("用户名正确√");
			}else{System.out.println("用户名不存在×");}
		}
	public void pd3(){
		if(flagPa2==1){
			System.out.println("密码正确√");
			}else{System.out.println("密码错误×");}
		}

	public int pd4(int flagPd4){
		if(flagPa2==0||flagUs2==0){
			System.out.println("用户名或密码错误");
			}else{flagPd4=1;}return flagPd4;
		}
	public int pdZc(int flagZc){
		if(flagZc==1){
			System.out.println("用户名已经存在");
			}else{flagZc=0;}return flagZc;
		}
	public void checkPa(String mm,User2[] us){
		for (int i = 0; i < us.length; i++) {
			if(us[i].getPassword().equals(mm)){
				this.flag2=1;
				int mmi=i;
				if(flag1==1&&flag2==1&&yhi==mmi){
					System.out.println("登录成功!");
					break;
				}								
			}
		}
		
	}
	public int pd(int flag3){
		if(flag1==0||flag2==0){
			System.out.println("用户名或密码错误");
			
		}else{flag3=1;}
		return flag3;
	}
	public User2[] register(User2[] us,String yh,String mm){
		
		for (int i = 0; i < us.length; i++) {
			if(us[us.length-1]==null){
				User2 us0=new User2();
				us0.username=yh;
				us0.password=mm;
				us[us.length-1]=us0;//遍历找空插入×
				break;
			}else{
				//int fir=us[i].getPassword().substring(0,1).compareTo("[A-Z]");
				//System.out.println(fir);
				User2[] us0=new User2[us.length+1];
				User2 us3=new User2();
				for (int j = 0; j < us.length; j++) {
					us0[j]=us[j];
					//传值
				}
				for (int k = 0; k < us0.length; k++) {
					if(us0[k]==null){
						us3.username=yh;
						us3.password=mm;
						us0[k]=us3;
						us=us0;
						System.out.println("注册成功");
						System.out.println("用户名:"+us3.getUsername()+" 性别:"+us3.getSex()+
								" 年龄:"+us3.getAge()+" 密码:"+us3.getPassword());
						break;
					}
				}break;
			}					
		}return us;
		
	}
    public static boolean checkPasswordRule(String password){
        String regex = "([A-Z])";
        boolean isMatch = password.substring(0, 1).matches(regex);
        
        if(isMatch&&password.length()>=5&&password.length()<=10){
            return true;
        }else {
            return false;
        }
    }
    public static boolean checkusernameRule(String username){
        if(username.length()>=5&&username.length()<=10){
            return true;
        }else {
            return false;
        }
    }

	public User2[] changemm(String yh,String mm,User2[] us){
		
		for (int i = 0; i < us.length; i++) {

			if(us[i].getPassword().length()<5||us[i].getPassword().length()>10){
				System.out.println("密码长度不合格");
			}
			if(us[i].getUsername().equals(yh)){
				us[i].password=mm;
				System.out.println("新密码为:"+us[i].password);
				return us;
			}
		}return us;					
	}

	public void show2(User2[] us){
		for (int i = 0; i < us.length; i++) {
				if(us[i]==null){break;}	
				else{
					System.out.println("用户名:"+us[i].getUsername()+" 性别:"+us[i].getSex()+" 年龄:"
							+us[i].getAge()+" 密码:"+us[i].getPassword());
					
				}						
		}
	}
}
package javaHome3;

import java.util.Scanner;

import javaWeek3.Student;

public class DemoUser2 {
	public static void main(String[] args) {
		User2 us1=new User2();//创建对象
		us1.setUsername("张三");
		us1.setAge(20);
		us1.setSex("男");
		us1.setPassword("123123");
		User2 us2=new User2();
		us2.setUsername("李四");
		us2.setAge(22);
		us2.setSex("男");
		us2.setPassword("234234");
		User2[] us=new User2[2];//创建用户对象数组;
		us[0]=us1;
		us[1]=us2;
		for (int i = 0; i < 10; i++) {
			System.out.println("1.登录 2.注册 3.输入0结束程序 ");
			User2 us3=new User2();
			Scanner n=new Scanner(System.in);
			int x=n.nextInt();
			if(x==1){//登录
				System.out.println("请输入用户名");
				String yhm=n.next();
				//us3.setUsername(yhm);
				us3.checkUs(yhm,us);
				System.out.println("请输入密码");
				String mm=n.next();
				//us3.setPassword(mm);
				us3.checkUs(mm,us);
				us3.checkPa(mm,us);				
				int flag3=us3.pd(0);
				if(flag3==1){
					System.out.println("4.显示用户信息 5.修改密码 ");
				}else{break;}
				int y=n.nextInt();
				if(y==4){//显示个人信息
					us3.show2(us);				
				}else if(y==5){//修改密码
					System.out.println("请输入用户名:");
					String yhm1=n.next();
					us3.checkUs(yhm1, us);
					us3.pd2();
					System.out.println("请输入原密码:");
					String mm1=n.next();
					us3.checkPa2(mm1, us);
					int pd4=us3.pd4(0);
					if(pd4==1){
						System.out.println("请输入新密码:");
						String mm2=n.next();
						us3.changemm(yhm1,mm2,us);
						us3.show2(us);
					}
				}
			}else if(x==2){
				System.out.println("请输入用户名");
				String yhmZc=n.next();
				boolean f0=us3.checkusernameRule(yhmZc);
				if(f0==false){
					System.out.println("用户名格式不正确!");
					break;
				}
				us3.checkUs(yhmZc,us);
				int flagZc=us3.pdZc(0);
				if(flagZc==0){
					System.out.println("请输入密码");
					String mmZc=n.next();
					boolean f=us3.checkPasswordRule(mmZc);
					//System.out.println(f);
					if(f==false){
						System.out.println("密码格式不正确!");
						break;
						}else{
							us=us3.register(us, yhmZc, mmZc);
						}
															
				}								
			}else if(x==0){System.out.println("结束程序"); break;}
		}	
	}
}

java多用户登录怎么用文本文件实现 java写一个登录_JAVA如何设置免登陆

 

java多用户登录怎么用文本文件实现 java写一个登录_用户名_02