import java.util.ArrayList;
import java.util.List;
Student 类
public class Student {
private int id;
private String name;
private String number;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int id, String name, String number) {
super();
this.id = id;
this.name = name;
this.number = number;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
集合做数据的增删改查
package cn.scxh.www.information;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class DataChange {
public List<Student> studentInformation = new ArrayList<Student>();
public Student checkInformation(int num){
int count = studentInformation.size();
for (int i = 0; i < count; i++) {
Student student1 = studentInformation.get(i);
if (student1.getId() == num) {
return student1;
}
}
return null;
}
public void addInformation(){
System.out.println("======添加学生信息操作=========");
Scanner scaner = new Scanner(System.in);
System.out.println("请输入id:");
int id = scaner.nextInt();
System.out.println("请输入姓名:");
String name = scaner.next();
System.out.println("请输入学号:");
String number = scaner.next();
Student stu = checkInformation(id);
if (stu==null) {
Student student = new Student(id,name,number);
studentInformation.add(student);
System.out.println("输入成功");
}else{
System.out.println("此人存在,输入失败");
}
}
public void deletInformation(){
System.out.println("======删除学生信息操作=========");
Scanner scaner = new Scanner(System.in);
System.out.println("请输入id:");
int id = scaner.nextInt();
Student stu = checkInformation(id);
if (stu!=null) {
studentInformation.remove(stu);
System.out.println("删除成功");
}else{
System.out.println("此人不在管理系统中");
}
}
public void selectInformation(){
Scanner scaner = new Scanner(System.in);
System.out.println("请输入id:");
int id = scaner.nextInt();
Student stu = checkInformation(id);
if (stu!=null) {
String stuName = stu.getName();
String stuNumber = stu.getNumber();
System.out.println("此学生的姓名是:"+stuName+" "+"此学生的学号是:"+stuNumber);
}else{
System.out.println("此人不在管理系统中");
}
}
public void selectAllStudentInformation(){
for (Iterator iterator = studentInformation.iterator(); iterator
.hasNext();) {
Student object = (Student) iterator.next();
System.out.println(object.getId()+" "+object.getName()+" "+object.getNumber());
}
}
public void exit(){
System.out.println("退出学生管理系统");
System.exit(0);
}
}
测试学生类
package cn.scxh.www.information;
import java.util.Scanner;
public class TestStudent {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎来到学生管理系统");
Scanner scInformation = new Scanner(System.in);
DataChange studentInfo = new DataChange();
while (true) {
System.out.println("请输入操作命令");
int num = scInformation.nextInt();
switch(num){
case 1:
studentInfo.addInformation();
break;
case 2:
studentInfo.deletInformation();
break;
case 3:
studentInfo.selectInformation();
break;
case 4:
studentInfo.selectAllStudentInformation();
break;
case 5:
studentInfo.exit();
}
}
}
}