import java.util.UUID; public class Utils { /* 设置UUid */ public static String uuid(){ String uuid= UUID.randomUUID().toString(); char[] cs=new char[32]; int j=0; for(int i=uuid.length();i-->0;){ if(uuid.charAt(i)!='-'){ cs[j++]=uuid.charAt(i); } } return new String(cs); } } ---------------------------------------------------------------------------------------
/** * 课程 * 课程编号,课程名称, 课程性质(公共课、必修课、选修课),总学时,授课学时 */ public class Source { private String sNo;//uuid private String sName; private String sXingzhi; //private int sZongxueshi;总学时=授课+实验 private int sShouke; private int sShiyan; private double sXuefen; private String sXueqi; public String getsNo() { return sNo; } public void setsNo(String sNo) { this.sNo = sNo; } public String getsName() { return sName; } public void setsName(String sName) { this.sName = sName; } public String getsXingzhi() { return sXingzhi; } public void setsXingzhi(String sXingzhi) { this.sXingzhi = sXingzhi; } public int getsShouke() { return sShouke; } public void setsShouke(int sShouke) { this.sShouke = sShouke; } public int getsShiyan() { return sShiyan; } public void setsShiyan(int sShiyan) { this.sShiyan = sShiyan; } public double getsXuefen() { return sXuefen; } public void setsXuefen(double sXuefen) { this.sXuefen = sXuefen; } public String getsXueqi() { return sXueqi; } public void setsXueqi(String sXueqi) { this.sXueqi = sXueqi; } public Source(String sNo, String sName, String sXingzhi, int sShouke, int sShiyan, double sXuefen, String sXueqi) { this.sNo = sNo; this.sName = sName; this.sXingzhi = sXingzhi; this.sShouke = sShouke; this.sShiyan = sShiyan; this.sXuefen = sXuefen; this.sXueqi = sXueqi; } public Source() { } @Override public String toString() { return "课程{" + "编号='" + sNo + '\'' + ", 课程名='" + sName + '\'' + ", 课程性质='" + sXingzhi + '\'' + ", 授课学时=" + sShouke + ", 实验学时=" + sShiyan + ", 学分=" + sXuefen + ", 开课学期='" + sXueqi + '\'' + '}'; } }
---------------------------------------------------------------------------------------------------------------------------------
import java.io.*; import java.nio.charset.StandardCharsets; import java.util.ArrayList; class FileIO { public static void read(ArrayList<Source> sources) throws IOException { // 防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw /* 读入TXT文件 */ FileReader fr = new FileReader("d:/naoki/output.txt"); BufferedReader br = new BufferedReader(fr); // 建立一个对象,它把文件内容转成计算机能读懂的语言 String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); } public static void write(ArrayList<Source> sources){ try { /* 写入Txt文件 */ File writename = new File("output.txt"); // 相对路径,如果没有则要建立一个新的output。txt文件 writename.createNewFile(); // 创建新文件 BufferedWriter out = new BufferedWriter(new FileWriter(writename)); for(Source sc:sources){ out.write(sc.toString()+"\r\n"); // \r\n即为换行 } out.flush(); // 把缓存区内容压入文件 out.close(); // 最后记得关闭文件 }catch (Exception e){ e.printStackTrace(); } } }
---------------------------------------------------------------------------------------------------------------------------------
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<Source> 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("写入成功"); } }