1.感想:编程时先进行框架设计,然后把代码一个一个放入框架,这是一种很有用的思维方式。因为一开始我们往往容易立马开始敲代码,然后逐渐迷失了方向,用框架来思考比较清晰。
2.管理系统框架和代码如下:
public class StudentManangerTest {
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 choiceString = sc.nextLine();
switch (choiceString) {
case "1":
findAllStudent(array);
break;
case "2":
addStudent(array);
break;
case "3":
deleteStudent(array);
break;
case "4":
updateStudent(array);
break;
case "5":
default:
System.out.println("谢谢你的使用 ");
System.exit(0);
break;
}
}
}
3.写完框架后,再把空缺补上:
private static void updateStudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要修改学生的学号:");
String sid = sc.nextLine();
int index = -1;
for (int x = 0; x < array.size(); x++) {
Student s = array.get(x);
if (s.getSid().equals(sid)) {
index = x;
break;
}
}
if (index != -1) {
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);
array.set(index, s);
System.out.println("修改学生成功");
} else {
System.out.println("不好意思,你要修改的学号对应的学生信息不存在,请回去重新选择你的操作");
return;
}
}
private static void deleteStudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要删除学生的学号:");
String sid = sc.nextLine();
int index = -1;
for (int x = 0; x < array.size(); x++) {
Student s = array.get(x);
if (s.getSid().equals(sid)) {
index = x;
break;
}
}
if (index != -1) {
array.remove(index);
System.out.println("删除学生成功");
} else {
System.out.println("不好意思,你要删除的学号对应的学生信息不存在,请回去重新选择你的操作");
return;
}
}
private static void addStudent(ArrayList<Student> array) {
Scanner sc = new Scanner(System.in);
String sid;
while (true) {
System.out.println("请输入学号:");
sid = sc.nextLine();
boolean flag = false;
for (int x = 0; x < array.size(); x++) {
Student s = array.get(x);
if (s.getSid().equals(sid)) {
flag = true;
}
}
if (flag == true) {
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 s = new Student();
s.setSid(sid);
s.setName(name);
s.setAge(age);
s.setAddress(address);
array.add(s);
System.out.println("添加学生成功");
}
private static void findAllStudent(ArrayList<Student> array) {
if (array.size() == 0) {
System.out.println("不好意思,目前没有学生信息可供查看,请回去重新选择你的操作");
return;
}
System.out.println("学号\t\t姓名\t年龄\t居住地");
for (int x = 0; x < array.size(); x++) {
Student s = array.get(x);
System.out.println(s.getSid() + "\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getAddress());
}
}
}
|
|