(请发邮箱)综合运用自己所学的Java面向对象程序设计知识,设计一学生成绩管理系统。要求如下:
综合运用自己所学的Java面向对象程序设计知识,设计一学生成绩管理系统。要求如下:(1)学生的信息包括:学号、姓名、班级、多门课程的成绩等。(2)系统功能包括:输入、删除...
综合运用自己所学的Java面向对象程序设计知识,设计一学生成绩管理系统。要求如下: (1)学生的信息包括:学号、姓名、班级、多门课程的成绩等。 (2)系统功能包括:输入、删除、修改、查询学生成绩等。 (3)学生成绩等信息保存在studentscores.txt文件中。 (4)编码符合规范。不需要调用数据库,只需要做出界面即可。最好能够使用eclipse。
展开
1个回答
展开全部
下午写了个~木有界面木有数据库~ 可以增删学生 修改删除添加学生成绩 查询学生信息和查询全部学生信息 package mon_10; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class StudentManager { private static File DB = new File("studentscores.txt"); private static Set<Student> studentList = null; @SuppressWarnings("unchecked") private static void initStudentList() { if (DB.exists()) { FileInputStream fi = null; ObjectInputStream oi = null; try { fi = new FileInputStream(DB); oi = new ObjectInputStream(fi); Object obj = oi.readObject(); fi.close(); oi.close(); if (obj instanceof Set) studentList = (Set<Student>) obj; } catch (Exception e) { e.printStackTrace(); } } if (studentList == null) studentList = new HashSet<Student>(); } private static void save() { if (studentList == null) return; if (!DB.exists()) try { DB.createNewFile(); } catch (Exception e) { e.printStackTrace(); } FileOutputStream fo; try { fo = new FileOutputStream(DB); ObjectOutputStream oo = new ObjectOutputStream(fo); oo.writeObject(studentList); fo.close(); oo.close(); } catch (Exception e) { e.printStackTrace(); } } private static Student[] getStudents(int id, String name) { HashSet<Student> rlt = new HashSet<Student>(); if (id != 0) { for (Student s : studentList) { if (s.id == id) rlt.add(s); } } else if (name != null && !name.isEmpty()) { for (Student s : studentList) { if (s._name.equalsIgnoreCase(name)) rlt.add(s); } } return rlt.toArray(new Student[0]); } static { initStudentList(); } static void out(Object obj){ System.out.println(obj); } public static void menu() { out("Welcome to use Student Manager!\n\n1-search student;\t2-add student;\t3-list all students;\nothers-exit"); Scanner in = new Scanner(System.in); Student student = null; String input = in.nextLine(); if (input.equals("1")) { Student[] students = new Student[0]; out("please input name/id"); String ins = in.nextLine(); if (ins.matches("\\d+")) students = getStudents(Integer.parseInt(ins), null); else students = getStudents(0, ins); if (students.length != 0) { out(Arrays.toString(students).replaceAll( "\\[|\\]|, ", "\n")); if (students.length == 1) student = students[0]; else { out("please choose a student:\n"); int i = in.nextInt(); for (Student s : students) { if (s.id == i) { student = s; break; } else { out("error"); menu(); } } } out("1-set score;\t2-delete score;\t3-delete student;\tother-exit"); int input1=in.nextInt(); in.nextLine(); String course=null; float score=0; switch(input1){ case 1: out("please input course"); course=in.nextLine(); out("please input score"); score = in.nextFloat(); in.nextLine(); student.setScore(course, score); break; case 2: out("please input course"); course=in.nextLine(); student.removeScore(course); break; case 3: student.removeFrom(studentList); break; } } else { out("NO current record!"); } } if (input.equals("2")) { out("please input id"); int id=in.nextInt(); in.nextLine(); out("please input name"); String name = in.nextLine(); out("please input class"); String cname=in.nextLine(); student=new Student(id, name, cname); if(studentList.add(student)){ out("create success!\n"); String input2="-"; while(!input2.isEmpty()){ out("set score? \n1-yes;\t2-no"); input2=in.nextLine(); if(input2.equals("1")){ out("please input course"); String course=in.nextLine(); out("please input score"); float score = in.nextFloat(); student.setScore(course, score); in.nextLine(); } else break; } } } if (input.equals("3")) { out(studentList.toString().replaceAll("\\[|\\]|, ", "\n")); } if(input.isEmpty())return; save(); menu(); } public static void main(String[] args){ menu(); } } class Student implements Serializable { @Override public String toString() { return "id: " + id + "\tclass: " + _class + "\tname: " + _name + courses.toString().replaceAll("\\{|\\}|, ", "\n").replaceAll("=", ": "); } Student(int id, String _name, String _class) { this.id = id; this._class = _class; this._name = _name; } private static final long serialVersionUID = 1L; public int id = 0; public String _name = ""; public String _class = ""; HashMap<String, Float> courses = new HashMap<String, Float>(); void setScore(String course, float score) { courses.put(course, score); System.out.println("set success!\n\n"+this); } void removeScore(String course) { courses.remove(course); System.out.println("delete success!\n\n"+this); }; void removeFrom(Set<?> list) { list.remove(this); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((_class == null) ? 0 : _class.hashCode()); result = prime * result + ((_name == null) ? 0 : _name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (this.id == ((Student) obj).id) return true; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (_class == null) { if (other._class != null) return false; } else if (!_class.equals(other._class)) return false; if (_name == null) { if (other._name != null) return false; } else if (!_name.equals(other._name)) return false; return true; } }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询