From 1ebc7023c6aa4f9b184cae9c4ca362888d447485 Mon Sep 17 00:00:00 2001 From: zyx <1029606625@qq.com> Date: Sat, 29 Oct 2022 16:02:48 +0800 Subject: [PATCH] =?UTF-8?q?2022/10/29=2016:02=20=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A8=EF=BC=9AJava=20IO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/modules.xml | 2 + .idea/statistic.xml | 7 + Week9/Lesson9/Lesson9.iml | 11 + .../Lesson9/src/work1/CalculateQuestion.java | 69 ++++ Week9/Lesson9/src/work1/question.txt | 6 + Week9/Lesson9/src/work2/SqlSimulate.java | 322 ++++++++++++++++++ Week9/Lesson9/src/work2/Student.java | 79 +++++ Week9/Lesson9/src/work2/data.txt | 9 + Week9/Lesson9/src/work3/Field.java | 19 ++ Week9/Lesson9/src/work3/GenerateCode.java | 96 ++++++ Week9/Week9.iml | 11 + 11 files changed, 631 insertions(+) create mode 100644 .idea/statistic.xml create mode 100644 Week9/Lesson9/Lesson9.iml create mode 100644 Week9/Lesson9/src/work1/CalculateQuestion.java create mode 100644 Week9/Lesson9/src/work1/question.txt create mode 100644 Week9/Lesson9/src/work2/SqlSimulate.java create mode 100644 Week9/Lesson9/src/work2/Student.java create mode 100644 Week9/Lesson9/src/work2/data.txt create mode 100644 Week9/Lesson9/src/work3/Field.java create mode 100644 Week9/Lesson9/src/work3/GenerateCode.java create mode 100644 Week9/Week9.iml diff --git a/.idea/modules.xml b/.idea/modules.xml index 78a93a1..47ad9b5 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,6 +2,7 @@ + @@ -9,6 +10,7 @@ + diff --git a/.idea/statistic.xml b/.idea/statistic.xml new file mode 100644 index 0000000..c38f2df --- /dev/null +++ b/.idea/statistic.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/Week9/Lesson9/Lesson9.iml b/Week9/Lesson9/Lesson9.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Week9/Lesson9/Lesson9.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Week9/Lesson9/src/work1/CalculateQuestion.java b/Week9/Lesson9/src/work1/CalculateQuestion.java new file mode 100644 index 0000000..a83d309 --- /dev/null +++ b/Week9/Lesson9/src/work1/CalculateQuestion.java @@ -0,0 +1,69 @@ +package work1; + +import java.io.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class CalculateQuestion { + public static void main(String[] args) { + String regex = "((add)|(sub)|(max)|(muti)|(div)|(min)|(doubleMe))\\(\\d+(.\\d+)?,\\d+(.\\d+)?\\)"; + Pattern p = Pattern.compile(regex); + + try(BufferedReader br = new BufferedReader(new FileReader("Week9/Lesson9/src/work1/question.txt")); + BufferedWriter bw = new BufferedWriter(new FileWriter("Week9/Lesson9/src/work1/answer.txt")) + ) { + String expression = ""; + while ((expression=br.readLine()) != null){ + String expressionStr = expression; + String res = ""; + if("doubleMe".equals(expression.substring(0, expression.indexOf("(")))){ + res = calcExpression(expression)+""; + } else { + Matcher matcher = p.matcher(expression); + while (matcher.find()) { + String subStr = matcher.group(); + res = calcExpression(subStr) + ""; + expression = expression.replaceAll(regex, res); + matcher = p.matcher(expression); + } + } + bw.write(expressionStr + "=" + res + "\n"); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static double calcExpression(String expression){ + double res = 0; + String exp = expression.substring(0, expression.indexOf("(")); + if ("doubleMe".equals(exp)){ + double num = Double.parseDouble(expression.substring(expression.indexOf("(")+1,expression.indexOf(")"))); + res = num*2; + } else { + double num1 = Double.parseDouble(expression.substring(expression.indexOf("(")+1, expression.indexOf(","))); + double num2 = Double.parseDouble(expression.substring(expression.indexOf(",")+1, expression.indexOf(")"))); + switch (exp){ + case "add": + res = num1+num2; + break; + case "sub": + res = num1-num2; + break; + case "muti": + res = num1*num2; + break; + case "div": + res = num1/num2; + break; + case "max": + res = Math.max(num1, num2); + break; + case "min": + res = Math.min(num1, num2); + break; + } + } + return res; + } +} diff --git a/Week9/Lesson9/src/work1/question.txt b/Week9/Lesson9/src/work1/question.txt new file mode 100644 index 0000000..e19cfca --- /dev/null +++ b/Week9/Lesson9/src/work1/question.txt @@ -0,0 +1,6 @@ +add(23,45) +sub(44,12) +muti(3,5) +div(54,9) +doubleMe(5) +add(max(32.2,3),doubleMe(div(9,3))) \ No newline at end of file diff --git a/Week9/Lesson9/src/work2/SqlSimulate.java b/Week9/Lesson9/src/work2/SqlSimulate.java new file mode 100644 index 0000000..12f9f99 --- /dev/null +++ b/Week9/Lesson9/src/work2/SqlSimulate.java @@ -0,0 +1,322 @@ +package work2; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.text.SimpleDateFormat; +import java.util.*; + +public class SqlSimulate { + public static void main(String[] args) { + List students = new ArrayList<>(); + Scanner scanner = new Scanner(System.in); + while (true) { + System.out.print("请输入命令(输入help显示帮助信息):"); + String opt = scanner.nextLine(); + if ("exit".equalsIgnoreCase(opt)) { + System.out.println("欢迎下次使用!"); + return; + } + + String[] optSplit = opt.split(" "); + String command = optSplit[0]; + if ("help".equalsIgnoreCase(command)) { + help(); + } else if ("load".equalsIgnoreCase(command)) { + students = load(optSplit[1]); + } else if ("sort".equalsIgnoreCase(command)) { + sort(students, optSplit[1]); + } else if ("select".equalsIgnoreCase(command)) { + boolean existWhere = false; + List columns = new ArrayList<>(); + List conditions = new ArrayList<>(); + for (int i = 1; i < optSplit.length; i++) { + if (!"where".equalsIgnoreCase(optSplit[i]) && !existWhere) { + columns.add(optSplit[i]); + } else { + existWhere = true; + if(!"where".equalsIgnoreCase(optSplit[i])) + conditions.add(optSplit[i]); + } + } + select(students, columns, existWhere, conditions); + } + } + } + + /** + * 输出帮助信息 + */ + public static void help() { + System.out.println("--------------------------------\n" + + "load data.txt\n" + + "从当刖目录装入文件data .txt,并显示\n" + + "sort 成绩\n" + + "按“成绩”排序,并显示\n" + + "类似地,还可以是 sort 学号,sort 姓名,sort 性别,sort 省份,sort 出生年月等\n" + + "select 学号 姓名\n" + + "只显示学号,姓名两列,显示的列还可以其他任意\n" + + "select 学号 姓名 where 成绩>60\n" + + "只显示 学号,姓名两列,只包含 成绩>60 的行\n" + + "select * where 成绩>60 性别=男\n" + + "显示所有列,只包含成绩>60 且 性别=男 的行\n" + + "(使用select where时,各条件之间以空格分隔,且各条件内部不得有空格)\n" + + "其它的组合,从上边类推\n" + + "exit\n" + + "退出程序\n" + + "--------------------------------\n"); + } + + /** + * 装载指定文件中的数据 + * @param fileName 数据文件路径及文件名 + * @return 文件中的数据转换出的学生列表List< Student> + */ + public static List load(String fileName) { + List list = new ArrayList<>(); + try (BufferedReader br = new BufferedReader(new FileReader(fileName))) { + br.readLine(); + br.readLine(); + String studentStr = ""; + while ((studentStr = br.readLine()) != null) { + String[] studentProperties = studentStr.split(" "); + list.add(new Student(studentProperties[0], studentProperties[1], studentProperties[2], studentProperties[3], + new SimpleDateFormat("yyyy-MM-dd").parse(studentProperties[4]), Double.parseDouble(studentProperties[5]))); + } + System.out.println("装载数据成功!"); + System.out.println("学号\t姓名\t性别\t省份\t出生年月\t\t成绩"); + list.forEach(System.out::println); + System.out.println(); + } catch (Exception e) { + System.out.println("装载数据失败!"); + } + return list; + } + + + /** + * 根据指定关键字排序 + * @param students 待排序的学生列表 + * @param sortBy 排序关键字 + */ + public static void sort(List students, String sortBy) { + switch (sortBy) { + case "学号": + students.sort(Comparator.comparing(Student::getId)); + break; + case "姓名": + students.sort(Comparator.comparing(Student::getName)); + break; + case "性别": + students.sort(Comparator.comparing(Student::getGender)); + break; + case "省份": + students.sort(Comparator.comparing(Student::getProvince)); + break; + case "出生年月": + students.sort(Comparator.comparing(Student::getBirth)); + break; + case "成绩": + students.sort(Comparator.comparing(Student::getScore)); + break; + } + System.out.println("排序成功!"); + System.out.println("学号\t姓名\t性别\t省份\t出生年月\t\t成绩"); + students.forEach(System.out::println); + System.out.println(); + } + + /** + * 输出属性标题行 + * @param columns 待输出的各属性名 + */ + public static void printColumns(List columns) { + for (String column : columns) { + System.out.print(column + "\t"); + if ("出生年月".equals(column)) + System.out.print("\t"); + } + System.out.println(); + } + + /** + * 根据指定属性输出学生信息 + * @param student 待输出信息的学生 + * @param columns 指定的各属性 + */ + public static void printSingleStudentByColumns(Student student, List columns){ + for (String column : columns) { + if ("学号".equals(column)) { + System.out.print(student.getId() + "\t"); + } else if ("姓名".equals(column)) { + System.out.print(student.getName() + "\t"); + } else if ("性别".equals(column)) { + System.out.print(student.getGender() + "\t\t"); + } else if ("省份".equals(column)) { + System.out.print(student.getProvince() + "\t"); + } else if ("出生年月".equals(column)) { + System.out.print(new SimpleDateFormat("yyyy-MM-dd").format(student.getBirth()) + "\t"); + } else if ("成绩".equals(column)) { + System.out.print(student.getScore() + "\t"); + } + } + System.out.println(); + } + + /** + * 查询学生信息 + * @param students 所有学生信息 + * @param columns 待查询的所有属性 + * @param existWhere 是否存在where关键字 + * @param conditions 查询条件 + */ + public static void select(List students, List columns, boolean existWhere, List conditions) { + if (columns.size() == 1 && "*".equals(columns.get(0))) { + columns.clear(); + columns.add("学号"); + columns.add("姓名"); + columns.add("性别"); + columns.add("省份"); + columns.add("出生年月"); + columns.add("成绩"); + } + + if (!existWhere) { + printColumns(columns); + for (Student student : students) { + printSingleStudentByColumns(student, columns); + } + } else { + Map conditionsMap = new HashMap<>(); + for (String condition : conditions) { + String[] conditionSplit = condition.split("[<>=]"); + //成绩>60 => {"成绩": [">", "60"]} + conditionsMap.put(conditionSplit[0], new String[]{condition.substring(conditionSplit.length, condition.indexOf(conditionSplit[1])), conditionSplit[1]}); + } + + printColumns(columns); + + for (Student student : students) { + boolean validate = true; + for (String column : columns) { + if ("学号".equals(column)) { + if (conditionsMap.containsKey("学号")) { + String[] condition = conditionsMap.get("学号"); + switch (condition[0]) { + case "<": + if (!(student.getId().compareTo(condition[1]) < 0)) + validate = false; + break; + case "=": + if (!(student.getId().compareTo(condition[1]) == 0)) + validate = false; + break; + case ">": + if (!(student.getId().compareTo(condition[1]) > 0)) + validate = false; + break; + } + } + } else if ("姓名".equals(column)) { + if (conditionsMap.containsKey("姓名")) { + String[] condition = conditionsMap.get("姓名"); + switch (condition[0]) { + case "<": + if (!(student.getName().compareTo(condition[1]) < 0)) + validate = false; + break; + case "=": + if (!(student.getName().compareTo(condition[1]) == 0)) + validate = false; + break; + case ">": + if (!(student.getName().compareTo(condition[1]) > 0)) + validate = false; + break; + } + } + } else if ("性别".equals(column)) { + if (conditionsMap.containsKey("性别")) { + String[] condition = conditionsMap.get("性别"); + switch (condition[0]) { + case "<": + if (!(student.getGender().compareTo(condition[1]) < 0)) + validate = false; + break; + case "=": + if (!(student.getGender().compareTo(condition[1]) == 0)) + validate = false; + break; + case ">": + if (!(student.getGender().compareTo(condition[1]) > 0)) + validate = false; + break; + } + } + } else if ("省份".equals(column)) { + if (conditionsMap.containsKey("省份")) { + String[] condition = conditionsMap.get("省份"); + switch (condition[0]) { + case "<": + if (!(student.getProvince().compareTo(condition[1]) < 0)) + validate = false; + break; + case "=": + if (!(student.getProvince().compareTo(condition[1]) == 0)) + validate = false; + break; + case ">": + if (!(student.getProvince().compareTo(condition[1]) > 0)) + validate = false; + break; + } + } + } else if ("出生年月".equals(column)) { + if (conditionsMap.containsKey("出生年月")) { + String[] condition = conditionsMap.get("出生年月"); + try { + switch (condition[0]) { + case "<": + if (!(student.getBirth().compareTo(new SimpleDateFormat("yyyy-MM-dd").parse(condition[1])) < 0)) + validate = false; + break; + case "=": + if (!(student.getBirth().compareTo(new SimpleDateFormat("yyyy-MM-dd").parse(condition[1])) == 0)) + validate = false; + break; + case ">": + if (!(student.getBirth().compareTo(new SimpleDateFormat("yyyy-MM-dd").parse(condition[1])) > 0)) + validate = false; + break; + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } else if ("成绩".equals(column)) { + if (conditionsMap.containsKey("成绩")) { + String[] condition = conditionsMap.get("成绩"); + switch (condition[0]) { + case "<": + if (!(student.getScore() < Double.parseDouble(condition[1]))) + validate = false; + break; + case "=": + if (!(student.getScore() == Double.parseDouble(condition[1]))) + validate = false; + break; + case ">": + if (!(student.getScore() > Double.parseDouble(condition[1]))) + validate = false; + break; + } + } + } + } + if (validate) { + printSingleStudentByColumns(student, columns); + } + } + } + System.out.println(); + } +} diff --git a/Week9/Lesson9/src/work2/Student.java b/Week9/Lesson9/src/work2/Student.java new file mode 100644 index 0000000..ef5d1c1 --- /dev/null +++ b/Week9/Lesson9/src/work2/Student.java @@ -0,0 +1,79 @@ +package work2; + +import java.text.SimpleDateFormat; +import java.util.Date; + +public class Student { + private String id; + private String name; + private String gender; + private String province; + private Date birth; + private Double score; + + public Student() { + } + + public Student(String id, String name, String gender, String province, Date birth, Double score) { + this.id = id; + this.name = name; + this.gender = gender; + this.province = province; + this.birth = birth; + this.score = score; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getGender() { + return gender; + } + + public void setGender(String gender) { + this.gender = gender; + } + + public String getProvince() { + return province; + } + + public void setProvince(String province) { + this.province = province; + } + + public Date getBirth() { + return birth; + } + + public void setBirth(Date birth) { + this.birth = birth; + } + + public Double getScore() { + return score; + } + + public void setScore(Double score) { + this.score = score; + } + + @Override + public String toString() { + return id + "\t" + name + "\t" + gender + "\t\t" + province + "\t" + + new SimpleDateFormat("yyyy-MM-dd").format(birth) + "\t" + score; + } +} diff --git a/Week9/Lesson9/src/work2/data.txt b/Week9/Lesson9/src/work2/data.txt new file mode 100644 index 0000000..832978e --- /dev/null +++ b/Week9/Lesson9/src/work2/data.txt @@ -0,0 +1,9 @@ +学号 姓名 性别 省份 出生年月 成绩 +---------------------------------------------------------------- +1001 小王 男 湖北 1998-11-12 90 +1002 小刘 女 北京 1997-01-12 60.5 +1003 小张 女 湖南 1997-11-01 86 +1004 王艳 男 广东 1998-11-14 55 +1005 刘华 女 云南 1998-09-12 76 +1006 李华 男 贵州 1998-07-12 92 +1007 张艳 女 湖北 1998-10-12 66 \ No newline at end of file diff --git a/Week9/Lesson9/src/work3/Field.java b/Week9/Lesson9/src/work3/Field.java new file mode 100644 index 0000000..22606d2 --- /dev/null +++ b/Week9/Lesson9/src/work3/Field.java @@ -0,0 +1,19 @@ +package work3; + +public class Field { + private String attributeName; + private String attributeType; + + public Field(String attributeName, String attributeType) { + this.attributeName = attributeName; + this.attributeType = attributeType; + } + + public String getAttributeName() { + return attributeName; + } + + public String getAttributeType() { + return attributeType; + } +} diff --git a/Week9/Lesson9/src/work3/GenerateCode.java b/Week9/Lesson9/src/work3/GenerateCode.java new file mode 100644 index 0000000..badfae2 --- /dev/null +++ b/Week9/Lesson9/src/work3/GenerateCode.java @@ -0,0 +1,96 @@ +package work3; + +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class GenerateCode { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + System.out.println("请输入类名:"); + String className = scanner.nextLine(); + System.out.println("请输入成员变量的名称和类型(如studentId,String),以'end'结束输入:"); + + List fields = new ArrayList<>(); + while (true){ + String fieldStr = scanner.nextLine(); + if ("end".equals(fieldStr)){ + break; + } + String[] nameAndType = fieldStr.split("[,,]"); + fields.add(new Field(nameAndType[0], nameAndType[1])); + } + writeCode(className, fields); + } + + public static void writeCode(String className, List fields){ + try(BufferedWriter bw = new BufferedWriter(new FileWriter( className + ".java"))) { + //类开始 + bw.write("public class " + className + " {\n"); + + int size = fields.size(); + //成员变量 + for (Field field : fields) { + bw.write("\tprivate " + field.getAttributeType() + " " + field.getAttributeName() + ";\n"); + } + bw.write("\n"); + + //无参构造 + bw.write("\tpublic " + className + "() {\n\n\t}\n\n"); + + //全参构造参数列表 + bw.write("\tpublic " + className + "("); + for (int i = 0; i < size; i++) { + Field field = fields.get(i); + bw.write(field.getAttributeType() + " " + field.getAttributeName()); + if(i != size-1){ + bw.write(", "); + } + } + bw.write(") {\n"); + //全参构造函数体 + for (Field field : fields) { + bw.write("\t\tthis." + field.getAttributeName() + " = " + field.getAttributeName() + ";\n"); + } + bw.write("\t}\n\n"); + + //各成员变量的get/set方法 + for (Field field : fields) { + //用于get/set方法的属性名 + String attributeName = field.getAttributeName(); + attributeName = attributeName.substring(0, 1).toUpperCase() + attributeName.substring(1); + //get方法 + bw.write("\tpublic " + field.getAttributeType() + " get" + attributeName + "() {\n"); + bw.write("\t\treturn " + field.getAttributeName() + ";\n\t}\n\n"); + //set方法 + bw.write("\tpublic void" + " set" + attributeName + "(" + + field.getAttributeType() + " " + field.getAttributeName() + ") {\n"); + bw.write("\t\tthis." + field.getAttributeName() + " = " + field.getAttributeName() + ";\n\t}\n\n"); + } + + //toString方法 + bw.write("\t@Override\n\tpublic String toString() {\n"); + bw.write("\t\treturn \"" + className + "{\" +\n"); + for (int i = 0; i < size; i++) { + Field field = fields.get(i); + bw.write("\t\t\t\t\""); + if(i != 0){ + bw.write(", "); + } + if ("String".equals(field.getAttributeType())) { + bw.write(field.getAttributeName() + "='\" + " + field.getAttributeName() + " + '\\'' +\n"); + } else { + bw.write(field.getAttributeName() + "=\" + " + field.getAttributeName() + " + \n"); + } + } + bw.write("\t\t\t\t'}';\n\t}\n"); + + //类结束 + bw.write("}"); + } catch (Exception e){ + e.printStackTrace(); + } + } +} diff --git a/Week9/Week9.iml b/Week9/Week9.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Week9/Week9.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file