import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; //import static FileOperation; //javac -encoding UTF-8 xxx.java class Menu { private static ArrayList sources = new ArrayList<>(); public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); int choice;//选项 while (true) { showMenu(); try { choice = in.nextInt(); switch (choice) { case 1: try { sources.add(newSource()); System.out.println("添加成功"); } catch (Exception e) { System.out.println("添加失败。。等下重试"); } break; case 2: System.out.println("课程列表如下:"); readSource(); break; case 3: querySource(); break; case 4: saveSource(); break; default: System.out.println("输入有误,请重试"); break; } } catch (Exception e) { System.out.println("输入有误,请重试"); } } } public static void showMenu() { System.out.println(); System.out.println(); System.out.println("====================="); System.out.println("=欢迎使用课程选修系统="); System.out.println("=1. 添加课程 ="); System.out.println("=2. 显示所有课程 ="); System.out.println("=3. 查询课程 ="); System.out.println("=4. 保存课程 ="); System.out.println("====================="); System.out.println("输入你的选择:"); } private static Source newSource() { /* * 课程 * 课程编号,课程名称, 课程性质(公共课、必修课、选修课),总学时,授课学时, 实验或上机学时,学分,开课学期等信息 **/ Scanner in = new Scanner(System.in); String sNo;//uuid String sName; String sXingzhi; int sShouke; int sShiyan; double sXuefen; String sXueqi; sNo=Utils.uuid(); System.out.println("输入课程名称:"); sName=in.next(); System.out.println("输入课程性质(1--选修,2--必修):"); sXingzhi=in.next(); System.out.println("输入授课学时(数字):"); sShouke=in.nextInt(); System.out.println("输入实验学时(数字):"); sShiyan=in.nextInt(); System.out.println("输入学分:"); sXuefen=in.nextDouble(); System.out.println("输入开课学期:"); sXueqi=in.next(); return new Source(sNo,sName,sXingzhi,sShouke,sShiyan,sXuefen,sXueqi); } private static void querySource() throws IOException { System.out.println("请输入查询类别:"); System.out.println("1---按照学分查询"); System.out.println("2---按照课程性质查询"); Scanner in = new Scanner(System.in); int choice = in.nextInt(); double key; if (choice == 1) { System.out.println("输入学分:"); key = in.nextDouble(); for (Source sc : sources) { if (sc.getsXuefen() == key) { System.out.println(sc); } } } else if (choice == 2) { System.out.println("输入查询课程性质(1--选修,2--必修):"); String xingzhi = in.next(); for (Source sc : sources) { if (sc.getsXingzhi().equals(xingzhi)) { System.out.println(sc); } } } else { System.out.println("输入有误"); } } private static void readSource() throws IOException { FileIO.read(sources); } private static void saveSource(){ FileIO.write(sources); System.out.println("写入成功"); } }