zoukankan      html  css  js  c++  java
  • 学生增删改查练习

      1 package day11;
      2 
      3 import java.util.ArrayList;
      4 import java.util.Scanner;
      5 
      6 public class Test {
      7     public static void main(String[] args) {
      8         Scanner sc = new Scanner(System.in);
      9         ArrayList<Student> list = new ArrayList<>();
     10         while (true) {
     11             System.out.println("------欢迎来到学生管理系统--------");
     12             System.out.println("1 添加学生");
     13             System.out.println("2 删除学生");
     14             System.out.println("3 修改学生");
     15             System.out.println("4 查看学生");
     16             System.out.println("5 退出");
     17             System.out.println("请输入您的选择:");
     18             int choice = sc.nextInt();
     19             switch (choice) {
     20                 case 1:
     21                     addStudent(list);
     22                     break;
     23                 case 2:
     24                     deleteStudent(list);
     25                     break;
     26                 case 3:
     27                     updateStudent(list);
     28                     break;
     29                 case 4:
     30                     queryStudent(list);
     31                     break;
     32                 case 5:
     33                     System.out.println("感谢您的使用");
     34                     break;
     35                 default:
     36                     System.out.println("输入的数据有误!");
     37                     break;
     38             }
     39         }
     40     }
     41 
     42     private static void addStudent(ArrayList<Student> list) {
     43         Scanner sc = new Scanner(System.in);
     44         String sid;
     45         while (true) {
     46             System.out.println("请输入学号:");
     47             sid = sc.nextLine();
     48             int index = getIndex(list, sid);
     49             if (index == -1) {
     50                 break;
     51             } else {
     52                 System.out.println("学号已被占用");
     53             }
     54         }
     55         System.out.println("请输入姓名:");
     56         String name = sc.nextLine();
     57         System.out.println("请输入年龄:");
     58         String age = sc.nextLine();
     59         System.out.println("请输入生日:");
     60         String birthday = sc.nextLine();
     61         Student stu = new Student(sid, name, age, birthday);
     62         list.add(stu);
     63         System.out.println("添加学生成功");
     64     }
     65 
     66     private static void deleteStudent(ArrayList<Student> list) {
     67         Scanner sc = new Scanner(System.in);
     68         System.out.println("请输入要删除的学号");
     69         String delSid = sc.nextLine();
     70         int index = getIndex(list, delSid);
     71         if (index == -1) {
     72             System.out.println("该信息不存在,请重新输入");
     73         } else {
     74             list.remove(index);
     75             System.out.println("删除成功!");
     76         }
     77     }
     78 
     79     private static void updateStudent(ArrayList<Student> list) {
     80         Scanner sc = new Scanner(System.in);
     81         System.out.println("请输入你要修改的学号:");
     82         String updateSid = sc.nextLine();
     83         int index = getIndex(list, updateSid);
     84         if (index == -1) {
     85             System.out.println("输入的信息不存在,请重入");
     86         } else {
     87             System.out.println("请输入新的姓名:");
     88             String name = sc.nextLine();
     89             System.out.println("请输入新的年龄:");
     90             String age = sc.nextLine();
     91             System.out.println("请输入新的生日:");
     92             String birthday = sc.nextLine();
     93             Student stu = new Student(updateSid, name, age, birthday);
     94             list.set(index, stu);
     95             System.out.println("修改成功");
     96         }
     97     }
     98 
     99     private static int getIndex(ArrayList<Student> list, String sid) {
    100         int index = -1;
    101         for (int i = 0; i < list.size(); i++) {
    102             Student stu = list.get(i);
    103             if (sid.equals(stu.getSid())) {
    104                 index = i;
    105             }
    106         }
    107         return index;
    108     }
    109 
    110     private static void queryStudent(ArrayList<Student> list) {
    111         if (list == null || list.size() == 0) {
    112             System.out.println("无此学生信息,请添加后重试");
    113         } else {
    114             System.out.println("学号		姓名		年龄		生日");
    115             for (int i = 0; i < list.size(); i++) {
    116                 Student stu = list.get(i);
    117                 System.out.println(stu.getSid() + "		" + stu.getName() + "	" + stu.getAge() + "		" + stu.getBirthday());
    118             }
    119         }
    120     }
    121 }

    执行结果1:

     执行结果2:

     执行结果3:

     执行结果:

     【Student.java】

     1 package day11;
     2 
     3 public class Student {
     4 private String sid;
     5 private String name;
     6 private String age;
     7 private String birthday;
     8 
     9     public Student(String sid, String name, String age, String birthday) {
    10         this.sid = sid;
    11         this.name = name;
    12         this.age = age;
    13         this.birthday = birthday;
    14     }
    15 
    16     public String getBirthday() {
    17         return birthday;
    18     }
    19 
    20     public void setBirthday(String birthday) {
    21         this.birthday = birthday;
    22     }
    23 
    24     public String getAge() {
    25         return age;
    26     }
    27 
    28     public void setAge(String age) {
    29         this.age = age;
    30     }
    31 
    32     public String getName() {
    33         return name;
    34     }
    35 
    36     public void setName(String name) {
    37         this.name = name;
    38     }
    39 
    40     public String getSid() {
    41         return sid;
    42     }
    43 
    44     public void setSid(String sid) {
    45         this.sid = sid;
    46     }
    47 }
    欢迎批评指正,提出问题,谢谢!
  • 相关阅读:
    App更新之dialog数字进度条
    Android app启动是出现白屏或者黑屏如何解决?
    Tensorflow报错:AttributeError: module 'tensorflow._api.v1.io' has no attribute 'gfile'
    《Python深度学习》第三章阅读笔记
    在Ubuntu 18.04上配置CPU深度学习环境
    《Python深度学习》第二章阅读笔记
    《Python深度学习》第一章阅读笔记
    POJ 1118 Lining Up
    蓝桥杯-地宫取宝(动态规划)
    洛谷P2280[HNOI2003] 激光炸弹(二维前缀和)
  • 原文地址:https://www.cnblogs.com/xxeleanor/p/14284683.html
Copyright © 2011-2022 走看看