Java小例子(学习整理)-----学生管理系统-控制台版

 1、功能介绍:

     首先,这个小案例没有使用数据库,用集合的形式暂时保存数据,做测试!

     功能:

        增加学生信息

        删除学生信息

        修改学生信息

        查询学生信息:  按照学号(精确查询)  按照姓名(模糊查询)

        打印展示

        备份数据到磁盘

        退出      

2、代码块:(作为一个小案例,代码有点多了,我就不一一分析了,直接上代码!)

 

(1):Model层

----------学生实体类:

 1 package cn.jason.db;
 2 
 3 /**
 4  * 学生实体类
 5  *    Created by Jason  2016-7-15  上午8:36:20
 6  */
 7 public class StudentBean {
 8     
 9     private String sno;
10     private String name;
11     private String sex;
12     private int age;
13     private String address;
14     
15     public String getSno() {
16         return sno;
17     }
18     public void setSno(String sno) {
19         this.sno = sno;
20     }
21     public String getName() {
22         return name;
23     }
24     public void setName(String name) {
25         this.name = name;
26     }
27     public String getSex() {
28         return sex;
29     }
30     public void setSex(String sex) {
31         this.sex = sex;
32     }
33     public int getAge() {
34         return age;
35     }
36     public void setAge(int age) {
37         this.age = age;
38     }
39     public String getAddress() {
40         return address;
41     }
42     public void setAddress(String address) {
43         this.address = address;
44     }
45     
46     
47     
48 }

----------存放学生实体数据的仓库集合:

 1 package cn.jason.db;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 /**
 7  * 学生数据存放集合
 8  *    Created by Jason  2016-7-15  上午8:38:50
 9  */
10 public abstract class DBUtils {
11 
12     private static List<StudentBean> list=null;
13     
14     static{
15         list=new ArrayList<StudentBean>();
16     }
17     
18     private DBUtils(){}
19 
20     public static List<StudentBean> getList() {
21         return list;
22     }
23 }

----------数据操作(接口)定义规则:

 1 package cn.jason.daos;
 2 
 3 import java.util.List;
 4 
 5 import cn.jason.db.StudentBean;
 6 
 7 
 8 /**
 9  * 定义数据操作规则
10  *    Created by Jason  2016-7-15  上午8:43:55
11  * @param <StudentBean>
12  */
13 public interface StudentDBDAOInf {
14 
15     //查询所有
16     List<StudentBean> getAllStudent(); 
17     //学号 精确查找
18     List<StudentBean> getStudentBySno(String sno);
19     //姓名 模糊查找
20     List<StudentBean> getStudentByName(String name);
21     
22     
23     //判断非空性
24     boolean isEmpty();
25     
26     //新增
27     boolean insertStudentDB(StudentBean bean);
28     
29     //删除
30     boolean deleteStudentDB(StudentBean bean);
31     
32     //修改
33     boolean updateStudentDB(StudentBean bean);
34     
35 }

----------数据操作(接口实现子类)实现规则:

 1 package cn.jason.daos;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import cn.jason.db.DBUtils;
 7 import cn.jason.db.StudentBean;
 8 
 9 /**
10  * 数据操作实现子类
11  *    Created by Jason  2016-7-15  上午8:55:08
12  */
13 public class StudentDBDAOImpl implements StudentDBDAOInf {
14 
15     private static List<StudentBean> list=null;
16     static{
17         list=DBUtils.getList();
18     }
19     
20     
21     /**
22      * 得到所有信息
23      */
24     public List<StudentBean> getAllStudent() {        
25         return new ArrayList<StudentBean>(list);
26     }
27 
28     /**
29      * 根据学号查找
30      */
31     public List<StudentBean> getStudentBySno(String sno) {
32         ArrayList<StudentBean> rList=new ArrayList<StudentBean>();        
33         for (StudentBean s : list) {
34             if(s.getSno().equals(sno)){
35                 rList.add(s);
36             }
37         }        
38         return rList;
39     }
40 
41     /**
42      * 根据姓名查找
43      */
44     public List<StudentBean> getStudentByName(String name) {
45         ArrayList<StudentBean> rList=new ArrayList<StudentBean>();        
46         for (StudentBean s : list) {
47             if(s.getName().indexOf(name)!=-1){
48                 rList.add(s);
49             }
50         }        
51         return rList;
52     }
53 
54     /**
55      * 判断是否为空
56      */
57     public boolean isEmpty() {        
58         return list.isEmpty();
59     }
60 
61     /**
62      * 新增数据
63      */
64     public boolean insertStudentDB(StudentBean bean) {        
65         return list.add(bean);
66     }
67 
68     /**
69      * 删除操作:按学号删
70      */
71     public boolean deleteStudentDB(StudentBean bean) {
72         for ( StudentBean  s: list) {
73             if(s.getSno().equals(bean.getSno())){
74                 return list.remove(s);
75             }
76         }
77         return false;
78     }
79 
80     /**
81      * 修改操作
82      */
83     public boolean updateStudentDB(StudentBean bean) {
84         boolean bool=false;
85         int i=0;
86         for (StudentBean s : list) {
87             if(s.getSno().equals(bean.getSno())){
88                 list.set(i, bean);
89                 bool=true;
90                 break;
91             }
92             i++;
93         }        
94         return bool;
95     }
96 
97 }
View Code

(2): Control层

----------逻辑控制操作(接口)控制规则:

 1 package cn.jason.services;
 2 
 3 import java.util.List;
 4 
 5 import cn.jason.db.StudentBean;
 6 
 7 /**
 8  * 逻辑控制层的借口
 9  *    Created by Jason  2016-7-15  上午8:51:43
10  */
11 public interface StudentServiceInf {
12 
13     //增删查改操作
14     
15     boolean insertStudent();
16     
17     boolean deleteStudent();
18     
19     boolean updateStudent();
20     
21     List<StudentBean> searchStudent(int index);//查询模式  1、姓名   2、学号
22     
23     
24 }

----------逻辑控制操作(接口实现子类)实现控制规则:

  1 package cn.jason.services;
  2 
  3 import java.io.BufferedOutputStream;
  4 import java.io.BufferedReader;
  5 import java.io.File;
  6 import java.io.FileNotFoundException;
  7 import java.io.FileOutputStream;
  8 import java.io.IOException;
  9 import java.text.SimpleDateFormat;
 10 import java.util.List;
 11 
 12 import cn.jason.daos.StudentDBDAOImpl;
 13 import cn.jason.db.StudentBean;
 14 
 15 /**
 16  * 逻辑控制实现子类
 17  *    Created by Jason  2016-7-15  上午9:19:37
 18  */
 19 public class StudentServiceImpl implements StudentServiceInf {
 20 
 21     private StudentDBDAOImpl dao=null;
 22     private BufferedReader br=null ;
 23     public StudentServiceImpl(BufferedReader br){
 24         dao=new StudentDBDAOImpl();
 25         this.br=br;
 26     }
 27     
 28     /**
 29      * 数据显示处理
 30      */
 31     private StudentBean getData(){
 32         
 33         StudentBean student=null;
 34         try {
 35             System.out.println("请输入姓名:");
 36             String name=br.readLine();
 37             System.out.println("请输入性别:");
 38             String sex=br.readLine();
 39             if("".equals(sex)||"".equals(sex)){
 40                 
 41             }else{
 42                 System.out.println("性别输入有误!");
 43                 return student;
 44             }
 45             System.out.println("请输入年龄:");
 46             String age=br.readLine();
 47             int sage=0;
 48             if(Integer.parseInt(age)<0 || Integer.parseInt(age)>100){
 49                 System.out.println("请设置年龄区段为:0-100 岁");
 50                 return student;
 51             }else{
 52                 sage=Integer.parseInt(age);
 53             }
 54             System.out.println("请输入地址:");
 55             String address=br.readLine();
 56             
 57             student=new StudentBean();
 58             student.setName(name);
 59             student.setSex(sex);
 60             student.setAge(sage);
 61             student.setAddress(address);
 62             
 63         } catch (IOException e) {
 64             e.printStackTrace();
 65         }
 66         return student;
 67     } 
 68     
 69     /**
 70      * 新增操作    
 71      */
 72     public boolean insertStudent() {
 73         boolean bool=false;        
 74         try {
 75             System.out.println("请输入学号:");
 76             String sno=br.readLine();
 77             if(dao.getStudentBySno(sno).size()>0){
 78                 System.out.println("当前学号已存在,不能做新增操作!");
 79                 return false;
 80             }
 81             StudentBean bean=getData();
 82             if(bean==null){
 83                 return bool;
 84             }
 85             bean.setSno(sno);
 86             bool= dao.insertStudentDB(bean);
 87             
 88         } catch (IOException e) {
 89             e.printStackTrace();
 90             return false;
 91         }        
 92         return bool;
 93     }
 94 
 95     /**
 96      * 删除操作
 97      */
 98     public boolean deleteStudent() {
 99         boolean bool=false;
100         System.out.println("请输入要删除的学生学号");        
101         try {
102             String sno = br.readLine();
103             StudentBean bean=null;
104             if(dao.getStudentBySno(sno).size()==0){
105                 System.err.println("没有找到这个学生,不能删除!");
106             }else{
107                 bean=new StudentBean();
108                 bean.setSno(sno);
109                 bool=dao.deleteStudentDB(bean);
110             }
111         } catch (IOException e) {
112             e.printStackTrace();
113         }        
114         
115         return bool;
116     }
117 
118     /**
119      * 修改操作
120      */
121     public boolean updateStudent() {
122         boolean bool=false;        
123         try {
124             System.out.println("请输入要修改的学生学号:");
125             String sno=br.readLine();
126             if(dao.getStudentBySno(sno).size()==0){
127                 System.out.println("当前学号不存在,不能做修改操作!");
128                 return false;
129             }
130             StudentBean bean=getData();
131             if(bean==null){
132                 return bool;
133             }
134             bean.setSno(sno);
135             bool= dao.updateStudentDB(bean);
136             
137         } catch (IOException e) {
138             e.printStackTrace();
139             return false;
140         }        
141         return bool;
142     }
143 
144     /**
145      * 查询操作
146      */
147     public List<StudentBean> searchStudent(int index) {
148         List<StudentBean> rlist=null;
149         switch (index) {//1、姓名   2、学号
150             case 1:
151                 rlist=searchByName();
152                 break;
153             case 2:
154                 rlist=searchBySno();
155                 break;
156         
157         }
158         return rlist;
159     }
160 
161     /**
162      *按学号查(精确查)
163      */
164     private List<StudentBean> searchBySno() {
165         List<StudentBean> rList=null;        
166         try {
167             System.out.println("请输入学号:");
168             String sno=br.readLine();
169             rList=dao.getStudentBySno(sno);
170             PrintData(rList);
171         } catch (IOException e) {
172             System.err.println("输入学号处发生错误");
173             return rList;
174         }
175         return rList;        
176     }
177 
178     /**
179      *按姓名查(模糊查)
180      */
181     private List<StudentBean> searchByName() {
182         List<StudentBean> rList=null;        
183         try {
184             System.out.println("请输入姓名:");
185             String sname=br.readLine();
186             rList=dao.getStudentByName(sname);
187             PrintData(rList);
188         } catch (IOException e) {
189             System.err.println("输入姓名处发生错误");
190             return rList;
191         }
192         return rList;
193     }
194 
195     /**
196      * 显示查询的数据
197      */
198     private void PrintData(List<StudentBean> rList){
199         System.out.println("姓名	学号	性别	年龄	地址");
200         for (StudentBean s : rList) {
201             System.out.println(s.getName()+"	"+s.getSno()+"	"+s.getSex()+"	"+s.getAge()+"	"+s.getAddress());
202         }
203     }
204     
205     /**
206      * 打印所有信息
207      */
208     public void ShowData(){
209         System.out.println("姓名	学号	性别	年龄	地址");
210         for (StudentBean s : dao.getAllStudent()) {
211             System.out.println(s.getName()+"	"+s.getSno()+"	"+s.getSex()+"	"+s.getAge()+"	"+s.getAddress());
212         }
213     }
214     
215     /**
216      * 备份数据
217      */
218     public boolean BakData(){
219         
220         boolean bool=false;
221         BufferedOutputStream bos=null;
222         try{
223             File file=new File("testData.bak");            
224             FileOutputStream fos = null;
225             try {
226                 fos = new FileOutputStream(file,true);
227             } catch (FileNotFoundException e) {
228                 e.printStackTrace();
229             }
230              bos=new BufferedOutputStream(fos);
231             
232             SimpleDateFormat sdf=new SimpleDateFormat("yyyy年-MM月-dd日  HH:mm:ss");
233             String date=sdf.format(System.currentTimeMillis());
234             bos.write(date.getBytes());
235             bos.write("姓名	学号	性别	年龄	地址".getBytes());
236             for (StudentBean s : dao.getAllStudent()) {
237                 bos.write((s.getName()+"	"+s.getSno()+"	"+s.getSex()+"	"+s.getAge()+"	"+s.getAddress()).getBytes());
238             }
239             bool=true;
240         }catch(IOException e){
241             return false;
242         }    
243         finally{
244             if(bos!=null){
245                 try {
246                     bos.close();
247                 } catch (IOException e) {
248                     e.printStackTrace();
249                 }
250             }            
251         }
252         return bool;
253     }
254     
255     
256 
257 }
View Code

(3): Views层

----------视图界面(在控制台展示)

  1 package cn.jason.views;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.IOException;
  5 import java.io.InputStreamReader;
  6 
  7 import com.sun.org.apache.xml.internal.security.Init;
  8 
  9 import cn.jason.services.StudentServiceImpl;
 10 
 11 /**
 12  * 控制台显示效果
 13  *    Created by Jason  2016-7-15  上午10:36:36
 14  */
 15 public class MainView {
 16 
 17     private StudentServiceImpl control=null;
 18     private BufferedReader br=null ;
 19     
 20     public MainView(){
 21         br=new BufferedReader(new InputStreamReader(System.in));
 22         control=new StudentServiceImpl(br);
 23         
 24         //初始化界面
 25         init();
 26     }
 27 
 28     /**
 29      *初始化界面
 30      */
 31     private void init() {
 32         System.out.println("学生关系系统测试版");
 33         boolean bool=true;
 34         while(bool){
 35             System.out.println("信息操作指令:0、删除  1、新增  2、修改   3、查询    4、打印     5、备份   6、退出");
 36             String order;
 37             try {
 38                 order = br.readLine();
 39                 if("0".equals(order)){
 40                     if(control.deleteStudent()){
 41                         System.out.println("删除成功!");
 42                     }else{
 43                         System.out.println("删除失败!");
 44                     }
 45                 }else if("1".equals(order)){
 46                     if(control.insertStudent()){
 47                         System.out.println("新增成功!");
 48                     }else{
 49                         System.out.println("新增失败!");
 50                     }
 51                 }else if("2".equals(order)){
 52                     if(control.updateStudent()){
 53                         System.out.println("修改成功!");
 54                     }else{
 55                         System.out.println("修改失败!");
 56                     }
 57                 }else if("3".equals(order)){
 58                     checkKindSearch();
 59                 }else if("4".equals(order)){
 60                     control.ShowData();
 61                 }else if("5".equals(order)){
 62                     if(control.BakData()){
 63                         System.out.println("打印完成,请注意查看!");
 64                     }else{
 65                         System.out.println("打印操作失败!");
 66                     }
 67                 }else if("6".equals(order)){
 68                     
 69                         if(br!=null){
 70                             try {
 71                                 br.close();
 72                             } catch (IOException e) {
 73                                 e.printStackTrace();
 74                             }
 75                         }                    
 76                     System.exit(0);
 77                 }else{
 78                     System.out.println("抱歉!请输入正确的操作指令!");
 79                 }
 80             } catch (IOException e) {
 81                 e.printStackTrace();
 82             }
 83         }
 84         
 85     }
 86 
 87     /**
 88      *查询细分类
 89      */
 90     private void checkKindSearch() {
 91         
 92         try {
 93             System.out.println("查询条件:1、姓名     2、学号");
 94             String index=br.readLine();
 95             if("1".equals(index)){
 96                 control.searchStudent(1);
 97             }else if("2".equals(index)){
 98                 control.searchStudent(2);
 99             }else{
100                 System.err.println("输入有误,请核对后再输入");
101             }
102             
103         } catch (IOException e) {
104             e.printStackTrace();
105         }
106         
107     }
108 }
View Code

(4): 启动程序:main

 1 package cn.jason.start;
 2 
 3 import cn.jason.views.MainView;
 4 
 5 /**
 6  * 程序启动类:
 7  *    Created by Jason  2016-7-15  上午10:50:49
 8  */
 9 public class Start {
10 
11     
12     public static void main(String[] args) {
13         new MainView();
14     }
15 
16 }

3、运行测试结果:

  1 学生关系系统测试版
  2 信息操作指令:0、删除  1、新增  2、修改   3、查询    4、打印     5、备份   6、退出
  3 1
  4 请输入学号:
  5 s001
  6 请输入姓名:
  7 Jason
  8 请输入性别:
  9  10 请输入年龄:
 11 20
 12 请输入地址:
 13 长沙市 岳麓区
 14 新增成功!
 15 信息操作指令:0、删除  1、新增  2、修改   3、查询    4、打印     5、备份   6、退出
 16 1
 17 请输入学号:
 18 s002
 19 请输入姓名:
 20 Jask
 21 请输入性别:
 22  23 请输入年龄:
 24 20
 25 请输入地址:
 26 北京
 27 新增成功!
 28 信息操作指令:0、删除  1、新增  2、修改   3、查询    4、打印     5、备份   6、退出
 29 1
 30 请输入学号:
 31 s003
 32 请输入姓名:
 33 Rose
 34 请输入性别:
 35  36 请输入年龄:
 37 20
 38 请输入地址:
 39 纽约
 40 新增成功!
 41 信息操作指令:0、删除  1、新增  2、修改   3、查询    4、打印     5、备份   6、退出
 42 4
 43 姓名    学号    性别    年龄    地址
 44 Jason    s001    男    20    长沙市 岳麓区
 45 Jask    s002    男    20    北京
 46 Rose    s003    女    20    纽约
 47 信息操作指令:0、删除  1、新增  2、修改   3、查询    4、打印     5、备份   6、退出
 48 3
 49 查询条件:1、姓名     2、学号
 50 1
 51 请输入姓名:
 52 Jason
 53 姓名    学号    性别    年龄    地址
 54 Jason    s001    男    20    长沙市 岳麓区
 55 信息操作指令:0、删除  1、新增  2、修改   3、查询    4、打印     5、备份   6、退出
 56 3
 57 查询条件:1、姓名     2、学号
 58 2
 59 请输入学号:
 60 s002
 61 姓名    学号    性别    年龄    地址
 62 Jask    s002    男    20    北京
 63 信息操作指令:0、删除  1、新增  2、修改   3、查询    4、打印     5、备份   6、退出
 64 5
 65 打印完成,请注意查看!
 66 信息操作指令:0、删除  1、新增  2、修改   3、查询    4、打印     5、备份   6、退出
 67 2
 68 请输入要修改的学生学号:
 69 s002
 70 请输入姓名:
 71 Jack_2
 72 请输入性别:
 73  74 请输入年龄:
 75 20
 76 请输入地址:
 77 暂无详细住址信息
 78 修改成功!
 79 信息操作指令:0、删除  1、新增  2、修改   3、查询    4、打印     5、备份   6、退出
 80 4
 81 姓名    学号    性别    年龄    地址
 82 Jason    s001    男    20    长沙市 岳麓区
 83 Jack_2    s002    女    20    暂无详细住址信息
 84 Rose    s003    女    20    纽约
 85 信息操作指令:0、删除  1、新增  2、修改   3、查询    4、打印     5、备份   6、退出
 86 1
 87 请输入学号:
 88 s004
 89 请输入姓名:
 90 测试人物
 91 请输入性别:
 92  93 请输入年龄:
 94 20
 95 请输入地址:
 96 暂无
 97 新增成功!
 98 信息操作指令:0、删除  1、新增  2、修改   3、查询    4、打印     5、备份   6、退出
 99 0
100 请输入要删除的学生学号
101 s005
102 没有找到这个学生,不能删除!
103 删除失败!
104 信息操作指令:0、删除  1、新增  2、修改   3、查询    4、打印     5、备份   6、退出
105 4
106 姓名    学号    性别    年龄    地址
107 Jason    s001    男    20    长沙市 岳麓区
108 Jack_2    s002    女    20    暂无详细住址信息
109 Rose    s003    女    20    纽约
110 测试人物    s004    男    20    暂无
111 信息操作指令:0、删除  1、新增  2、修改   3、查询    4、打印     5、备份   6、退出
112 0
113 请输入要删除的学生学号
114 s004
115 删除成功!
116 信息操作指令:0、删除  1、新增  2、修改   3、查询    4、打印     5、备份   6、退出
117 4
118 姓名    学号    性别    年龄    地址
119 Jason    s001    男    20    长沙市 岳麓区
120 Jack_2    s002    女    20    暂无详细住址信息
121 Rose    s003    女    20    纽约
122 信息操作指令:0、删除  1、新增  2、修改   3、查询    4、打印     5、备份   6、退出
View Code
原文地址:https://www.cnblogs.com/newwind/p/5673025.html