2021-08-19 学习笔记
笔记内容:javaee版学生管理系统(使用ArrayList进行存储)
内容来源:黑马视频
1.预计成果:
效果图
2.完成步骤
3.标准类写法
i.student类(定义student类)
package com.itheima.Aug.学生管理系统;
public class Student {
//学号 姓名 年龄 居住地
private String sid;
private String name;
private String age;
private String address;
public Student() {
}
@Override
public String toString() {
return "Student{" +
"sid='" + sid + '\'' +
", name='" + name + '\'' +
", age='" + age + '\'' +
", address='" + address + '\'' +
'}';
}
public Student(String sid, String name, String age, String address) {
this.sid = sid;
this.name = name;
this.age = age;
this.address = address;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
1.4主界面编写
***ii.studentManager类(学生主页面)***
package com.itheima.Aug.学生管理系统;
import com.itheima.Aug.fifty.student;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentManager {
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 sc = new Scanner(System.in);
String s = sc.nextLine();
switch (s) {
case "1":
System.out.println("添加学生");
addStudent(array);
break;
case "2":
System.out.println("删除学生");
deleteStudent(array);
break;
case "3":
System.out.println("修改学生");
updateStudent(array);
break;
case "4":
System.out.println("查看学生");
checkStudent(array);
break;
case "5":
System.out.println("退出");
System.exit(0);
}
}
}
public static void addStudent(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 addresss = sc.nextLine();
Student s = new Student();
s.setAddress(addresss);
s.setSid(sid);
s.setAge(age);
s.setName(name);
array.add(s);
System.out.println("添加完成");
}
public static void checkStudent(ArrayList<Student> array) {
System.out.println("学号 姓名 年龄 居住地");
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
System.out.println(s.getSid() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getAddress());
}
}
public static void deleteStudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的学生学号");
String sid = sc.nextLine();
// for(int i=0;i<array.size();i++)
// {
// Student s =array.get(i);
// if(s.getSid().equals(sid))
// {
// array.remove(i);
// System.out.println("删除学生成功");
// break;
// }else
// {
// System.out.println("删除学生失败");
// }
// }
int index = -1;
for (int i = 0; i < array.size(); i++) {
Student s = array.get(i);
if (s.getSid().equals(sid)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("该信息不存在,请重新输入");
} else {
array.remove(index);
System.out.println("删除成功");
}
}
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);
s.setName(name);
s.setAge(age);
s.setAddress(address);
for (int i = 0; i < array.size(); i++) {
Student student = array.get(i);
if (student.getSid().equals(sid)) {
array.set(i, s);
break;
}
}
}
至此代码基本编写完毕:
已知bug 重复不会报错、通过id修改信息如果id不存在不会发出报错信息