Java设计一个用户注册的程序

设计一个用户注册的程序,使用Scanner在控制台输入,字段有 用户名、密码、手机号、邮箱
要求:用户名不超过6位不能以数字开头,密码必须包含大写小写字母和数字6-8位,手机号为11位数字,邮箱必须符合邮箱格式。
使用面向对象的编程思想完成用户注册,可以持续操作,并把注册成功的用户保存。

package lianxi10;

public class User {

private String name;
private String pwd;
private String phone;
private String email;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getPwd() {
    return pwd;
}
public void setPwd(String pwd) {
    this.pwd = pwd;
}
public String getPhone() {
    return phone;
}
public void setPhone(String phone) {
    this.phone = phone;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
public User(String name, String pwd, String phone, String email) {
    super();
    this.name = name;
    this.pwd = pwd;
    this.phone = phone;
    this.email = email;
}
public User() {
    super();
    // TODO Auto-generated constructor stub
}
@Override
public String toString() {
    return "姓名:" + name + ", 密码:" + pwd + ", 手机号:" + phone + ", 邮箱:" + email;
}   

}

package lianxi10;

import java.util.ArrayList;
import java.util.Scanner;

public class Reg {

static ArrayList list = new ArrayList();

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);


    System.out.println("=============用户注册界面=============");

    a:while (true) {
        System.out.println("请选择:1.用户注册;2.查看用户;3.修改用户;4.删除用户;0.退出");
        int i = sc.nextInt();
        switch (i) {
        case 1:
            //注册信息
            User u = yanzheng(sc);
            list.add(u);
            break;
        case 2:
            if(list.size()==0){
                System.out.println("当前没有用户,请先注册!");
            }else{
                for (User user : list) {
                    System.out.println(user);
                }
            }
            break;
        case 3:
            setUser(sc);
            break;
        case 4:
            delUser(sc);
            break;
        case 0:
            break a;
        default:

        }
    }

    System.out.println("================退出系统,再见!===============");

}

/**
 * 删除用户
 */
public static void delUser(Scanner sc){
    System.out.println("请输入要删除的用户名:");
    String name = sc.next();
    //声明要修改的用户对象
    User u = null;
    //在集合中查询同名的对象
    for (User user : list) {
        if(user.getName().equals(name)){
            u = user;
        }
    }

    if(u==null){
        System.out.println("没有查询到该用户!");
        return;
    }

    list.remove(u);
    System.out.println("删除成功");
}

/**
 * 修改用户
 * @param sc
 */
public static void setUser(Scanner sc){
    System.out.println("请输入要修改的用户名:");
    String name = sc.next();
    //声明要修改的用户对象
    User u = null;
    //在集合中查询同名的对象
    for (User user : list) {
        if(user.getName().equals(name)){
            u = user;
        }
    }

    if(u==null){
        System.out.println("没有查询到该用户!");
        return;
    }

    User user = yanzheng(sc);

    list.set(list.indexOf(u), user);
    System.out.println("修改成功");

}

/**
 * 注册
 * @param sc
 * @return
 */
private static User yanzheng(Scanner sc) {

    User u = new User();

    //判断姓名
    while(true){
        System.out.println("请输入用户名:(不能以数字开头,不超过6位)");
        String name = sc.next();
        if(name.matches("^[^0-9]([0-9a-zA-Z]{0,5})$")){
            System.out.println("用户名通过");
            u.setName(name);
            break;
        }else{
            System.out.println("用户名不正确");
        }
    }

    //判断密码,必须包含大写、小写、数字
    while(true){
        System.out.println("请输入密码:,必须包含大写、小写、数字6-8位");
        String pwd = sc.next();
        if(pwd.matches("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])([a-zA-Z0-9]{6,8})$")){
            System.out.println("密码通过");
            u.setPwd(pwd);
            break;
        }else{
            System.out.println("密码不通过");
        }
    }

    //手机号
    while(true){
        System.out.println("请输入手机号:11位数字");
        String phone = sc.next();
        if(phone.matches("^([1])([3]|[5]|[7]|[8])([0-9]{9})$")){
            System.out.println("手机号通过");
            u.setPhone(phone);
            break;
        }else{
            System.out.println("手机号不通过");
        }
    }

    //邮箱
    while(true){
        System.out.println("请输入邮箱:");
        String email = sc.next();
        if(email.matches("^[0-9a-zA-Z_]+[@][a-zA-Z0-9]+[.](([c][o][m])|([c][n]))$")){
            System.out.println("邮箱通过");
            u.setEmail(email);
            break;
        }else{
            System.out.println("邮箱不通过");
        }
    }

    return u;

}

}

package lianxi10;

import java.util.ArrayList;

public class Test {

public static void main(String[] args) {

    User u = new User("tom","123","1306666","[email protected]");
    User u2 = new User("jack","123","1306666","[email protected]");
    Animal a = new Animal("狗");

    //使用泛型,是在编译期对代码的限制
    ArrayList list = new ArrayList();

    //添加
    list.add(u);
    list.add(u2);

// list.add(a);

    //取值,单个查询

// System.out.println(list.size());
// System.out.println(list.get(0));
// System.out.println(list.get(1));

    System.out.println("修改前:");

    //遍历集合,查询
    for (User user : list) {
        System.out.println(user);
    }

    System.out.println("======================");

    //修改
    list.set(1, new User("Lily","123","1306666","[email protected]"));

    System.out.println("修改后:");

    //遍历集合,查询
    for (User user : list) {
        System.out.println(user);
    }


    //删除
    list.remove(1);
    System.out.println("删除后:"+list.size());

}

}

你可能感兴趣的:(Java,java)