import java.util.*;
import java.io.*;
class StuMgr{
public static class Student{
public int id;
public String name;
public int age;
public Student(int id ,String name,int age){
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString(){
return id "," name "," age;
}
}
public List<Student> stuList = new LinkedList<>();
public void add(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生学号:");
String id = sc.nextLine();
int intId = 0;
try{
intId = Integer.parseInt(id);
}catch( ex){
System.out.println("学号输入有误,请输入数字!");
return;
}
if (find(intId) != null){
System.out.println("该学号已经存在!");
return ;
}
System.out.println("请输入学生姓名:");
String name = sc.nextLine();
System.out.println("请输入学生年龄:");
String age = sc.nextLine();
int intAge = 0;
try{
intAge = Integer.parseInt(age);
}catch( ex){
System.out.println("年龄输入有误,请输入数字!");
return;
}
Student stu = new Student(intId,name,intAge);
stuList.add(stu);
store();
System.out.println("-----------------------");
System.out.println("学生信息已增加");
System.out.println(stu);
System.out.println("-----------------------");
}
public void del(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生学号:");
String id = sc.nextLine();
int intId = 0;
try{
intId = Integer.parseInt(id);
}catch( ex){
System.out.println("学号输入有误,请输入数字!");
return;
}
Student stu = find(intId);
if ( stu == null){
System.out.println("该学号不存在!");
return ;
}
stuList.remove(stu);
store();
System.out.println("-----------------------");
System.out.println("学生信息已删除");
System.out.println(stu);
System.out.println("-----------------------");
}
public void find(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生学号:");
String id = sc.nextLine();
int intId = 0;
try{
intId = Integer.parseInt(id);
}catch( ex){
System.out.println("学号输入有误,请输入数字!");
return;
}
Student stu = find(intId);
if ( stu == null){
System.out.println("该学号不存在!");
return ;
}
System.out.println("-----------------------");
System.out.println("查找学生信息如下");
System.out.println(stu);
System.out.println("-----------------------");
}
public Student find(int id){
for(Student stu : stuList){
if(stu.id == id){
return stu;
}
}
return null;
}
public void modify(){
store();
}
public void foreach(){
System.out.println("-----------------------");
for(Student stu : stuList){
System.out.println(stu);
}
System.out.println("-----------------------");
}
public void store(){
Iterator iterator = stuList.iterator();
File file = new File("stuList.txt");
FileWriter fw = null;
writer = null;
try {
fw = new FileWriter(file);
writer = new (fw);
while(iterator.hasNext()){
writer.write(iterator.next().toString());
writer.newLine();//换行
}
writer.flush();
} catch ( e) {
e.();
}catch (IOException e) {
e.();
}finally{
try {
writer.close();
fw.close();
} catch (IOException e) {
e.();
}
}
}
public static void main(String[] args){
StuMgr mgr = new StuMgr();
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("6:退出");
System.out.println("-----------------------");
Scanner sc = new Scanner(System.in);
String op = sc.nextLine();
if("6".equals(op)){
return;
}
if("1".equals(op)){
mgr.add();
}
if("2".equals(op)){
mgr.del();
}
if("3".equals(op)){
mgr.find();
}
if("4".equals(op)){
mgr.modify();
}
if("5".equals(op)){
mgr.foreach();
}
}
}
}
时间仓促,还有一个modify方法没实现,留给你自己练手。