学生选课系统完成的步骤:
- 创建学生类和选课类
- 添加课程
- 课程查询
- 课程修改
- 课程删除
要求:
使用泛型管理课程
通过set集合管理课程
使用Map添加学生和删除Map中的学生
修改Map中的学生
package Package12;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class UserController {
public static void main(String[] args) {
//1.
ArrayList<student> list = new ArrayList<>();
loop :while (true){
cscd();
//2.
Scanner sc = new Scanner(System.in);
String xz = sc.next();
switch (xz){
case "1":
tj(list);
break;
case "2":
sc(list);
break;
case "3":
xg(list);
break;
case "4":
cx(list);
break;
case "5":
System.out.println("退出");
break loop;
//System.exit(0);//停止虚拟机运行
default:
System.out.println("没有这个选项");
}
}
}
public static void cscd(){
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("请输入您的选择");
}
//添加学生
public static void tj(ArrayList<student> list){
Scanner sc = new Scanner(System.in);
System.out.println("请问有多少学生");
int a = sc.nextInt();
System.out.println("请输入学生信息");
for (int i = 0; i < a; i++) {
System.out.println("请按顺序输入,回车隔开(id,姓名,年龄,地址)");
String id = sc.next();
for (int j = 0; j < list.size(); j++) {
student stu = list.get(j);
if (id.equals(stu.getId())){
System.out.println("id重复,请重新输入(id,姓名,年龄,地址)");
id = sc.next();
j=-1;
}
}
String name = sc.next();
int age = sc.nextInt();
String address = sc.next();
student s = new student(id,name,age,address);
list.add(s);
}
System.out.println("添加成功,已经返回主菜单");
}
//删除学生
public static void sc(ArrayList<student> list){
Scanner sc = new Scanner(System.in);
System.out.println("请输入id");
String id = sc.next();
int count = 0;
for (int i = 0; i < list.size(); i++) {
student stu = list.get(i);
if(id.equals(stu.getId())){
list.remove(i);
System.out.println("删除成功,已返回主菜单");
count++;
}
}
if (count == 0){
System.out.println("用户不存在,已返回初始菜单");
//cscd();
}
}
//修改学生
public static void xg(ArrayList<student> list){
Scanner sc = new Scanner(System.in);
System.out.println("请输入要修改的id");
String id = sc.next();
int count = 0;
for (int i = 0; i < list.size(); i++) {
student stu = list.get(i);
if(id.equals(stu.getId())){
System.out.println("请输入姓名,年龄,地址");
String name = sc.next();
int age = sc.nextInt();
String address = sc.next();
student s = new student(id,name,age,address);
list.set(i,s);
System.out.println("修改成功");
count++;
}
}
if(count == 0){
System.out.println("该用户不存在,已返回");
}
}
//查询学生
public static void cx(ArrayList<student> list){
//根据id查学生
/*Scanner sc = new Scanner(System.in);
System.out.println("请输入要查询学生id");
String id = sc.next();
int count = 0;
for (int i = 0; i < list.size(); i++) {
student stu = list.get(i);
if(id.equals(stu.getId())){
System.out.println("id\t\t姓名\t年龄\t家庭住址");
System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAddress());
count++;
}
}
if (count == 0){
System.out.println("当前无学生信息,请添加后再查询");
System.out.println("已返回主菜单");
}*/
//查询学生
if(list.size() == 0){
System.out.println("当前无学生信息,请添加后再查询");
System.out.println("已返回主菜单");
}else {
System.out.println("id\t\t姓名\t年龄\t家庭住址");
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.getAddress());
}
}
}
}
class student{
private String id; //id
private String name; //名字
private int age; //年龄
private String address; //地址
public student() {
}
public student(String id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}