本編文章主要介紹一個關於集合的應用案例:完成班級學員錄入功能
(沒有持久化操作,每次重啟錄入的信息都保存不了)
一、需求:
創建學生類:添加以下屬性以及相應的構造函數!
使用集合保存學員信息!
完成登錄功能!
二、功能展示:
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 this.name = 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 }