“键盘敲烂,月薪过万!”
记录一下今天的学习:
跟着黑马老师敲代码,实现了一个简单的学生管理系统
来人,咖啡泡上——————
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Itheima.day12; /** * * @author NJUST'er'wang's */ public class Student { //学号 private String sid; //姓名 private String name; //年龄 private String age; //居住地 private String address; public Student() { } public Student(String sid, String name, String age, String address) { this.sid = sid; this.name = name; this.age = age; this.address = address; } public void setSid(String sid) { this.sid = sid; } public void setName(String name) { this.name = name; } public void setAge(String age) { this.age = age; } public void setAddress(String address) { this.address = address; } public String getSid() { return sid; } public String getName() { return name; } public String getAge() { return age; } public String getAddress() { return address; } }
接着在主类里面实现对于学生的管理——添加,删除,修改,查看等等;
public static void main(String[] args) { //创建集合对象,用于存储学生数据 ArrayList<Student> array = new ArrayList<Student>(); 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("请输入你的选择"); //用 Scanner 实现键盘的录入 Scanner sc = new Scanner(System.in); String s = sc.nextLine(); //switch 完成操作 switch (s) { case "1": // System.out.println("添加学生"); addStudent(array); break; case "2": // System.out.println("删除学生"); deleStudent(array); break; case "3": // System.out.println("修改学生"); updateStudent(array); break; case "4": // System.out.println("查看所有学生"); findAllStudent(array); break; case "5": System.out.println("谢谢使用"); System.exit(0);//JVM退出 } } }
public static void addStudent(ArrayList<Student> array) { //键盘录入学生对象所需的信息 Scanner sc = new Scanner(System.in); String sid; while (true) { System.out.println("请输入学生学号:"); sid = sc.nextLine(); boolean flag = isUesd(array, sid); if (flag) { System.out.println("你输入的学号已经被使用,请重新输入"); } else { break; } } System.out.println("请输入学生姓名:"); String name = sc.nextLine(); System.out.println("请输入学生年龄:"); String age = sc.nextLine(); System.out.println("请输入学生居住地:"); String address = sc.nextLine(); //创建学生对象 Student st = new Student(); st.setSid(sid); st.setName(name); st.setAge(age); st.setAddress(address); //将学生对象添加到集合中 array.add(st); //输出添加成功提示 System.out.println("添加成功!"); }
public static boolean isUesd(ArrayList<Student> array, String sid) { boolean flag = false; for (int i = 0; i < array.size(); i++) { Student s = array.get(i); if (s.getSid().equals(sid)) { flag = true; break; } } return flag; }
public static void deleStudent(ArrayList<Student> array) { Scanner sc = new Scanner(System.in); System.out.println("请输入你要删除的学号:"); String sid = sc.nextLine(); int index = -1; //遍历集合将对应学生对象从集合中删除 for (int i = 0; i < array.size(); i++) { Student get = array.get(i); if (get.getSid().equals(sid)) { // array.remove(i); index = i; break; } } if (index == -1) { System.out.println("该信息不存在,请重新输入"); } else { array.remove(index); System.out.println("删除成功!!!"); } }
public static void findAllStudent(ArrayList<Student> array) { //判断集合是否为空 if (array.size() == 0) { System.out.println("无信息,请先添加学生信息"); } else { //显示学生信息 //\t 其实就是tab键的位置 System.out.println("学号\t姓名\t年龄\t居住地"); //将集合中数据取出按照对应格式显示学生信息,年龄补充“岁”; for (int i = 0; i < array.size(); i++) { Student get = array.get(i); System.out.println(get.getSid() + "\t" + get.getName() + "\t" + get.getAge() + "岁\t" + get.getAddress()); } } }
public static void updateStudent(ArrayList<Student> array) { Scanner sc = new Scanner(System.in); System.out.println("请输入要修改的学号"); String sid = sc.nextLine(); //键盘录入学生的新名 System.out.println("请输入新姓名"); String name = sc.nextLine(); System.out.println("请输入新年龄"); String age = sc.nextLine(); System.out.println("请输入新居住地"); String address = sc.nextLine(); //创建学生对象 Student s = new Student(); s.setSid(sid); //必须要,新建对象内部值为默认null s.setName(name); s.setAge(age); s.setAddress(address); for (int i = 0; i < array.size(); i++) { Student get = array.get(i); if (get.getSid().equals(sid)) { array.set(i, s); break; } } System.out.println("修改学生成功!!!"); }
好了 咖啡泡好!!!