diff --git a/.idea/compiler.xml b/.idea/compiler.xml
new file mode 100644
index 0000000..7728699
--- /dev/null
+++ b/.idea/compiler.xml
@@ -0,0 +1,17 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml
new file mode 100644
index 0000000..a468a99
--- /dev/null
+++ b/.idea/jarRepositories.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/libraries/Maven__org_projectlombok_lombok_1_18_22.xml b/.idea/libraries/Maven__org_projectlombok_lombok_1_18_22.xml
new file mode 100644
index 0000000..0c3a557
--- /dev/null
+++ b/.idea/libraries/Maven__org_projectlombok_lombok_1_18_22.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 89a2b57..a1cc96d 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -1,5 +1,12 @@
+
+
+
diff --git a/vegetables/pom.xml b/vegetables/pom.xml
new file mode 100644
index 0000000..162ebd9
--- /dev/null
+++ b/vegetables/pom.xml
@@ -0,0 +1,25 @@
+
+
+ 4.0.0
+
+ vegetables
+ vegetables
+ 0.0.0
+
+
+ 8
+ 8
+
+
+
+
+ org.projectlombok
+ lombok
+ 1.18.22
+ provided
+
+
+
+
\ No newline at end of file
diff --git a/vegetables/resources/empty.txt b/vegetables/resources/empty.txt
new file mode 100644
index 0000000..e69de29
diff --git a/vegetables/resources/worn.txt b/vegetables/resources/worn.txt
new file mode 100644
index 0000000..172dd82
--- /dev/null
+++ b/vegetables/resources/worn.txt
@@ -0,0 +1,4 @@
+大白菜 1.4 1.7 2.0 2022-03-24 00:00:00
+大白菜 2.5 2.6 2.7 2022-03-24 00:00:00
+娃娃菜 1.5 1.6s5 1.8 2022-03-24 00:00:00
+小白菜 1.2 1.35 1.5 2022-03-24 00:00:00
\ No newline at end of file
diff --git a/vegetables/src/me/lensfrex/vegetables/VegetablesMain.java b/vegetables/src/main/java/me/lensfrex/vegetables/VegetablesMain.java
similarity index 74%
rename from vegetables/src/me/lensfrex/vegetables/VegetablesMain.java
rename to vegetables/src/main/java/me/lensfrex/vegetables/VegetablesMain.java
index 94fa934..1887683 100644
--- a/vegetables/src/me/lensfrex/vegetables/VegetablesMain.java
+++ b/vegetables/src/main/java/me/lensfrex/vegetables/VegetablesMain.java
@@ -1,6 +1,7 @@
package me.lensfrex.vegetables;
import me.lensfrex.vegetables.data.Vegetable;
+import me.lensfrex.vegetables.resopnse.ResponseProcessor;
import me.lensfrex.vegetables.resopnse.UnityResponse;
import java.util.ArrayList;
@@ -18,6 +19,22 @@ public class VegetablesMain {
UnityResponse> response = null;
ResponseProcessor responseProcessor = null;
+ // 测试数据部分不正确时
+ responseProcessor = new ResponseProcessor(System.getProperty("user.dir") + "/resources/worn.txt");
+ response = responseProcessor.getListPage(1, 10);
+ System.out.println("------------------------------------");
+ System.out.println("测试:数据部分不正确时");
+ System.out.println(response);
+ System.out.println("------------------------------------");
+
+ // 测试数据为空时
+ responseProcessor = new ResponseProcessor(System.getProperty("user.dir") + "/resources/empty.txt");
+ response = responseProcessor.getListPage(1, 10);
+ System.out.println("------------------------------------");
+ System.out.println("测试:数据空时");
+ System.out.println(response);
+ System.out.println("------------------------------------");
+
if (args.length != 0) {
System.out.println("Read source data from: " + args[0]);
responseProcessor = new ResponseProcessor(args[0]);
diff --git a/vegetables/src/me/lensfrex/vegetables/data/Vegetable.java b/vegetables/src/main/java/me/lensfrex/vegetables/data/Vegetable.java
similarity index 70%
rename from vegetables/src/me/lensfrex/vegetables/data/Vegetable.java
rename to vegetables/src/main/java/me/lensfrex/vegetables/data/Vegetable.java
index 172c5d8..f6a8cff 100644
--- a/vegetables/src/me/lensfrex/vegetables/data/Vegetable.java
+++ b/vegetables/src/main/java/me/lensfrex/vegetables/data/Vegetable.java
@@ -1,10 +1,13 @@
package me.lensfrex.vegetables.data;
+import lombok.Data;
+
import java.util.Arrays;
/**
* Vegetable的信息数据类,仅用于存储相关的数据结构
*/
+@Data
public class Vegetable {
public static final int PRICE_MIN = 0;
public static final int PRICE_AVG = 1;
@@ -15,14 +18,6 @@ public class Vegetable {
private String priceDate;
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
public double getPrice(int priceType) {
return price[priceType];
}
@@ -31,14 +26,6 @@ public class Vegetable {
this.price[priceType] = price;
}
- public String getPriceDate() {
- return priceDate;
- }
-
- public void setPriceDate(String priceDate) {
- this.priceDate = priceDate;
- }
-
@Override
public String toString() {
return "{" +
diff --git a/vegetables/src/me/lensfrex/vegetables/exceptions/IllegalPageParamException.java b/vegetables/src/main/java/me/lensfrex/vegetables/exceptions/IllegalPageParamException.java
similarity index 100%
rename from vegetables/src/me/lensfrex/vegetables/exceptions/IllegalPageParamException.java
rename to vegetables/src/main/java/me/lensfrex/vegetables/exceptions/IllegalPageParamException.java
diff --git a/vegetables/src/me/lensfrex/vegetables/exceptions/RequestPageTooLargeException.java b/vegetables/src/main/java/me/lensfrex/vegetables/exceptions/RequestPageTooLargeException.java
similarity index 100%
rename from vegetables/src/me/lensfrex/vegetables/exceptions/RequestPageTooLargeException.java
rename to vegetables/src/main/java/me/lensfrex/vegetables/exceptions/RequestPageTooLargeException.java
diff --git a/vegetables/src/main/java/me/lensfrex/vegetables/exceptions/SourceDataWornException.java b/vegetables/src/main/java/me/lensfrex/vegetables/exceptions/SourceDataWornException.java
new file mode 100644
index 0000000..a8ed77e
--- /dev/null
+++ b/vegetables/src/main/java/me/lensfrex/vegetables/exceptions/SourceDataWornException.java
@@ -0,0 +1,7 @@
+package me.lensfrex.vegetables.exceptions;
+
+public class SourceDataWornException extends Exception {
+ public SourceDataWornException(String message) {
+ super(message);
+ }
+}
diff --git a/vegetables/src/main/java/me/lensfrex/vegetables/resopnse/ResponseGenerator.java b/vegetables/src/main/java/me/lensfrex/vegetables/resopnse/ResponseGenerator.java
new file mode 100644
index 0000000..0ae9736
--- /dev/null
+++ b/vegetables/src/main/java/me/lensfrex/vegetables/resopnse/ResponseGenerator.java
@@ -0,0 +1,22 @@
+package me.lensfrex.vegetables.resopnse;
+
+public class ResponseGenerator {
+ /**
+ * 构建请求成功的响应返回类
+ * @param data 请求到的数据
+ * @param 请求的数据类型
+ * @return 构建出的请求响应类
+ */
+ public static UnityResponse success(T data) {
+ return new UnityResponse<>(ResultStatus.SUCCESS, data);
+ }
+
+ /**
+ * 构建请求失败的响应返回类
+ * @param resultStatus 指定状态信息
+ * @return 构建出的请求响应类
+ */
+ public static UnityResponse error(ResultStatus resultStatus) {
+ return new UnityResponse<>(resultStatus, null);
+ }
+}
diff --git a/vegetables/src/me/lensfrex/vegetables/ResponseProcessor.java b/vegetables/src/main/java/me/lensfrex/vegetables/resopnse/ResponseProcessor.java
similarity index 64%
rename from vegetables/src/me/lensfrex/vegetables/ResponseProcessor.java
rename to vegetables/src/main/java/me/lensfrex/vegetables/resopnse/ResponseProcessor.java
index 077a536..599b758 100644
--- a/vegetables/src/me/lensfrex/vegetables/ResponseProcessor.java
+++ b/vegetables/src/main/java/me/lensfrex/vegetables/resopnse/ResponseProcessor.java
@@ -1,10 +1,9 @@
-package me.lensfrex.vegetables;
+package me.lensfrex.vegetables.resopnse;
import me.lensfrex.vegetables.data.Vegetable;
import me.lensfrex.vegetables.exceptions.IllegalPageParamException;
import me.lensfrex.vegetables.exceptions.RequestPageTooLargeException;
-import me.lensfrex.vegetables.resopnse.ResponseTool;
-import me.lensfrex.vegetables.resopnse.UnityResponse;
+import me.lensfrex.vegetables.exceptions.SourceDataWornException;
import me.lensfrex.vegetables.utils.IOUtils;
import java.util.ArrayList;
@@ -16,10 +15,11 @@ import java.util.LinkedList;
*/
public class ResponseProcessor {
- public final String sourceFilePath;
+ private final String sourceFilePath;
/**
* 初始化ResponseProcessor实例,并指定源数据文件的路径
+ *
* @param sourceFilePath 数据源文件路径
*/
public ResponseProcessor(String sourceFilePath) {
@@ -28,12 +28,13 @@ public class ResponseProcessor {
/**
* 从文件中读取并根据参数进行分页
- * @param pageNum 页码(从1开始)
+ *
+ * @param pageNum 页码(从1开始)
* @param pageSize 每页结果的数量
- * @return UnityResponse> 统一的响应类
+ * @return UnityResponse> 统一的响应类
*/
public UnityResponse> getListPage(int pageNum, int pageSize) {
- Tools tools = new Tools();
+ ResponseTool tools = new ResponseTool();
try {
LinkedList sourceData = IOUtils.getListFromFile(sourceFilePath);
@@ -41,16 +42,19 @@ public class ResponseProcessor {
ArrayList result = new ArrayList<>(tools.getListPage(pageNum, pageSize, allVegetables));
- return ResponseTool.success(result, "成功");
+ return ResponseGenerator.success(result);
} catch (RequestPageTooLargeException e) {
e.printStackTrace();
- return ResponseTool.error("请求的页码超过最大值", UnityResponse.ResultCode.PAGE_OUT_OF_MAX);
+ return ResponseGenerator.error(ResultStatus.PAGE_OUT_OF_RANGE);
} catch (IllegalPageParamException e) {
e.printStackTrace();
- return ResponseTool.error("请求的分页参数有误", UnityResponse.ResultCode.PAGE_PARAM_WORN);
+ return ResponseGenerator.error(ResultStatus.PAGE_PARAM_WORN);
+ } catch (SourceDataWornException e) {
+ e.printStackTrace();
+ return ResponseGenerator.error(ResultStatus.SOURCE_DATA_WORN);
} catch (Exception e) {
e.printStackTrace();
- return ResponseTool.error("未知错误", UnityResponse.ResultCode.UNKNOWN_ERROR);
+ return ResponseGenerator.error(ResultStatus.UNKNOWN_ERROR);
}
}
}
diff --git a/vegetables/src/me/lensfrex/vegetables/Tools.java b/vegetables/src/main/java/me/lensfrex/vegetables/resopnse/ResponseTool.java
similarity index 62%
rename from vegetables/src/me/lensfrex/vegetables/Tools.java
rename to vegetables/src/main/java/me/lensfrex/vegetables/resopnse/ResponseTool.java
index 7c8c0e0..469656f 100644
--- a/vegetables/src/me/lensfrex/vegetables/Tools.java
+++ b/vegetables/src/main/java/me/lensfrex/vegetables/resopnse/ResponseTool.java
@@ -1,8 +1,9 @@
-package me.lensfrex.vegetables;
+package me.lensfrex.vegetables.resopnse;
import me.lensfrex.vegetables.data.Vegetable;
import me.lensfrex.vegetables.exceptions.IllegalPageParamException;
import me.lensfrex.vegetables.exceptions.RequestPageTooLargeException;
+import me.lensfrex.vegetables.exceptions.SourceDataWornException;
import java.util.LinkedList;
import java.util.List;
@@ -11,10 +12,7 @@ import java.util.List;
* 请求处理工具
* 由于功能较为简单,目前仅有获取分页数据的功能
*/
-public class Tools {
- // 测试用路径,使用前记得改 :P
- // public static final String SOURCE_FILE_PATH = "C:\\Users\\lenfrex\\projects\\AmberGround\\vegetables\\resources\\vegetables.txt";
-
+public class ResponseTool {
/**
* 根据给定的页索引以及页大小,对全部蔬菜数据进行分页并返回分页后的List。
*
@@ -26,7 +24,7 @@ public class Tools {
* @throws IllegalPageParamException 如果分页请求的参数不对
*/
public List getListPage(int pageNum, int pageSize, List allVegetableList)
- throws RequestPageTooLargeException, IllegalPageParamException {
+ throws RequestPageTooLargeException, IllegalPageParamException, SourceDataWornException {
// 请求的分页大小或页码非法
if (pageNum <= 0 || pageSize <= 0) {
@@ -37,12 +35,15 @@ public class Tools {
int resultSize = allVegetableList.size();
// 判断上一页的记录是不是已经超过最后一条记录,防止页码超过最大值
- if ((pageNum - 1)*pageSize >= resultSize) {
+ if ((pageNum - 1) * pageSize >= resultSize) {
+ if (allVegetableList.isEmpty()) {
+ throw new SourceDataWornException("源数据为空");
+ }
throw new RequestPageTooLargeException("请求页码:" + pageNum);
}
LinkedList pagedVegetableList = new LinkedList<>();
- for (int i = (pageNum - 1)*pageSize; (i < pageNum*pageSize && i < resultSize); i++) {
+ for (int i = (pageNum - 1) * pageSize; (i < pageNum * pageSize && i < resultSize); i++) {
pagedVegetableList.add(allVegetableList.get(i));
}
@@ -60,9 +61,16 @@ public class Tools {
public LinkedList getAllVegetables(LinkedList vegetableStrings) {
LinkedList vegetables = new LinkedList<>();
for (String vegetableString : vegetableStrings) {
- vegetables.add(parseVegetableInfo(vegetableString));
+ try {
+ vegetables.add(parseVegetableInfo(vegetableString));
+ } catch (SourceDataWornException e) {
+ e.printStackTrace();
+ // 能成功读到多少就返回多少
+ return vegetables;
+ }
}
+
return vegetables;
}
@@ -74,15 +82,23 @@ public class Tools {
* 其中信息日期格式应为 "yyyy-mm-dd hh:mm:ss"
* @return 蔬菜信息数据类
*/
- private Vegetable parseVegetableInfo(String vegetableString) {
- String[] spilt = vegetableString.split(" ");
+ private Vegetable parseVegetableInfo(String vegetableString) throws SourceDataWornException {
Vegetable vegetable = new Vegetable();
+ try {
+ String[] spilt = vegetableString.split(" ");
- vegetable.setName(spilt[0]);
- vegetable.setPrice(Double.parseDouble(spilt[1]), Vegetable.PRICE_MIN);
- vegetable.setPrice(Double.parseDouble(spilt[2]), Vegetable.PRICE_AVG);
- vegetable.setPrice(Double.parseDouble(spilt[3]), Vegetable.PRICE_MAX);
- vegetable.setPriceDate(spilt[4] + ' ' + spilt[5]);
+ vegetable.setName(spilt[0]);
+ vegetable.setPrice(Double.parseDouble(spilt[1]), Vegetable.PRICE_MIN);
+ vegetable.setPrice(Double.parseDouble(spilt[2]), Vegetable.PRICE_AVG);
+ vegetable.setPrice(Double.parseDouble(spilt[3]), Vegetable.PRICE_MAX);
+ vegetable.setPriceDate(spilt[4] + ' ' + spilt[5]);
+
+ } catch (Exception e) {
+ throw new SourceDataWornException("解析源数据时发生错误:" + e.getMessage()
+ + "\n请检查源数据格式是否符合“蔬菜名 最低价(double) 平均价(double) 最高价(double) 信息日期”"
+ + "其中信息日期格式应为 \"yyyy-mm-dd hh:mm:ss\""
+ + "当前输入为:" + vegetableString);
+ }
return vegetable;
}
diff --git a/vegetables/src/main/java/me/lensfrex/vegetables/resopnse/ResultStatus.java b/vegetables/src/main/java/me/lensfrex/vegetables/resopnse/ResultStatus.java
new file mode 100644
index 0000000..dfaa77d
--- /dev/null
+++ b/vegetables/src/main/java/me/lensfrex/vegetables/resopnse/ResultStatus.java
@@ -0,0 +1,32 @@
+package me.lensfrex.vegetables.resopnse;
+
+public enum ResultStatus {
+ SUCCESS(200, "成功!"),
+
+ EMPTY_DATA(0, "数据是空的"),
+ PAGE_OUT_OF_RANGE(100, "请求的页码超过最大值"),
+ PAGE_PARAM_WORN(101, "分页参数有误"),
+ SOURCE_DATA_WORN(404, "源数据有错误,分页读取失败"),
+ UNKNOWN_ERROR(503, "未知错误");
+
+ /**
+ * 返回码。其值为ResultCode中定义的常量
+ */
+ private final int code;
+
+ /**
+ * 返回的结果信息
+ */
+ private final String message;
+
+ ResultStatus(int code, String message) {
+ this.code = code;
+ this.message = message;
+ }
+
+ @Override
+ public String toString() {
+ return "code: " + code + ", " +
+ "msg: '" + message + '\'';
+ }
+}
diff --git a/vegetables/src/main/java/me/lensfrex/vegetables/resopnse/UnityResponse.java b/vegetables/src/main/java/me/lensfrex/vegetables/resopnse/UnityResponse.java
new file mode 100644
index 0000000..0c3a097
--- /dev/null
+++ b/vegetables/src/main/java/me/lensfrex/vegetables/resopnse/UnityResponse.java
@@ -0,0 +1,41 @@
+package me.lensfrex.vegetables.resopnse;
+
+import lombok.Data;
+
+/**
+ * 统一的返回数据结构,含有字段code, message, data
+ *
+ * @param data的类型
+ */
+@Data
+public class UnityResponse {
+
+ /**
+ * 请求处理的状态信息
+ */
+ private final ResultStatus resultStatus;
+
+ /**
+ * 请求的数据,其类型为声明返回类时指定的类型
+ */
+ private T data;
+
+ public UnityResponse(ResultStatus resultCodes, T data) {
+ this.resultStatus = resultCodes;
+ this.data = data;
+ }
+
+ public UnityResponse() {
+ resultStatus = ResultStatus.EMPTY_DATA;
+ this.data = null;
+ }
+
+ // 手动拼成类似json的格式...
+ // 当然真正生成json还是看gson之类专门的库吧...
+ @Override
+ public String toString() {
+ return "{" + resultStatus +
+ ", data: " + data +
+ '}';
+ }
+}
diff --git a/vegetables/src/me/lensfrex/vegetables/utils/IOUtils.java b/vegetables/src/main/java/me/lensfrex/vegetables/utils/IOUtils.java
similarity index 100%
rename from vegetables/src/me/lensfrex/vegetables/utils/IOUtils.java
rename to vegetables/src/main/java/me/lensfrex/vegetables/utils/IOUtils.java
diff --git a/vegetables/src/me/lensfrex/vegetables/resopnse/ResponseTool.java b/vegetables/src/me/lensfrex/vegetables/resopnse/ResponseTool.java
deleted file mode 100644
index 3c4d001..0000000
--- a/vegetables/src/me/lensfrex/vegetables/resopnse/ResponseTool.java
+++ /dev/null
@@ -1,24 +0,0 @@
-package me.lensfrex.vegetables.resopnse;
-
-public class ResponseTool {
- /**
- * 构建请求成功的响应返回类
- * @param data 请求到的数据
- * @param message 请求信息
- * @param 请求的数据类型
- * @return 构建出的请求响应类
- */
- public static UnityResponse success(T data, String message) {
- return new UnityResponse<>(UnityResponse.ResultCode.SUCCESS, message, data);
- }
-
- /**
- * 构建请求失败的响应返回类
- * @param message 失败信息
- * @param code 失败状态码,应从UnityResponse.ResultCode中定义的常量中选择
- * @return 构建出的请求响应类
- */
- public static UnityResponse error(String message, int code) {
- return new UnityResponse<>(code, message, null);
- }
-}
diff --git a/vegetables/src/me/lensfrex/vegetables/resopnse/UnityResponse.java b/vegetables/src/me/lensfrex/vegetables/resopnse/UnityResponse.java
deleted file mode 100644
index d0ec918..0000000
--- a/vegetables/src/me/lensfrex/vegetables/resopnse/UnityResponse.java
+++ /dev/null
@@ -1,94 +0,0 @@
-package me.lensfrex.vegetables.resopnse;
-
-/**
- * 统一的返回数据结构,含有字段code, message, data
- *
- * @param data的类型
- */
-public class UnityResponse {
- /**
- * 返回码。其值为ResultCode中定义的常量
- */
- private int code;
- /**
- * 返回的结果信息
- */
- private String message;
- /**
- * 请求的数据,其类型为声明返回类时指定的类型
- */
- private T data;
-
- public UnityResponse(int code, String message, T data) {
- this.code = code;
- this.message = message;
- this.data = data;
- }
-
- public UnityResponse() {
- this.code = ResultCode.EMPTY_DATA;
- this.message = "数据未初始化";
- this.data = null;
- }
-
- public int getCode() {
- return code;
- }
-
- public void setCode(int code) {
- this.code = code;
- }
-
- public String getMessage() {
- return message;
- }
-
- public void setMessage(String message) {
- this.message = message;
- }
-
- public T getData() {
- return data;
- }
-
- public void setData(T data) {
- this.data = data;
- }
-
- // 手动拼成json格式...
- // 当然真正生成json还是看gson之类的吧...
- @Override
- public String toString() {
- return "{" +
- "code=" + code +
- ", message='" + message + '\'' +
- ", data=" + data +
- '}';
- }
-
- /**
- * 状态码的常量
- */
- public static class ResultCode {
- /**
- * 请求成功
- */
- public static final int SUCCESS = 200;
- /**
- * 数据是空的
- */
- public static final int EMPTY_DATA = 0;
- /**
- * 请求的分页页码超过最大值
- */
- public static final int PAGE_OUT_OF_MAX = 100;
- /**
- * 请求的分页参数有误
- */
- public static final int PAGE_PARAM_WORN = 101;
- /**
- * 未知错误
- */
- public static final int UNKNOWN_ERROR = 503;
- }
-}
diff --git a/vegetables/vegetables.iml b/vegetables/vegetables.iml
index 9465dd8..e5aeff5 100644
--- a/vegetables/vegetables.iml
+++ b/vegetables/vegetables.iml
@@ -1,11 +1,16 @@
-
-
-
+
+
+
+
-
+
+
+
+
+
\ No newline at end of file