一、学生管理系统包含增删改查,如果学号相同的话则不能添加。
二、先写出Student类,然后构造方法,再创建一个Manager类对学生用户进行增删改查管理:
1.主操作界面,while循环使程序一直运行,直到选择退出,方便用户操作;然后使用switch选择对应的程序操作:
public static void main(String[] args) {
ArrayList<Student> arr = new ArrayList<>();
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("===================================");
System.out.println("请选择");
//键盘录入
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
//选择操作
switch (line) {
case "1":
System.out.println("添加学生");
add(arr);
break;
case "2":
System.out.println("删除学生");
shan(arr);
break;
case "3":
System.out.println("修改学生");
gai(arr);
break;
case "4":
System.out.println("查询学生");
cha(arr);
break;
case "5":
System.out.println("谢谢使用");
return;
default:
System.out.println("输入错误");
break;
}
}
}
2.添加数据,其中 boolean flag = isUsed(arr,xuehao)
;是调用查重方法判断学号是否相同,如果flag为true则与之前学号相同,就不能继续进行下一步数据插入:
public static void add(ArrayList<Student> arr) {
Scanner sc = new Scanner(System.in);
String xuehao;
while (true) {
System.out.println("请输入学生学号");
xuehao = sc.nextLine();
boolean flag = isUsed(arr,xuehao);
if (flag){
System.out.println("你输入的学号已被使用");
}else {
break;
}
}
3.删除:
public static void shan(ArrayList<Student> arr) {
//键盘录入学号信息
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的学号");
String xuehao = sc.nextLine();
//遍历集合找到对应的学号
for (int i = 0; i < arr.size(); i++) {
Student student = arr.get(i);
if (student.getXuehao().equals(xuehao)) {
arr.remove(i);
System.out.println("删除成功");
return;
}
}
System.out.println("删除失败");
}
4.修改:
public static void gai(ArrayList<Student> arr) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入学号");
String xuehao = sc.nextLine();
for (int i = 0; i < arr.size(); i++) {
Student student = arr.get(i);
if (student.getXuehao().equals(xuehao)){
System.out.println("请输入新的姓名");
String name = sc.nextLine();
System.out.println("请输入新的年龄");
int age = sc.nextInt();
System.out.println("请输入新的地址");
String address = sc.nextLine();
Student s = new Student();
s.setName(name);
s.setAge(age);
s.setAddress(address);
arr.set(i,s);
System.out.println("修改成功");
return;
}
}
System.out.println("输入错误");
}
5.查询:
public static void cha(ArrayList<Student> arr) {
if (arr.size() == 0) {
System.out.println("无信息,请先添加");
return;
}
System.out.println("学号\t姓名\t年龄\t地址");
for (int i = 0; i < arr.size(); i++) {
Student student = arr.get(i);
System.out.println(student.getXuehao() + "\t\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getAddress());
}
}
}
6.检验学号是否重复,如果 boolean flag = false;
则证明学号与之前不重复,直接返回flag到添加方法里面,遍历集合有相同的学号,flag就等于true,返回到添加:
public static boolean isUsed(ArrayList<Student> arr,String xuehao){
boolean flag = false;
for (int i = 0; i < arr.size(); i++) {
Student student = arr.get(i);
if (student.getXuehao().equals(xuehao)){
flag = true;
break;
}
}
return flag;
}
三、完整代码如下:
public class Student {
private String name;
private int age;
private String xuehao;
private String address;
public Student(){}
public Student(String name, int age, String xuehao,String address) {
this.name = name;
this.age = age;
this.xuehao = xuehao;
this.address = address;
}
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 getXuehao() {
return xuehao;
}
public void setXuehao(String xuehao) {
this.xuehao = xuehao;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Mannger {
public static void main(String[] args) {
ArrayList<Student> arr = new ArrayList<>();
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("===================================");
System.out.println("请选择");
//键盘录入
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
//选择操作
switch (line) {
case "1":
System.out.println("添加学生");
add(arr);
break;
case "2":
System.out.println("删除学生");
shan(arr);
break;
case "3":
System.out.println("修改学生");
gai(arr);
break;
case "4":
System.out.println("查询学生");
cha(arr);
break;
case "5":
System.out.println("谢谢使用");
return;
default:
System.out.println("输入错误");
break;
}
}
}
public static void add(ArrayList<Student> arr) {
Scanner sc = new Scanner(System.in);
String xuehao;
while (true) {
System.out.println("请输入学生学号");
xuehao = sc.nextLine();
boolean flag = isUsed(arr,xuehao);
if (flag){
System.out.println("你输入的学号已被使用");
}else {
break;
}
}
System.out.println("请输入学生姓名");
String name = sc.nextLine();
System.out.println("请输入学生年龄");
int age = sc.nextInt();
System.out.println("请输入学生地址");
String address = sc.next();
//创建对象并赋值
Student s = new Student(name, age, xuehao, address);
//往集合中添加数据
arr.add(s);
}
public static void shan(ArrayList<Student> arr) {
//键盘录入学号信息
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除的学号");
String xuehao = sc.nextLine();
//遍历集合找到对应的学号
for (int i = 0; i < arr.size(); i++) {
Student student = arr.get(i);
if (student.getXuehao().equals(xuehao)) {
arr.remove(i);
System.out.println("删除成功");
return;
}
}
System.out.println("删除失败");
}
public static boolean isUsed(ArrayList<Student> arr,String xuehao){
boolean flag = false;
for (int i = 0; i < arr.size(); i++) {
Student student = arr.get(i);
if (student.getXuehao().equals(xuehao)){
flag = true;
break;
}
}
return flag;
}
public static void gai(ArrayList<Student> arr) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入学号");
String xuehao = sc.nextLine();
for (int i = 0; i < arr.size(); i++) {
Student student = arr.get(i);
if (student.getXuehao().equals(xuehao)){
System.out.println("请输入新的姓名");
String name = sc.nextLine();
System.out.println("请输入新的年龄");
int age = sc.nextInt();
System.out.println("请输入新的地址");
String address = sc.nextLine();
Student s = new Student();
s.setName(name);
s.setAge(age);
s.setAddress(address);
arr.set(i,s);
System.out.println("修改成功");
return;
}
}
System.out.println("输入错误");
}
public static void cha(ArrayList<Student> arr) {
if (arr.size() == 0) {
System.out.println("无信息,请先添加");
return;
}
System.out.println("学号\t姓名\t年龄\t地址");
for (int i = 0; i < arr.size(); i++) {
Student student = arr.get(i);
System.out.println(student.getXuehao() + "\t\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getAddress());
}
}
}