学生管理系统

学生管理系统

package com.itheima.test;



public class Student {

  private String id;

  private String name;

  private int age;

  private String birthday;





  public Student() {

 }



  public Student(String id, String name, int age, String birthday) {

    this.id = id;

    this.name = name;

    this.age = age;

    this.birthday = birthday;

 }



  /**

  \* 获取

  *

  \* @return id

  */

  public String getId() {

    return id;

 }



  /**

  \* 设置

  *

  \* @param id

  */

  public void setId(String id) {

    this.id = id;

 }



  /**

  \* 获取

  *

  \* @return name

  */

  public String getName() {

    return name;

 }



  /**

  \* 设置

  *

  \* @param name

  */

  public void setName(String name) {

    this.name = name;

 }



  /**

  \* 获取

  *

  \* @return age

  */

  public int getAge() {

    return age;

 }



  /**

  \* 设置

  *

  \* @param age

  */

  public void setAge(int age) {

    this.age = age;

 }



  /**

  \* 获取

  *

  \* @return birthday

  */

  public String getBirthday() {

    return birthday;

 }



  /**

  \* 设置

  *

  \* @param birthday

  */

  public void setBirthday(String birthday) {

    this.birthday = birthday;

 }



}

package com.itheima.test;



import java.util.ArrayList;

import java.util.Scanner;



public class Test {

  public static void main(String[] args) {

    ArrayList<Student> list = new ArrayList<>();

    Student stu1 = new Student("heima001", "张三", 23, "1999-11-11");

    Student stu2 = new Student("heima002", "张四", 24, "1989-11-11");

    Student stu3 = new Student("heima003", "张五", 26, "1936-11-11");

    list.add(stu1);

    list.add(stu2);

    list.add(stu3);

    Scanner sc = new Scanner(System.in);



    // ctrl + alt + T

    while (true) {

      System.out.println("--------欢迎来到学生管理系统--------");

      System.out.println("1 添加学生");

      System.out.println("2 删除学生");

      System.out.println("3 修改学生");
      
      System.out.println("4 查看学生");

      System.out.println("5 退出");

      System.out.println("请输入您的选择:");



      int choice = sc.nextInt();



      switch (choice) {

        case 1:

          System.out.println("添加学生...");

          break;

        case 2:

          deletestudentById(list);

          break;

        case 3:

          updateStudentInById(list);

          break;

        case 4:

          querystudentinfor(list);

          break;

        case 5:

          System.out.println("感谢您的使用, 再见!");

          System.exit(0);   // 终止正在运行的JVM虚拟机

          break;

        default:

          System.out.println("您的输入有误!");

          break;

     }

   }



 }



  /**

  \* 此方法根据学号,修改学生信息

  */

  private static void updateStudentInById(ArrayList<Student> list) {

    //1.

    Scanner sc = new Scanner(System.in);

    System.out.println("请输入要修改的学生学号");

    String updatId = sc.next();

    //2.

    int index = getIndex(list,updatId);

    //3.

    if (index == -1 ){

      //4.

      System.out.println("查无此人,修改失败");

   }else{

      //5.

      System.out.println("请输入学生姓名");

      String name = sc.next();

      System.out.println("请输入学生年龄");

      System.out.println("请输入学生生日");

      String birthday = sc.next();

      //6.

      Student stu = new Student(updatId,name,age,birthday);

      //7.

      list.set(index,stu);

      System.out.println("修改成功");

   }



 }



  /**

  \* 此方法根据学号,修改学生信息

  */





  /**

  \* 此方法根据学号删除集合中的学生

  */

  private static void deletestudentById(ArrayList<Student> list) {

    //1.键盘输入要删除的学号

    Scanner sc = new Scanner(System.in);

    System.out.println("请输入您要删除的学号");

    String deleted = sc.next();

    //2.调用getindex方法,查找学号在集合的索引

    int index = getIndex(list, deleted);

    if (index == -1) {

      //3.判断是-1,说明学号不存在,给出提示

      System.out.println("查无此人,删除失败");

   } else {

      //4.如果不是-1,学号纯在,调佣集合的remove方法删除

      list.remove(list);

      System.out.println("删除成功");

   }

 }



  /**

  \* 此方法用于查看学生

  */

  private static void querystudentinfor(ArrayList<Student> list) {

    if (list.size() == 0) {

      System.out.println("查无信息,请稍后再试");

   } else {

      for (int i = 0; i < list.size(); i++) {

        Student stu = list.get(i);

        System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getBirthday());

     }

   }

 }



  /**

  \* 此方法查找学号索引位置

  */

  private static int getIndex(ArrayList<Student> list, String id) {

    //假设学号在集合中不存在

    int index = -1;

    //遍历数组:目的是为了获取每个学生对象

    for (int i = 0; i < list.size(); i++) {

      Student stu = list.get(i);

      //stu.getId():从集合中取出的学号

      //id:要查找的学号

      if (id.equals(stu.getId())) {

        //找到了,让正确的ID记录索引

        index = i;

        break;//提高效率

     }

   }

    return index;

 }





}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值