集合方式添加学生信息

来源:互联网 发布:天融信数据库审计 编辑:程序博客网 时间:2024/05/12 23:52
package test;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.Scanner;public class Lianxi01 {    public static Scanner sc = new Scanner(System.in);    public static boolean b = true;    public static ArrayList list = new ArrayList();// 集合,在声明的时候不用确定长度,有多少存多少,集合中只能存放对象    public static void main(String[] args) {        while (b) {            System.out.println("1、添加学生;2、退出");            int xz = sc.nextInt();            if (xz == 1) {                Student stu = Lianxi01.addStu();                list.add(stu);                continue;            } else if (xz == 2) {                break;            }        }        // 遍历集合        if (list.size() == 0) {            System.out.println("没有添加一个学号,你要打印啥?");        } else {            System.out.println("---------------学生信息表---------------");            for (Object obj : list) {                if (obj instanceof Student) {                    Student stu = (Student) obj;                    System.out.println(stu);                }                System.out.println("--------------");            }        }    }    // 生成学号    public static String getID() {        return "zz_" + new SimpleDateFormat("yyyyMMddHHmmssSS").format(new Date());    }    // 添加学生    public static Student addStu() {        Student stu = new Student();        String id = Lianxi01.getID();        stu.setId(id);        System.out.println("学号:" + id);        while (b) {            System.out.println("请输入姓名:");            String name = sc.next();            if (name.matches("^[\u4e00-\u9fa5]{2,6}$")) {                stu.setName(name);                break;            } else {                System.out.println("名字为2-6位的汉字!");                continue;            }        }        while (b) {            System.out.println("请输入年龄:");            String age = sc.next();            if (age.matches("^[1][2-8]$")) {                stu.setAge(age);                break;            } else {                System.out.println("年龄必须为12-18岁!");                continue;            }        }        phone: while (b) {            System.out.println("请输入手机号码:");            String phone = sc.next();            if(list.size()==0){                if (phone.matches("^[1]([3]|[5]|[7]|[8])[0-9]{9}$")) {                    stu.setPhone(phone);                    System.out.println("添加成功");                    break;                } else {                    System.out.println("手机号应为11位的正常手机号码!");                    continue;                }            }else{                for (Object obj : list) {                    if(obj instanceof Student){                        Student s = (Student) obj;                        if(s.getPhone().equals(phone)){                            System.out.println("手机号已存在,请重新输入!");                            continue;                        }else{                            if (phone.matches("^[1]([3]|[5]|[7]|[8])[0-9]{9}$")) {                                stu.setPhone(phone);                                System.out.println("添加成功");                                break phone;                            } else {                                System.out.println("手机号应为11位的正常手机号码!");                                continue phone;                            }                        }                    }                }            }        }        while (b) {            System.out.println("请输入QQ号:");            String QQ = sc.next();            if (QQ.matches("^[1-9][0-9]{5,9}$")) {                stu.setQQ(QQ);                break;            } else {                System.out.println("QQ号码应为6-10数字号码!");                continue;            }        }        while (b) {            System.out.println("请输入邮箱:");            String mail = sc.next();            if (mail.matches("^([a-zA-Z0-9_]+)[@]([a-zA-Z0-9]+)[.](([c][o][m])|([c][n]))$")) {                stu.setMail(mail);                break;            } else {                System.out.println("邮箱因为.com或.cn结尾的正确邮箱地址!");                continue;            }        }        while (b) {            System.out.println("请输入爱好,以/隔开:");            String hobby = sc.next();            String[] hob = hobby.split("/");            if (hob.length > 0 && hob.length <= 3) {                stu.setHobby(hob);                break;            } else {                System.out.println("爱好数量为1-3!");                continue;            }        }        return stu;    }}
package test;import java.util.Arrays;public class Student {    private String id;    private String name;    private String age;    private String phone;    private String QQ;    private String mail;    private String[] hobby;    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getAge() {        return age;    }    public void setAge(String age) {        this.age = age;    }    public String getPhone() {        return phone;    }    public void setPhone(String phone) {        this.phone = phone;    }    public String getQQ() {        return QQ;    }    public void setQQ(String qQ) {        QQ = qQ;    }    public String getMail() {        return mail;    }    public void setMail(String mail) {        this.mail = mail;    }    public String[] getHobby() {        return hobby;    }    public void setHobby(String[] hobby) {        this.hobby = hobby;    }    @Override    public String toString() {        return "学生信息: \n学号:" + id + "\n姓名:" + name + "\n年龄:" + age + "\n手机号:" + phone + "\nQQ:" + QQ + "\n邮箱:"                + mail + "\n爱好:" + Arrays.toString(hobby);    }   }
0 0
原创粉丝点击