zoukankan      html  css  js  c++  java
  • Java 9天入门(黑马程序员) 课程收尾 学生管理系统 (9.13)

    1 实现功能

     2 结构概述

    分为两个包,各自一个类 Student.java 为学生类,目的是储存学生信息

    StudentManager.java 是主程序的代码

    3 Student.java 的代码如下

     可以快捷生成(eciplise--->右键代码区----->选择source----->选择 generate)

     1 package com.itheima.domain;
     2 
     3 public class Student {
     4     private String sid;//学生学号
     5     private String name;//学生姓名
     6     private int age;//学生年龄
     7     private String birthday;//学生生日
     8    //无参数构造方法
     9     public Student() {
    10         super();
    11     }
    12    //有参数构造方法
    13     public Student(String sid, String name, int age, String birthday) {
    14         super();
    15         this.sid = sid;
    16         this.name = name;
    17         this.age = age;
    18         this.birthday = birthday;
    19     }
    20 
    21     public String getSid() {
    22         return sid;
    23     }
    24 
    25     public void setSid(String sid) {
    26         this.sid = sid;
    27     }
    28 
    29     public String getName() {
    30         return name;
    31     }
    32 
    33     public void setName(String name) {
    34         this.name = name;
    35     }
    36 
    37     public int getAge() {
    38         return age;
    39     }
    40 
    41     public void setAge(int age) {
    42         this.age = age;
    43     }
    44 
    45     public String getBirthday() {
    46         return birthday;
    47     }
    48 
    49     public void setBirthday(String birthday) {
    50         this.birthday = birthday;
    51     }
    52 
    53 }

    4 StudentManager.java 代码如下 (各个功能以自定义方法实现)

     具体代码如下:

      1 package com.itheima.test;
      2 import java.util.ArrayList;
      3 import java.util.Scanner;
      4 
      5 import com.itheima.domain.Student;
      6 
      7 public class StudentManager {
      8 
      9     public static void main(String[] args) {
     10 
     11         // TODO Auto-generated method stub
     12         Scanner sc = new Scanner(System.in);
     13 //        创建集合容器对象
     14         ArrayList<Student> list = new ArrayList<Student>();
     15         lo: while (true) {
     16 //            lo 为循环标号 break lo;
     17 //            1、搭建主界面菜单
     18             System.out.println("--------------欢迎来到学生管理系统--------------");
     19             System.out.println("1 添加学生");
     20             System.out.println("2 删除学生");
     21             System.out.println("3 修改学生");
     22             System.out.println("4 查看学生");
     23             System.out.println("5 退出");
     24             System.out.println("请输入您的选择:");
     25             int sel = sc.nextInt();
     26             switch (sel) {
     27             case 1:
     28                 System.out.println("开始添加学生");
     29                 addStudent(list);
     30                 break;
     31             case 2:
     32                 System.out.println("删除学生");
     33                 deleteStudent(list);
     34                 break;
     35             case 3:
     36                 System.out.println("修改学生");
     37                 updateStudent(list);
     38                 break;
     39             case 4:
     40                 System.out.println("查看学生");
     41                 viewStudent(list);
     42                 break;
     43             case 5:
     44                 System.out.println("感谢您的使用");
     45                 break lo;
     46             default:
     47                 System.out.println("输入的数字不在我识别之内,请重新输入");
     48                 break;
     49             }
     50 
     51         }
     52     }
     53 
     54     // 修改学生的方法
     55     private static void updateStudent(ArrayList<Student> list) {
     56         // TODO Auto-generated method stub
     57         System.out.println("请输入您要修改的学号");
     58         Scanner sc = new Scanner(System.in);
     59         String updateSid = sc.next();
     60         int index = getIndex(list, updateSid);
     61         if (index == -1) {
     62             System.out.println("查无信息,请重新输入");
     63         } else {
     64 //            存在: 接受新的学生信息
     65             System.out.println("请输入新的学生姓名");
     66             String name = sc.next();
     67             System.out.println("请输入新的学生年龄");
     68             int age = sc.nextInt();
     69             System.out.println("请输入新的学生生日");
     70             String birthday = sc.next();
     71 //            封装为一个新的学生对象
     72             Student stu = new Student(updateSid, name, age, birthday);
     73 //            调用集合的set方法,完成修改
     74             list.set(index, stu);
     75             System.out.println("修改成功");
     76         }
     77 
     78     }
     79 
     80     // 删除学生的方法
     81     public static void deleteStudent(ArrayList<Student> list) {
     82         // TODO Auto-generated method stub
     83         System.out.println("请输入您要删除的学号");
     84         Scanner sc = new Scanner(System.in);
     85         String deleteSid = sc.next();
     86         int index = getIndex(list, deleteSid);
     87         if (index == -1) {
     88             System.out.println("查无信息,请重新输入");
     89         } else {
     90             list.remove(index);
     91             System.out.println("删除成功");
     92         }
     93     }
     94 
     95     // 查看学生的方法
     96     public static void viewStudent(ArrayList<Student> list) {
     97 
     98         // TODO Auto-generated method stub
     99 //        判断是否为空数据
    100         if (list.size() == 0) {
    101             System.out.println("无信息,请添加后重新查询");
    102             return;
    103         }
    104         System.out.println("学号\t姓名\t年龄\t生日");
    105         for (int i = 0; i < list.size(); i++) {
    106             Student stu = list.get(i);
    107             System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getBirthday());
    108         }
    109     }
    110 
    111 //    添加学生的方法
    112     public static void addStudent(ArrayList<Student> list) {
    113         Scanner sc = new Scanner(System.in);
    114         String sid;
    115         while (true) {
    116             System.out.println("请输入学号");
    117             sid = sc.next();
    118             int index = getIndex(list, sid);
    119             if (index == -1) {
    120 //                sid can use
    121                 break;
    122             }
    123         }
    124 //        1 给出录入的提示信息
    125 
    126         System.out.println("请输入姓名");
    127         String name = sc.next();
    128         System.out.println("请输入年龄");
    129         int age = sc.nextInt();
    130         System.out.println("请输入生日");
    131         String birthday = sc.next();
    132 //        2 将键盘录入的信息封装为学生对象
    133         Student stu = new Student(sid, name, age, birthday);
    134 //        3 将封装好的学生对象添加到集合容器中
    135         list.add(stu);
    136 //        4 给出添加成功的提示信息
    137         System.out.println("添加成功");
    138     }
    139 
    140     /*
    141      * 定义getIndex方法 : 接受一个集合对象,接受一个学生学号 查找这个学号在集合中出现的索引位置
    142      */
    143     public static int getIndex(ArrayList<Student> list, String sid) {
    144 //        假设传入的学号,在集合中不存在
    145         int index = -1;
    146 //        遍历集合,获取每一个学生对象,准备进行查找
    147         for (int i = 0; i < list.size(); i++) {
    148             Student stu = list.get(i);
    149             String id = stu.getSid();
    150 //            和传入的sid进行比对
    151             if (id.equals(sid)) {
    152 //            存在:让index变量记录正确的索引位置
    153                 index = i;
    154             }
    155 
    156         }
    157         return index;
    158     }
    159 
    160 }

    5 小结:

    这个项目融合了Java入门级别的知识,综合性强!(总计约210行代码)

                                  ------ BTLord 小白工作室

                                           2020.9.13

  • 相关阅读:
    观光奶牛Sightseeing Cows (二分+spfa(dfs))
    卢卡斯定理学习笔记
    分层图学习笔记
    热烈祝贺CRMEB运营中心乔迁新址
    CRMEB知识付费系统v1.2发布上线直播带课功能
    推荐一款超好用码云start超4.9k采用前后端分离开发的小程序商城源码
    crmeb打通版3.x小程序商城公众号重复无限刷新登陆解决
    如何利用CRMEB 3.2打通版小程序商城做直播带货
    crmeb打通版开源微信公众号小程序商城框架源码
    一文读懂知识付费平台的运营方向及平台选择
  • 原文地址:https://www.cnblogs.com/btlord/p/13662706.html
Copyright © 2011-2022 走看看