User, username, password, sex,age --- , login(用户名和密码), register(用户名和密码),拓展 : changePwd(index,pwd)
使用数组作为容器,存储对象,User[] users = {user1, user2 ,user3};
程序初始化的时候,在容器存储2个对象,
输入用户名和密码:
请选择操作:1注册,2登录
成功之后:
1:个人中心: 展示自己的个人信息
2:修改密码
package html;
import java.util.*;
public class user {
//设置私有变量
private String username;
private String password;
private String sex;
private int age;
int result=-1;
public user(){}
public user(String username){}
//有参构造方法
public user(String username, String password, String sex, int age) {
//传过来的值不能直接给使用方法进行判断是否符合要求
set_username(username);
set_password(password);
set_sex(sex);
set_age(age);
}
//判断用户名在6到15位之间
public void set_username(String username)
{
if (username.length()>=6 && username.length()<15)
{
this.username = username;
}
}
//姓名的获取
public String get_username() {
return username;
}
//用户密码在6到15位
public void set_password(String password)
{
if (password.length()>=6 && password.length()<15)
{
this.password =password;
}
}
//密码的获取
public String get_password() {
return password;
}
//性别的输入,男或者女,都不是的话就是未知
public void set_sex(String sex)
{
if(sex.equals("男")||sex.equals("女"))
{
this.sex=sex;
}
else
{
System.out.println("性别不是男或女的话,默认值为未知。");
this.sex="未知";
}
}
//性别的获取
public String get_sex()
{
return sex;
}
//年龄的输入,区间在0~130之间,不是的话就是20
public void set_age(int age)
{
if(age<130&&age>0)
{
this.age=age;
}
else
{
System.out.println("年龄在0-130是合理的,超过这个范围给出默认值20。");
this.age=20;
}
}
//年龄的获取
public int get_age()
{
return age;
}
//展示用户信息接收一个用户名,使用for循环找到这个用户名来展示这个用户名下的所有信息
public void show(String username){
for (int i = 0; i <index.users.length; i++)
{
if (index.users[i].get_username().equals(username))
{
System.out.println("用户名是:"+index.users[i].get_username());
System.out.println("密码是:"+index.users[i].get_password());
System.out.println("性别是:"+index.users[i].get_sex());
System.out.println("年龄是:"+index.users[i].get_age());
break;
}
}
}
//登录的方法判断用户名和密码是否符合 返回true或者false 有一个正确的就可以退出循环 没有正确就循环结束
public boolean login(String username,String password,boolean aa) {
for (int i = 0; i < index.users.length; i++) {
if (index.users[i].get_username().equals(username) && index.users[i].get_password().equals(password)) {
aa= true;
break;
} else {
aa= false;
}
}
return aa;
}
//使用对象数组接收对象的方法,先判断对象数组最后一位是否有空位如果有就直接赋值
//如果没有就扩容数组,把对象存在对象数组最后一位
public void register(user user) {
if (index.users[index.users.length - 1] == null) {
index.users[index.users.length - 1] = user;
for (int i = 0; i < index.users.length; i++) {
System.out.println(index.users[i].get_username());
System.out.println(index.users[i].get_password());
System.out.println(index.users[i].get_sex());
System.out.println(index.users[i].get_age());
}
} else {
index.users = Arrays.copyOf(index.users, index.users.length + 1);
index.users[index.users.length - 1] = user;
for (int i = 0; i < index.users.length; i++) {
System.out.println(index.users[i].get_username());
System.out.println(index.users[i].get_password());
System.out.println(index.users[i].get_sex());
System.out.println(index.users[i].get_age());
}
}
}
//修改密码的方法 先通过用户名找到用户名下的密码判断和用户输入的是否一致
//如果一致返回true,不一致返回false
public boolean ChangePwd(String password1,String password,String NAME,boolean aa){
for (int i = 0; i <index.users.length; i++) {
if (index.users[i].get_username().equals(NAME)){
if (index.users[i].get_password().equals(password1)){
index.users[i].set_password(password);
aa =true;
}else {
aa =false;
}
}
}
return aa;
}
}
package html;
import java.util.*;
public class index {
public static user user=new user();
//创建一个静态对象数组并放两个对象进去
static user[] users={
new user("zhangsan","111111","男",18),
new user("chenli","222222","女",34)};
public static void main(String[] args) {
page();
}
public static void page() {
while(true)
{
Scanner s=new Scanner(System.in);
System.out.println("请选择:1、注册 2、登录 ");
if (s.nextInt()==1)
{
System.out.println("------注册------");
register();
break;
}
else
{
login();
break;
}
}
}
public static void register(){
//设置全局变量获取循环里输入的值
String sex1=null;
String username1 =null;
String password1 =null;
int age1 =0;
while (true)
{
Scanner username =new Scanner(System.in);
System.out.println("请输入用户名(6-15位):");
username1=username.next();
if (username1.length()>=6&&username1.length()<15){
break;
}
}
while (true)
{
Scanner password =new Scanner(System.in);
System.out.println("请输入密码(6-15位):");
password1=password.next();
if (password1.length()>=6&&password1.length()<15)
{
break;
}
}
while (true)
{
Scanner age =new Scanner(System.in);
System.out.println("请输入年龄(0-130):");
age1=age.nextInt();
if (age1<130&&age1>0)
{
break;
}
else
{
age1=20;
break;
}
}
while (true)
{
Scanner sex =new Scanner(System.in);
System.out.println("请输入性别(男或者女):");
sex1=sex.next();
if (sex1.equals("男")||sex1.equals("女")){
break;
}
else
{
sex1="未知";
break;
}
}
//把创建的对象存进对象数组
user.register(new user(username1,password1,sex1,age1));
//存进去之后运行第一个注册页面让用户选择登录或者再注册
page();
}
//创建登录方法
public static void login()
{
System.out.println("------登录------");
int num5;
Scanner m =new Scanner(System.in);
System.out.println("请输入用户名:");
String num1 =m.next();
Scanner num2 =new Scanner(System.in);
System.out.println("请输入密码:");
String num3 =m.next();
//把boolean传进方法里如果输入的用户名和密码都符合要求就传回来true,不符合就false
boolean aaa =true;
//创建一个新的boolean接收
boolean aa = user.login(num1,num3,aaa);
do {
//如果为true运行if
if (aa==true){
System.out.println("登录成功欢迎:"+num1);
while (true){
//设置循环让用户选择内容并判断用户输入的内容运行方法如果输入的不是提示的1,2,3就一直循环
System.out.println("1、展示个人信息;2、修改密码;3、退出");
Scanner p =new Scanner(System.in);
int pa1 =p.nextInt();
if (pa1==1){
//如果输入1,运行展示的方法
user.show(num1);
}
else if (pa1==2){
//如果输入2,运行修改密码的方法
ChangePwd(num1);
}
else if (pa1==3){
//如果输入3,退出循环到下面是否继续登录的循环可以选择继续登录
System.out.println("退出程序");
break;
}
}
}
//如果为false运行登录失败让用户输入是否继续登录
else
{
System.out.println("登录失败");
}
Scanner mm =new Scanner(System.in);
System.out.println("是否继续登录:1、是;其他、退出");
num5 =mm.nextInt();
//设置循环条件
}while (num5==1);
}
//创建修改密码的方法,这个方法接收一个用户名,也就是用户注册输入的用户名
// 把参数传到对象类的方法中判断返回一个true或者false
//使用if判断修改密码成功还是失败
public static void ChangePwd(String name)
{
System.out.println("------修改密码------");
Scanner num =new Scanner(System.in);
System.out.println("请输入原密码:");
String num1 =num.next();
Scanner num2 =new Scanner(System.in);
System.out.println("请输入新密码:");
String num3 =num.next();
boolean aaa =true;
boolean aa = user.ChangePwd(num1,num3,name,aaa);
if (aa==true)
{
System.out.println("修改成功"+num3);
}
else
{
System.out.println("修改失败");
}
}
}