第一步:打开IDEA,创建包在包下编写基本信息类
运用JavaBean;
package Object.学生信息管理系统;
public class Student {
private String 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;
}
/**
* 获取
* @return id
*/
public String getId() {
return id;
}
/**
* 设置
* @param id
*/
public void setId(String id) {
this.id = id;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
/**
* 获取
* @return address
*/
public String getAddress() {
return address;
}
/**
* 设置
* @param address
*/
public void setAddress(String address) {
this.address = address;
}
}
再创建一个测试类
package Object.学生信息管理系统;
import java.util.ArrayList;
import java.util.Scanner;
public class StudentText {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<>();
loop:
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:退出页面");
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的选择:");
String num = sc.next();
switch (num) {
case "1":
addStudent(list);
break;
case "2":
deletsStudent(list);
break;
case "3":
updatesStudent(list);
break;
case "4":
queryStudent(list);
break;
case "5":
break loop;
// System.exit(0);//停止虚拟机运行
default:
System.out.println("输入错误·!!!");
}
}
}
public static void addStudent(ArrayList<Student> list) {
System.out.println("请输入你要添加的学生信息:");
Scanner sc = new Scanner(System.in);
String id = null;
while (true) {
System.out.println("输入学生ID");
id = sc.next();
boolean flag = panduan(list,id);
if (flag){
System.out.println("id存在!!!");
}else {
break;
}
}
System.out.println("输入学生姓名");
String name = sc.next();
System.out.println("输入学生年龄");
int age = sc.nextInt();
System.out.println("输入学生家庭地址");
String addrdess = sc.next();
Student student = new Student(id, name, age, addrdess);
list.add(student);
}
public static void deletsStudent(ArrayList<Student> list) {
System.out.println("请输入你要删除的学生id:");
Scanner sc = new Scanner(System.in);
String id = sc.next();
int num=Index(list,id);
if (num >=0) {
list.remove(num);
System.out.println("id为"+id+"的学生已经删除");
}else {
System.out.println("id不存在");
}
}
public static void updatesStudent(ArrayList<Student> list) {
System.out.println("请输入你要修改的学生id:");
Scanner sc = new Scanner(System.in);
String id = sc.next();
int num=Index(list,id);
if (num<0) {
System.out.println("id不存在!");
return;
}
Student stu=list.get(num);
System.out.println("修改后的名字");
String newName=sc.next();
stu.setName(newName);
System.out.println("修改后的年龄");
int newage=sc.nextInt();
stu.setAge(newage);
System.out.println("修改后的地址");
String newdiz=sc.next();
stu.setAddress(newdiz);
}
public static void queryStudent(ArrayList<Student> list) {
if (list.size() == 0) {
return;
}
System.out.println("id\t名字\t年纪\t家庭地址\t");
for (int i = 0; i < list.size(); i++) {
Student student = new Student();
student = list.get(i);
System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t" + student.getAddress());
}
}
public static boolean panduan(ArrayList<Student> list, String id) {
for (int i = 0; i < list.size(); i++) {
if (id.equals(list.get(i).getId())) {
return true;
}
}
return false;
}
public static int Index(ArrayList<Student> list, String id){
for (int i = 0; i < list.size(); i++) {
Student student=list.get(i);
String stu=student.getId();
if (stu.equals(id)) {
return i;
}
}
return -1;
}
}