2022/10/29 16:02 完成作业8:Java IO
This commit is contained in:
parent
56e026d12f
commit
1ebc7023c6
2
.idea/modules.xml
generated
2
.idea/modules.xml
generated
@ -2,6 +2,7 @@
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/Week9/Lesson9/Lesson9.iml" filepath="$PROJECT_DIR$/Week9/Lesson9/Lesson9.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/Week2/Week2.iml" filepath="$PROJECT_DIR$/Week2/Week2.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/Week3/Week3.iml" filepath="$PROJECT_DIR$/Week3/Week3.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/Week4/Week4.iml" filepath="$PROJECT_DIR$/Week4/Week4.iml" />
|
||||
@ -9,6 +10,7 @@
|
||||
<module fileurl="file://$PROJECT_DIR$/Week6/Week6.iml" filepath="$PROJECT_DIR$/Week6/Week6.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/Week7/Week7.iml" filepath="$PROJECT_DIR$/Week7/Week7.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/Week8/Week8.iml" filepath="$PROJECT_DIR$/Week8/Week8.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/Week9/Week9.iml" filepath="$PROJECT_DIR$/Week9/Week9.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/java_class.iml" filepath="$PROJECT_DIR$/java_class.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/week1/week1.iml" filepath="$PROJECT_DIR$/week1/week1.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/work10/work10.iml" filepath="$PROJECT_DIR$/work10/work10.iml" />
|
||||
|
7
.idea/statistic.xml
generated
Normal file
7
.idea/statistic.xml
generated
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Statistic">
|
||||
<option name="excludeCompilerOutput" value="false" />
|
||||
<option name="excludeIdea9ArtifactDirectory" value="false" />
|
||||
</component>
|
||||
</project>
|
11
Week9/Lesson9/Lesson9.iml
Normal file
11
Week9/Lesson9/Lesson9.iml
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
69
Week9/Lesson9/src/work1/CalculateQuestion.java
Normal file
69
Week9/Lesson9/src/work1/CalculateQuestion.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
6
Week9/Lesson9/src/work1/question.txt
Normal file
6
Week9/Lesson9/src/work1/question.txt
Normal file
@ -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)))
|
322
Week9/Lesson9/src/work2/SqlSimulate.java
Normal file
322
Week9/Lesson9/src/work2/SqlSimulate.java
Normal file
@ -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<Student> 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<String> columns = new ArrayList<>();
|
||||
List<String> 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<Student> load(String fileName) {
|
||||
List<Student> 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<Student> 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<String> 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<String> 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<Student> students, List<String> columns, boolean existWhere, List<String> 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<String, String[]> 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();
|
||||
}
|
||||
}
|
79
Week9/Lesson9/src/work2/Student.java
Normal file
79
Week9/Lesson9/src/work2/Student.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
9
Week9/Lesson9/src/work2/data.txt
Normal file
9
Week9/Lesson9/src/work2/data.txt
Normal file
@ -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
|
19
Week9/Lesson9/src/work3/Field.java
Normal file
19
Week9/Lesson9/src/work3/Field.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
96
Week9/Lesson9/src/work3/GenerateCode.java
Normal file
96
Week9/Lesson9/src/work3/GenerateCode.java
Normal file
@ -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<Field> 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<Field> 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();
|
||||
}
|
||||
}
|
||||
}
|
11
Week9/Week9.iml
Normal file
11
Week9/Week9.iml
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
Loading…
x
Reference in New Issue
Block a user