Model类:
package com.ha.student; import java.util.ArrayList; import java.util.HashMap; public class Model { private HashMap<Integer,Student> mlist=new HashMap<Integer,Student>(); private ArrayList<Student>alist=new ArrayList<Student>(); public void addStudent(Student stu){ mlist.put(stu.getSid(), stu); alist.add(stu); } public void deleteStudent(Student stu){ mlist.remove(stu.getSid()); alist.remove(stu); } public void xiugaiStudent(int sid,Student newstu){ alist.remove(bianliStudent(sid)); alist.add(newstu); mlist.remove(sid); mlist.put(sid,newstu); } public ArrayList<Student> returnStudent(){ return alist; } public Student bianliStudent(int sid){ return mlist.get(sid); } }
Student类:
package com.ha.student; public class Student { private int sid; private String name; private int age; public Student(int sid,String name,int age){ this.sid=sid; this.name=name; this.age=age; } public String toString(){ return sid+"\t"+name+"\t"+age; } public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } 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; } }
view类:
package com.ha.student; import java.util.List; import java.util.Scanner; public class View { Scanner input=new Scanner(System.in); public void welcome(){ 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("请您选择想要的操作!"); } public Student vStudent(){ System.out.println("学生的学号:"); int sid=input.nextInt(); System.out.println("学生的姓名:"); String name=input.next(); System.out.println("学生的年龄:"); int age=input.nextInt(); Student stu=new Student(sid,name,age); return stu; } public void display(List<Student> list){ for(Student stu:list){ System.out.println(stu); } } }
Main类:
package com.ha.student; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); View v = new View();//新建View的对象v Model m=new Model(); int choice; while (true) { v.welcome();//调用View类中的welcome方法 choice = input.nextInt();//进行选择操作 switch (choice) { case 1://添加学生信息 if(v.vStudent()==null){ System.out.println("添加的学生为空,请重新添加"); }else{ m.addStudent(v.vStudent()); } break; case 2://删除学生信息 System.out.println("请输入要删除的学号:"); int deletesid=input.nextInt(); if(m.bianliStudent(deletesid)==null){ System.out.println("不存在该学生学号,请重新输入!"); }else{ m.deleteStudent(m.bianliStudent(deletesid)); } break; case 3://修改学生信息 System.out.println("请输入要修改的学号:"); int xiugaisid=input.nextInt(); if(m.bianliStudent(xiugaisid)==null){ System.out.println("不存在该学生学号,请重新输入!"); }else{ m.xiugaiStudent(xiugaisid,v.vStudent()); } break; case 4://查询学生信息 System.out.println("请输入要查询的学号:"); int chaxunsid=input.nextInt(); Student chaxunStudent=m.bianliStudent(chaxunsid); if(chaxunStudent==null){ System.out.println("不存在该学生学号,请重新输入!"); }else{ System.out.println(chaxunStudent); } break; case 5://显示学生信息 v.display(m.returnStudent()); break; case 6://退出学生信息 return; default://选择有误请重新选择! System.out.println("选择有误请重新选择!"); break; }//switch() }//while() }//main() }//Main{}