用集合做數據的增刪改查

package cn.scxh.www.information;


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();
}

}


}


}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章