一.学生类
package collection.demo1;
public class Student {
private String name;
private int age;
private String address;
private int gender; //0代表女,1代表男
private String num;
public Student() {
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public Student(String name, int age, String address, int gender) {
this.name = name;
this.age = age;
this.address = address;
this.gender = gender;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
", gender=" + gender +
", num=" + num +
'}';
}
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;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
}
二.主启动类
package collection.demo1;
import java.util.ArrayList;
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
ArrayList<Student> arrayList=new ArrayList<>(); //定义集合
while(true){
show();
System.out.println("请输入你的选择:");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
switch (i){
case 1:
add(arrayList);
break;
case 2:
delete(arrayList);
break;
case 3:
update(arrayList);
break;
case 4:
query(arrayList);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
case 5:
System.out.println("欢迎使用!");
System.exit(0); //JVM终止程序 0位正常退出
default:
System.out.println("指令错误,请重新输入指令!");
}
}
}
public static void show(){ //界面功能显示
System.out.println(" 学生管理系统");
System.out.println("=======1.添加学生信息 ========");
System.out.println("=======2.删除学生信息 ========");
System.out.println("=======3.修改学生信息 ========");
System.out.println("=======4.显示学生信息 ========");
System.out.println("=======5.退出管理系统 ========");
}
public static void add(ArrayList<Student> array){ //添加学生
Scanner scanner = new Scanner(System.in);
String num;
while (true) {//判断是否重复元素
System.out.println("请输入学号:");
num = scanner.nextLine();
boolean flag = numIsUsed(array, num);
if (flag) {
System.out.println("输入的学号已存在,请重新输入!");
}else {
break;
}
}
Student student = new Student();
student.setNum(num);
System.out.println("请输入姓名:");
String name=scanner.nextLine();
student.setName(name);
System.out.println("请输入年龄:");
int age=scanner.nextInt();
String s= scanner.nextLine();
student.setAge(age);
System.out.println("请输入地址:");
String address=scanner.nextLine();
student.setAddress(address);
System.out.println("请输入性别(0-女 1-男):");
int gender=scanner.nextInt();
student.setGender(gender);
array.add(student); //集合添加学生
}
public static void update(ArrayList<Student> array){ //修改学生
Scanner scanner = new Scanner(System.in);
System.out.println("所要修改的学号为:");
String num=scanner.nextLine();
int index=-1; //通过索引下标来判断是否匹配集合中的学生
for (int i = 0; i < array.size(); i++) {//遍历集合,快捷方式 array.size().for回车
Student s1 = array.get(i);
if (s1.getNum().equals(num)){
index=i;
}
}
if (index==-1){ //代表没有遍历到
System.out.println("输入的学生学号不存在!");
}else {
System.out.println("学生姓名修改为:");
String name=scanner.nextLine();
System.out.println("学生年龄修改为:");
int age=scanner.nextInt();
String s=scanner.nextLine();//加这个是为了避免下一个nextLine以这个nextInt的回车作为结束符
System.out.println("学生地址修改为:");
String address=scanner.nextLine();
System.out.println("学生性别修改为:");
int gender=scanner.nextInt();
Student student = new Student();//创建一个新的学生对象
student.setNum(num);
student.setName(name);
student.setAge(age);
student.setAddress(address);
student.setGender(gender);
array.set(index,student);
System.out.println("修改学生成功!");
}
}
public static void delete(ArrayList<Student> array){ //根据学号删除
System.out.println("请输入你要删除学生的学号:");
Scanner scanner = new Scanner(System.in);
String num=scanner.nextLine();
int index=-1;
for (int i = 0; i < array.size(); i++) {
Student student = array.get(i);
if (num.equals(student.getNum())){
index=i;
}
}
if (index==-1){
System.out.println("删除的学生学号不存在!");
}else {
array.remove(index);
System.out.println("删除学生成功!");
}
}
public static void query(ArrayList<Student> array){//信息显示
if (array.size()==0){ //没有添加学生就无法查到
System.out.println("暂时查无信息,请先添加");
return;
}
System.out.println("学号\t姓名\t年龄\t地址\t性别(0-女 1-男)");
for (int i = 0; i < array.size(); i++) {
Student student = array.get(i);
System.out.println(student.getNum()+"\t"
+student.getName()+"\t"
+student.getAge()+"\t"
+student.getAddress()+"\t"
+student.getGender());
}
System.out.println("添加成功!");
}
public static boolean numIsUsed(ArrayList<Student> array,String num){ //判断学号是否重复
boolean flag=false; //为true位重复,默认为false
for (int i = 0; i < array.size(); i++) {
Student student = array.get(i);
if (student.getNum().equals(num)){
flag=true;
break;
}
}
return flag;
}
}