From df6b44c5694c0a4bd7f6e5438c0372e28255d1bd Mon Sep 17 00:00:00 2001 From: 643a0f6f2e6b2c0eaeb05acd <643a0f6f2e6b2c0eaeb05acd@devide> Date: Sat, 15 Apr 2023 02:46:52 +0000 Subject: [PATCH] Auto commit --- keshe/keshe.iml | 11 ++++ keshe/out/output.txt | 0 keshe/src/FileIo.java | 34 +++++++++++ keshe/src/Main.java | 4 ++ keshe/src/Menu.java | 128 ++++++++++++++++++++++++++++++++++++++++++ keshe/src/Source.java | 102 +++++++++++++++++++++++++++++++++ keshe/src/Utils.java | 18 ++++++ 7 files changed, 297 insertions(+) create mode 100644 keshe/keshe.iml create mode 100644 keshe/out/output.txt create mode 100644 keshe/src/FileIo.java create mode 100644 keshe/src/Main.java create mode 100644 keshe/src/Menu.java create mode 100644 keshe/src/Source.java create mode 100644 keshe/src/Utils.java diff --git a/keshe/keshe.iml b/keshe/keshe.iml new file mode 100644 index 0000000..9465dd8 --- /dev/null +++ b/keshe/keshe.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/keshe/out/output.txt b/keshe/out/output.txt new file mode 100644 index 0000000..e69de29 diff --git a/keshe/src/FileIo.java b/keshe/src/FileIo.java new file mode 100644 index 0000000..afd8c1f --- /dev/null +++ b/keshe/src/FileIo.java @@ -0,0 +1,34 @@ +import java.io.*; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; + +class FileIO { + public static void read(ArrayList 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 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(); + } + } +} \ No newline at end of file diff --git a/keshe/src/Main.java b/keshe/src/Main.java new file mode 100644 index 0000000..01e0b25 --- /dev/null +++ b/keshe/src/Main.java @@ -0,0 +1,4 @@ +public class Main { + public static void main(String[] args) { + } +} \ No newline at end of file diff --git a/keshe/src/Menu.java b/keshe/src/Menu.java new file mode 100644 index 0000000..c8b7fc1 --- /dev/null +++ b/keshe/src/Menu.java @@ -0,0 +1,128 @@ +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("写入成功"); + } +} + diff --git a/keshe/src/Source.java b/keshe/src/Source.java new file mode 100644 index 0000000..4c74c4b --- /dev/null +++ b/keshe/src/Source.java @@ -0,0 +1,102 @@ +/** + * 课程 + * 课程编号,课程名称, + 课程性质(公共课、必修课、选修课),总学时,授课学时 + */ +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 + '\'' + + '}'; + } +} diff --git a/keshe/src/Utils.java b/keshe/src/Utils.java new file mode 100644 index 0000000..c6a11fe --- /dev/null +++ b/keshe/src/Utils.java @@ -0,0 +1,18 @@ +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); + } +} -- GitLab