代码供参考,需求并不难,建立饮品类,行为有:登录、查询、统计等。
import java.awt.List;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.event.ListSelectionEvent;
/*
* 这是我的学生管理系统的主类
* 利用集合完成对学生的增删改查四个功能
* A、定义学生类
* 学生管理系统的主界面的代码的编写
* 学生管理系统的查看所有学生的代码的编写
* 学生管理系统的添加所有学生的代码的编写
* 学生管理系统的删除所有学生的代码的编写
* 学生管理系统的修改所有学生的代码的编写
* 退出
*/
public class StudentManagerTest {
public static void main(String[] args) {
ArrayList<Student> list = 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(list);
break;
case "2":
// 添加所有学生
addStudent(list);
break;
case "3":
// 删除所有学生
deleteStudent(list);
break;
case "4":
// 修改所有学生
break;
case "5":
// 退出
// System.out.println("谢谢你的使用");//利用的是case穿透的特效
// break;
default:
System.out.println("谢谢你的使用");
System.exit(0);// 注意:这里是JVM虚拟机的终止!!!
break;
}
}
}
private static void updateStudent(ArrayList<Student> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要修改学生的学号:");
String sid = sc.nextLine();
int index = -1;
for (int x = 0; x < list.size(); x++) {
Student s = list.get(x);
if (s.getId().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.setId(sid);
s.setName(name);
s.setAge(age);
s.setAddress(address);
list.set(index, s);
System.out.println("修改学生成功");
} else {
System.out.println("不好意思,你要修改的学号对应的学生信息不存在,请回去重新选择你的操作");
return;
}
}
public static void deleteStudent(ArrayList<Student> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你要删除学生的学号:");
String id = sc.nextLine();
int index = -1;
for (int x = 0; x < list.size(); x++) {
Student s = list.get(x);
if (s.getId().equals(id)) {
index = x;
break;
}
}
if (index != -1) {
list.remove(index);
System.out.println("删除学生成功");
} else {
System.out.println("不好意思,你要删除的学号对应的学生信息不存在,请回去重新选择你的操作");
return;
}
}
public static void addStudent(ArrayList<Student> list) {
Scanner sc = new Scanner(System.in);
String id;
while (true) {
System.out.println("请输入学号:");
id = sc.nextLine();
boolean flag = false;
for (int x = 0; x < list.size(); x++) {
Student s = list.get(x);
if (s.getId().equals(id)) {
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.setId(id);// 这里是set方法进行赋值。
s.setName(name);
s.setAge(age);
s.setAddress(address);
list.add(s);// 向集合中添加对象,把学生对象作为元素添加到集合中
System.out.println("添加学生成功");
}
public static void findAllStudent(ArrayList<Student> list) {
if (list.size() == 0) {
System.out.println("不好意思,目前没有学生信息可供查看,请回去重新选择你的操作");
return;
}
System.out.println("学号\t\t姓名\t年龄\t居住地");
for (int i = 0; i < list.size(); i++) {
// System.out.println(list.get(i));错误格式
// Student s=new Student();错误格式
Student s = list.get(i);
System.out.println(s.getId() + "\t\t" + s.getName() + "\t" + s.getAge() + "\t" + s.getAddress());
}
}
}