闲着无聊写着玩的,个人认为对于初学java的有所帮助吧
实体类
package net.etwo.cec.bean; import java.io.Serializable; public class StudentGradeInfo implements Serializable { private static final long serialVersionUID = -2332015918025542961L; private String sname; private String sno; private String chineseScore; private String mathScore; private String englishScore; public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getChineseScore() { return chineseScore; } public void setChineseScore(String chineseScore) { this.chineseScore = chineseScore; } public String getMathScore() { return mathScore; } public void setMathScore(String mathScore) { this.mathScore = mathScore; } public String getEnglishScore() { return englishScore; } public void setEnglishScore(String englishScore) { this.englishScore = englishScore; } }
package net.etwo.cec.main; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Scanner; import net.etwo.cec.bean.StudentGradeInfo; public class StudentGradeManage { /** * 成绩管理系统,控制台输出 * @author etwo * @param args */ private static String strFileName = "student.dat"; private static Map<String, StudentGradeInfo> sgiMap; // private static StudentGradeInfo students = new StudentGradeInfo(); public static void main(String[] args) { loadStudentGradeInfoFile(); selectMainMenu(); } /** * 主菜单显示 */ public static void selectMainMenu() { System.out.println("*********************成绩管理系统**********************"); System.out .println("*******************************************************"); System.out.println("1、信息维护\t2、信息查询\t3、成绩统计\t4、排序\t5、退出"); System.out .println("*******************************************************"); System.out.print("请从以上菜单中选择相应序号操作:"); int s = 0; try { s = mainMenu(); selectContainer(s); } catch (Exception e) { System.out.println("警告!您选择了特殊字符,为保证系统有效允许,请输入有效数字!"); selectContainer(s); } } /** * 主菜单显示@主菜单选择器 * * @param s */ public static void selectContainer(int s) { if (s == 1) { infoManage(); } else if (s == 2) { infoSearch(); } else if (s == 3) { infoCount(); } else if (s == 4) { infoOrder(); } else if (s == 5) { exitSystem(); } else { selectMainMenu(); } } /** * 主菜单显示@主菜单选择逻辑 * * @return * @throws Exception */ public static int mainMenu() throws Exception { Scanner sc = new Scanner(System.in); while (true) { int s = sc.nextInt(); if (s == 1) { return 1; } else if (s == 2) { return 2; } else if (s == 3) { return 3; } else if (s == 4) { return 4; } else if (s == 5) { return 5; } else { System.out.print("提示!选择有误,请重新选择:"); } } } /** * 信息维护 */ public static void infoManage() { System.out.println("\t**************************"); System.out.println("\t\t1、信息维护"); System.out.println("\t**************************"); System.out.println("1、增加学生信息\t2、删除学生信息\t3、修改学生信息\t4、返回主菜单\t5、退出"); Scanner sc = new Scanner(System.in); System.out.print("请选择:"); // s = sc.nextInt(); String s = sc.next(); if (s.equals("1")) { System.out.println("1、增加学生信息"); addStudentGradeInfo(); } else if (s.equals("2")) { System.out.println("2、删除学生信息"); deleteStudentGradeInfo(); } else if (s.equals("3")) { System.out.println("3、修改学生信息"); modifyStudentGradeInfo(); } else if (s.equals("4")) { selectMainMenu(); } else if (s.equals("5")) { exitSystem(); } else { System.out.println("输入有误,请输入以上数字!"); infoManage(); } } /** * 信息查询 */ public static void infoSearch() { System.out.println("\t**************************"); System.out.println("\t\t2、信息查询"); System.out.println("\t**************************"); System.out.println("1、查询所有\t2、按姓名查询\t3、按学号查询\t4、返回主菜单\t5、退出"); System.out.print("请选择:"); Scanner sc = new Scanner(System.in); String s = sc.next(); if (s.equals("1")) { searchAsAll(); } else if (s.equals("2")) { searchAsSname(); } else if (s.equals("3")) { searchAsSno(); } else if (s.equals("4")) { selectMainMenu(); } else if (s.equals("5")) { exitSystem(); } else { System.out.println("选择有误,请重新选择!"); infoSearch(); } } /** * 所有查询 */ public static void searchAsAll() { System.out.println("***************所有学生信息***************"); System.out.println("姓名\t学号\t语文\t数学\t英语"); StudentGradeInfo studentsInfo; for (Iterator<StudentGradeInfo> iter = sgiMap.values().iterator(); iter .hasNext();) { studentsInfo = iter.next(); showSearchResult(studentsInfo); } infoSearch(); } /** * 信息查询@按姓名查询 */ public static void searchAsSname() { System.out.print("请输入要查询学生姓名:"); Scanner sc = new Scanner(System.in); String sname = sc.nextLine(); System.out.println("***************查询结果***************"); System.out.println("姓名\t学号\t语文\t数学\t英语"); for (Iterator<StudentGradeInfo> iter = sgiMap.values().iterator(); iter .hasNext();) { StudentGradeInfo studentsInfo = (StudentGradeInfo) iter.next(); if (sname.equals(studentsInfo.getSname())) { // System.out.println("您要查找的学生姓名为:" + sname); showSearchResult(studentsInfo); } } infoSearch(); } /** * 查询显示界面 * * @param studentsInfo */ public static void showSearchResult(StudentGradeInfo studentsInfo) { String sname = studentsInfo.getSname(); String sno = studentsInfo.getSno(); String chineseScore = studentsInfo.getChineseScore(); String mathScore = studentsInfo.getMathScore(); String englishScore = studentsInfo.getEnglishScore(); System.out.println(sname + "\t" + sno + "\t" + chineseScore + "\t" + mathScore + "\t" + englishScore); } /** * 信息查询@按学号查询 */ public static void searchAsSno() { System.out.print("输入要查询学生的学号:"); Scanner sc = new Scanner(System.in); String sno = sc.nextLine(); System.out.println("***************查询结果***************"); System.out.println("姓名\t学号\t语文\t数学\t英语"); if (sgiMap.containsKey(sno)) { StudentGradeInfo studentsInfo = sgiMap.get(sno); showSearchResult(studentsInfo); } else { System.out.println("没有你要查找的... ..."); } infoSearch(); } /** * 成绩统计菜单显示 */ public static void infoCount() { System.out.println("\t**************************"); System.out.println("\t\t3、成绩统计"); System.out.println("\t**************************"); System.out.println("1、是否进入成绩统计\t2、返回主菜单\t3、退出"); Scanner sc = new Scanner(System.in); System.out.print("请选择:"); String s = sc.next(); if (s.equals("1")) { statisticsScore(); } else if (s.equals("2")) { selectMainMenu(); } else if (s.equals("3")) { exitSystem(); } else { System.out.println("选择有误,请重新选择!"); infoCount(); } } /** * 成绩统计 */ public static void statisticsScore() { System.out.print("请输入课程名:"); Scanner sc = new Scanner(System.in); String course = sc.nextLine(); if (!course.equals("语文") && !course.equals("数学") && !course.equals("英语")) { System.out.println("请输入正确的课程名!"); infoCount(); } else { System.out.print("请输入起始分数:"); String beginScore = sc.nextLine(); System.out.print("请输入结束分数:"); String endScore = sc.nextLine(); if (isNumberScore(beginScore) == false || isNumberScore(endScore) == false) { System.out.println("请输入正确的分数段!"); } else { System.out.println("***************统计结果***************"); System.out.println("姓名\t学号\t语文\t数学\t英语"); for (Iterator<StudentGradeInfo> iter = sgiMap.values() .iterator(); iter.hasNext();) { StudentGradeInfo studentsInfo = iter.next(); double dScore = formatScore(course, studentsInfo); limitScore(dScore, beginScore, endScore, studentsInfo); } } infoCount(); } } /** * 格式化成绩 * * @param course * @param studentsInfo * @return */ public static double formatScore(String course, StudentGradeInfo studentsInfo) { if (course.equals("语文")) { String chineseScore = studentsInfo.getChineseScore(); Double dChineseScore = Double.parseDouble(chineseScore); return dChineseScore; } else if (course.equals("数学")) { String mathScore = studentsInfo.getMathScore(); Double dMathScore = Double.parseDouble(mathScore); return dMathScore; } else if (course.equals("英语")) { String englishScore = studentsInfo.getEnglishScore(); Double dEnglishScore = Double.parseDouble(englishScore); return dEnglishScore; } else { return 0.0; } } /** * 判断成绩的范围 * * @param dScore * @param beginScore * @param endScore * @param studentsInfo */ public static void limitScore(double dScore, String beginScore, String endScore, StudentGradeInfo studentsInfo) { Double dBeginScore = Double.parseDouble(beginScore); Double dEndScore = Double.parseDouble(endScore); if (dScore >= dBeginScore && dScore <= dEndScore) { showSearchResult(studentsInfo); } } /** * 排序,能对用户指定的任意课程名,按成绩升序或降序排列学生数据,并显示排列结果 */ public static void infoOrder() { System.out.println("\t**************************"); System.out.println("\t\t4、排序"); System.out.println("\t**************************"); System.out.println("1、是否进入排序程序\t2、返回主菜单\t3、退出"); Scanner sc = new Scanner(System.in); System.out.print("请选择:"); String s = sc.next(); if (s.equals("1")) { sortOrder(); } else if (s.equals("2")) { selectMainMenu(); } else if (s.equals("3")) { exitSystem(); } else { System.out.println("选择有误,请重新选择!"); infoOrder(); } } /** * 排序程序 */ public static void sortOrder() { System.out.print("请输入需要排序的课程名:"); Scanner sc = new Scanner(System.in); String course = sc.nextLine(); if (!course.equals("语文") && !course.equals("数学") && !course.equals("英语")) { System.out.println("请输入正确的课程名!"); infoOrder(); } System.out.println("\t排序方式"); System.out.println("1、升序\t2、降序"); System.out.print("请选择:"); String s = sc.next(); if (!s.equals("1") && !s.equals("2")) { System.out.println("警告!请选择正确的排序方式!"); sortOrder(); } else { System.out.println("***************排序结果***************"); System.out.println("姓名\t学号\t语文\t数学\t英语"); // ArrayList<StudentGradeInfo> arrayListStudentsInfo = new // ArrayList<StudentGradeInfo>(); List<Double> listStudentInfo = new ArrayList<Double>(); ergodicOrder(course, listStudentInfo); if (s.equals("1")) { ascOrder(listStudentInfo); printOrder(course, listStudentInfo); infoOrder(); } else if (s.equals("2")) { descOrder(listStudentInfo); printOrder(course, listStudentInfo); infoOrder(); } } } /** * 排序程序@遍历出排序后的List,然后将排序后的结构利用Map接收,再逐一打印出来 * * @param course * @param listStudentInfo */ public static void printOrder(String course, List<Double> listStudentInfo) { // 判断dScore是不是第二次及以上次出现 double d = 0.0; for (int i = 0; i < listStudentInfo.size(); i++) { double dListScore = listStudentInfo.get(i); for (Iterator<StudentGradeInfo> iter = sgiMap.values().iterator(); iter .hasNext();) { StudentGradeInfo studentsInfo = iter.next(); double dScore = formatScore(course, studentsInfo); if (dListScore == dScore && d != dScore) { d = dScore; showSearchResult(studentsInfo); } } } } /** * 排序程序@遍历出各科成绩,然后将遍历出的成绩加到List上 * * @param course * @param listStudentInfo */ public static void ergodicOrder(String course, List<Double> listStudentInfo) { for (Iterator<StudentGradeInfo> iter = sgiMap.values().iterator(); iter .hasNext();) { StudentGradeInfo studentsInfo = iter.next(); double dScore = formatScore(course, studentsInfo); listStudentInfo.add(dScore); } } /** * 排序程序@升序排列 */ public static void ascOrder(List<Double> listStudentInfo) { Collections.sort(listStudentInfo); } /** * 排序程序@降序排列 */ public static void descOrder(List<Double> listStudentInfo) { DescComparator descCom = new StudentGradeManage().new DescComparator(); Collections.sort(listStudentInfo, descCom); } /** * 内部类,用于降序排序 * * @author 许波 * */ public class DescComparator implements Comparator<Double> { @Override public int compare(Double o1, Double o2) { // TODO Auto-generated method stub double d1 = o1; double d2 = o2; if (d1 < d2) { return 1; } else { return 0; } } } /** * 判断成绩分数是否是在0~100之间 * * @param str * @return */ public static boolean isNumberScore(String str) { try { Double d = Double.parseDouble(str); if (d < 0 || d > 100) { return false; } } catch (NumberFormatException e) { return false; } catch (Exception e) { return false; } return true; } /** * 判断学号是否是在1~200以内的有效数字 * * @param str * @return */ public static boolean isNumberSno(String str) { try { Integer i = Integer.parseInt(str); if (i <= 0 || i > 200) { return false; } } catch (NumberFormatException e) { return false; } catch (Exception e) { return false; } return true; } /** * 增加学生信息 */ public static void addStudentGradeInfo() { Scanner scStudentsInfo = new Scanner(System.in); System.out.print("请输入学生姓名:"); String sname = scStudentsInfo.nextLine(); System.out.print("请输入学生学号:"); String sno = scStudentsInfo.nextLine(); if (isNumberSno(sno) == false) { System.out.println("学号输入有误,请输入1~200之间的任意数字"); addStudentGradeInfo(); } else { System.out.print("请输入该学生语文成绩:"); String chineseScore = scStudentsInfo.nextLine(); System.out.print("请输入该学生数学成绩:"); String mathScore = scStudentsInfo.nextLine(); System.out.print("请输入该学生英语成绩:"); String englishScore = scStudentsInfo.nextLine(); if (isNumberScore(chineseScore) == false || isNumberScore(mathScore) == false || isNumberScore(englishScore) == false) { System.out.println("提示!成绩只能是0~100之间的任意数字,请重修输入"); addStudentGradeInfo(); } else { StudentGradeInfo students = new StudentGradeInfo(); students.setSname(sname); students.setSno(sno); students.setChineseScore(chineseScore); students.setMathScore(mathScore); students.setEnglishScore(englishScore); // 判断是否存在此学号的学生,如果存在,就不允许增加新的学生;反之则允许增加 if (sgiMap.containsKey(sno)) { System.out.println("提示!已经有学生占用了该学号!"); } else { // 写入文件并放入Map中 sgiMap.put(sno, students); writeStudentGradeInfo(); System.out.println("增加学生成绩信息成功!"); } infoManage(); } } } /** * 写入文件 */ public static void writeStudentGradeInfo() { FileOutputStream fos = null; ObjectOutputStream oos = null; try { fos = new FileOutputStream(strFileName); oos = new ObjectOutputStream(fos); oos.writeObject(sgiMap); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (oos != null) { oos.close(); } if (fos != null) { fos.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 删除学生信息 */ public static void deleteStudentGradeInfo() { System.out.print("请输入学生学号:"); Scanner sc = new Scanner(System.in); String sno = sc.nextLine(); if (sgiMap.containsKey(sno)) { sgiMap.remove(sno); // 移除学生过后,重新写入文件 writeStudentGradeInfo(); System.out.println("删除成功!"); infoManage(); } else { System.out.println("该学生不存在或输入有误,请重新输入!"); deleteStudentGradeInfo(); } } /** * 修改学生信息 */ public static void modifyStudentGradeInfo() { System.out.print("请输入要修改的学生学号:"); Scanner sc = new Scanner(System.in); String sno = sc.nextLine(); if (sgiMap.containsKey(sno)) { sgiMap.remove(sno); System.out.println("*******请输入要修改的相关信息*********"); System.out.print("学生姓名:"); String sname = sc.nextLine(); System.out.print("语文成绩:"); String chineseScore = sc.nextLine(); System.out.print("数学成绩:"); String mathScore = sc.nextLine(); System.out.print("英语成绩:"); String englishScore = sc.nextLine(); if (isNumberScore(chineseScore) == false || isNumberScore(mathScore) == false || isNumberScore(englishScore) == false) { System.out.println("提示!成绩只能是0~100之间的任意数字,请重修输入"); modifyStudentGradeInfo(); } else { StudentGradeInfo students = new StudentGradeInfo(); students.setSname(sname); students.setSno(sno); students.setChineseScore(chineseScore); students.setMathScore(mathScore); students.setEnglishScore(englishScore); sgiMap.put(sno, students); writeStudentGradeInfo(); System.out.println("修改成功!"); infoManage(); } } else { System.out.println("没有该学生或者输入有误,请正确输入!"); modifyStudentGradeInfo(); } } /** * 加载文件信息 */ public static void loadStudentGradeInfoFile() { File file = new File(strFileName); // 读取文件 ObjectInputStream ois = null; FileInputStream fis; try { if (file.exists()) { fis = new FileInputStream(file); ois = new ObjectInputStream(fis); sgiMap = (HashMap<String, StudentGradeInfo>) ois.readObject(); } else { sgiMap = new HashMap<String, StudentGradeInfo>(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 退出 */ public static void exitSystem() { System.out.println(); System.out.println("正在退出... ..."); System.out.println("**********************"); System.out.println("********退出**********"); System.out.println("1、是\t2、否"); Scanner sc = new Scanner(System.in); System.out.print("您确认退出:"); String s = sc.next(); if (s.equals("1")) { System.out.println("你已成功退出该系统!"); System.exit(0); } else if (s.equals("2")) { selectMainMenu(); } else { System.out.println("错误!请从以上选项中选择"); exitSystem(); } } }