本编文章主要介绍一个关于集合的应用案例:完成班级学员录入功能
(没有持久化操作,每次重启录入的信息都保存不了)
一、需求:
创建学生类:添加以下属性以及相应的构造函数!
使用集合保存学员信息!
完成登录功能!
二、功能展示:
1.系统启动时,因系统内没有学生信息,所以需要注册之后才能使用

2.注册时判断用户输入信息是否正确



3.可以使用注册信息登录系统


三、业务代码:
1 import java.io.Serializable;
2
3 /**
4 * 学生类
5 * @author Administrator
6 *
7 */
8 public class Student implements Serializable {
9 /**
10 *
11 */
12 private static final long serialVersionUID = 1L;
13 private int stuId;//学号
14 private int pwd;//密码
15 private String name;//姓名
16 private int age;//年龄
17 private String birth;//生日
18 public int getStuId() {
19 return stuId;
20 }
21 public void setStuId(int stuId) {
22 this.stuId = stuId;
23 }
24 public int getPwd() {
25 return pwd;
26 }
27 public void setPwd(int pwd) {
28 this.pwd = pwd;
29 }
30 public String getName() {
31 return name;
32 }
33 public void setName(String name) {
34 = name;
35 }
36 public int getAge() {
37 return age;
38 }
39 public void setAge(int age) {
40 this.age = age;
41 }
42 public String getBirth() {
43 return birth;
44 }
45 public void setBirth(String birth) {
46 this.birth = birth;
47 }
48
49 }1 import java.io.DataInputStream;
2 import java.io.DataOutputStream;
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.Scanner;
6 import java.util.Set;
7
8 /**
9 * 学生信息管理类
10 * @author Administrator
11 *
12 */
13 public class StudentBiz{
14 //添加二进制输入输出流读写磁盘文件
15 DataOutputStream dos = null;
16 DataInputStream dis = null;
17 //使用Map集合保存学生信息
18 Map<Integer,Student> stuMap = new HashMap<Integer,Student>();
19 //添加学员信息
20 public void addStu(){
21 @SuppressWarnings("resource")
22 Scanner input = new Scanner(System.in);
23 try {
24 System.out.println("======注册======");
25 System.out.print("请输入学生账号:");
26 int id = input.nextInt();
27 System.out.print("请输入学生密码:");
28 int pwd = input.nextInt();
29 System.out.print("请输入学生姓名:");
30 String name = input.next();
31 System.out.print("请输入学生年龄:");
32 int age = input.nextInt();
33 System.out.print("请输入学生生日:");
34 String birth = input.next();
35 //判断信息是否正确
36 //Set<Integer> ids = stuMap.keySet();
37 if (stuMap.containsKey(id)) {
38 System.out.println("注册失败!该帐号已经存在!");
39 }else{
40 if (birth.indexOf("/")==4&&(birth.lastIndexOf("/")==6||birth.lastIndexOf("/")==7) ){
41 //拆分生日比较格式
42 String[] nums = new String[3];
43 nums = birth.split("/");
44 int num1 = Integer.parseInt(nums[0]);
45 int num2 = Integer.parseInt(nums[1]);
46 int num3 = Integer.parseInt(nums[2]);
47 if ((num1>1900&&num2<2018)&&(num2>0&&num2<=12)&&(num3>0&&num3<=31) ){
48 //录入信息
49 Student stu = new Student();
50 stu.setStuId(id);
51 stu.setPwd(pwd);
52 stu.setName(name);
53 stu.setAge(age);
54 stu.setBirth(birth);
55 stuMap.put(id, stu);
56 System.out.println("信息录入成功!");
57 }else{
58 System.out.println("您输入的日期格式不正确!");
59 }
60 }else{
61 System.out.println("您输入的日期格式不正确!");
62 }
63 }
64 } catch (Exception e) {
65 System.out.println("输入有误!");
66 }
67 }
68 //登录系统的方法
69 public void login(){
70 @SuppressWarnings("resource")
71 Scanner input = new Scanner(System.in);
72 //判断账号和密码是否匹配
73 //Set<Integer> stuId = stuMap.keySet();
74 if (stuMap.isEmpty()) {//判断学生信息是否为空,为空提示注册
75 System.out.println("不好意思还没有数据!");
76 }else{//查找验证学生信息
77 try {
78 System.out.println("======登陆======");
79 System.out.print("请输入学员账号:");
80 int id = input.nextInt();
81 System.out.print("请输入学员密码:");
82 int pwd = input.nextInt();
83 if (stuMap.containsKey(id)) {
84 if (stuMap.get(id).getPwd()==pwd) {
85 //登陆成功
86 System.out.println("登陆成功:\n您的姓名为:"+stuMap.get(id).getName());
87 System.out.println("年龄:"+stuMap.get(id).getAge()+"岁");
88 System.out.println("生日:"+stuMap.get(id).getBirth());
89 this.showInfo();
90 } else {
91 System.out.println("账号和密码不匹配!");
92 }
93 } else {
94 System.out.println("账号不存在!");
95 }
96 } catch (Exception e) {
97 System.out.println("输入有误!");
98 }
99 }
100
101 }
102 //显示学员信息的方法
103 public void showInfo(){
104 Set<Integer> stuId = stuMap.keySet();
105 System.out.println("======当前所有用户信息======");
106 for (Integer id : stuId) {
107 System.out.println("学员姓名:"+stuMap.get(id).getName());
108 System.out.println("账号:"+stuMap.get(id).getStuId());
109 System.out.println("年龄:"+stuMap.get(id).getAge());
110 System.out.println("生日:"+stuMap.get(id).getBirth());
111 }
112 }
113
114 }1 import java.util.Scanner;
2
3 /**
4 * 三、完成班级学员录入功能(没有持久化操作,每次重启录入的信息都保存不了)
5 创建学生类:添加以下属性以及相应的构造函数
6 使用集合保存学员信息!
7 完成登录功能
8 * @author Administrator
9 *
10 */
11 public class Test03 {
12
13 public static void main(String[] args) {
14 //Scanner接收数据
15 @SuppressWarnings("resource")
16 Scanner input = new Scanner(System.in);
17 StudentBiz stuBiz = new StudentBiz();
18 boolean flag = false;//
19 do{
20 System.out.println("------欢迎光临青鸟学院录入系统------");
21 System.out.println("\t1.登陆\t2.注册(输入其他键退出)");
22 System.out.print("请选择:");
23 try {
24 int num;
25 num = input.nextInt();
26 switch(num){
27 case 1:
28 //登陆
29 stuBiz.login();
30 break;
31 case 2:
32 //注册
33 stuBiz.addStu();
34 break;
35 default:
36 System.out.println("程序退出!");
37 flag = true;
38 break;
39 }
40 } catch (Exception e) {
41 System.out.println("输入有误,程序退出!");
42 flag = true;
43 }
44 }while(!flag);
45
46 }
47
48 }
















