主要修改: - 完成蔬菜数据类(Vegetable.java) - 完成统一响应类(UnityResponse.java, ResponseTool.java,名字可能会改) - 完成底层的数据处理类(Tools.java,名字也可能会改,ResponseProcessor.java) - 新增部分异常类(me.lensfrex.vegetables.exceptions) - 小改IOUtil的注释 当然还不能跑 ~~(就算跑起来了也是bug一大坨吧(超小声))~~main
parent
abf361a8df
commit
9b22140351
@ -0,0 +1,12 @@ |
||||
<component name="ProjectRunConfigurationManager"> |
||||
<configuration default="false" name="vegetables" type="Application" factoryName="Application"> |
||||
<option name="ALTERNATIVE_JRE_PATH" value="1.8" /> |
||||
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" /> |
||||
<option name="MAIN_CLASS_NAME" value="me.lensfrex.vegetables.VegetablesMain" /> |
||||
<module name="vegetables" /> |
||||
<method v="2"> |
||||
<option name="Make" enabled="true" /> |
||||
<option name="com.soywiz.korge.intellij.UpdateResourceBeforeRunTask" enabled="false" /> |
||||
</method> |
||||
</configuration> |
||||
</component> |
@ -0,0 +1,22 @@ |
||||
package me.lensfrex.vegetables; |
||||
|
||||
import me.lensfrex.vegetables.data.Vegetable; |
||||
import me.lensfrex.vegetables.resopnse.UnityResponse; |
||||
|
||||
import java.util.ArrayList; |
||||
|
||||
/** |
||||
* 请求处理类,上层处理请求,主要为对返回数据的封装。 |
||||
* 由于功能简单,仅有处理分页数据 |
||||
*/ |
||||
public class ResponseProcessor { |
||||
public UnityResponse<ArrayList<Vegetable>> getListPage(int pageNum, int pageSize) { |
||||
try { |
||||
|
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
|
||||
return null; |
||||
} |
||||
} |
@ -1,15 +1,95 @@ |
||||
package me.lensfrex.vegetables; |
||||
|
||||
import me.lensfrex.vegetables.data.Vegetables; |
||||
import me.lensfrex.vegetables.utils.IOUtils; |
||||
import me.lensfrex.vegetables.data.Vegetable; |
||||
import me.lensfrex.vegetables.exceptions.IllegalPageParamException; |
||||
import me.lensfrex.vegetables.exceptions.RequestPageTooLargeException; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 请求处理工具 |
||||
* 由于功能较为简单,目前仅有获取分页数据的功能 |
||||
*/ |
||||
public class Tools { |
||||
public static final String SOURCE_FILE_PATH = "/vegetables.txt"; |
||||
// 测试用路径,使用前记得改 :P
|
||||
public static final String SOURCE_FILE_PATH = "C:\\Users\\lenfrex\\projects\\AmberGround\\vegetables\\resources\\vegetables.txt"; |
||||
|
||||
// public static List<Vegetables> getListPage(int pageNum, int pageSize) {
|
||||
// ArrayList<String> source = IOUtils.getListFromFile();
|
||||
// }
|
||||
/** |
||||
* 根据给定的页索引以及页大小,对全部蔬菜数据进行分页并返回分页后的List。 |
||||
* |
||||
* @param pageNum 页码(从1开始) |
||||
* @param pageSize 页大小 |
||||
* @param allVegetableList 全体的待分页的蔬菜数据 |
||||
* @return LinkedList<Vegetable> 分页后的蔬菜数据。 |
||||
* @throws RequestPageTooLargeException 如果请求的页码已经超过了最大页数 |
||||
* @throws IllegalPageParamException 如果分页请求的参数不对 |
||||
*/ |
||||
public List<Vegetable> getListPage(int pageNum, int pageSize, List<Vegetable> allVegetableList) |
||||
throws RequestPageTooLargeException, IllegalPageParamException { |
||||
|
||||
// 请求的分页大小或页码非法
|
||||
if (pageNum <= 0 || pageSize <= 0) { |
||||
throw new IllegalPageParamException( |
||||
String.format("分页请求的参数不对:pageNum: %d, pageSize: %d", pageNum, pageSize)); |
||||
} |
||||
|
||||
int resultSize = allVegetableList.size(); |
||||
|
||||
// 判断上一页的记录是不是已经超过最后一条记录,防止页码超过最大值
|
||||
if ((pageNum - 1)*pageSize >= resultSize) { |
||||
throw new RequestPageTooLargeException(); |
||||
} |
||||
|
||||
LinkedList<Vegetable> pagedVegetableList = new LinkedList<>(); |
||||
for (int i = (pageNum - 1)*pageSize; (i < pageNum*pageSize && i < resultSize); i++) { |
||||
pagedVegetableList.add(allVegetableList.get(i)); |
||||
} |
||||
|
||||
return pagedVegetableList; |
||||
} |
||||
|
||||
/** |
||||
* 根据数据字符串解析并获取蔬菜数据 |
||||
* |
||||
* @param vegetableStrings 蔬菜数据源字符串列表,字符串信息格式应为 |
||||
* "蔬菜名 最低价 平均价 最高价 信息日期" |
||||
* 其中信息日期格式应为 "yyyy-mm-dd hh:mm:ss" |
||||
* @return LinkedList<Vegetable> 解析后的蔬菜信息列表 |
||||
*/ |
||||
public LinkedList<Vegetable> getAllVegetables(LinkedList<String> vegetableStrings) { |
||||
LinkedList<Vegetable> vegetables = new LinkedList<>(); |
||||
for (String vegetableString : vegetableStrings) { |
||||
vegetables.add(parseVegetableInfo(vegetableString)); |
||||
} |
||||
|
||||
return vegetables; |
||||
} |
||||
|
||||
private static void printMessage() { |
||||
System.out.println("Get data source from: " + SOURCE_FILE_PATH); |
||||
System.out.println("This is just for test."); |
||||
System.out.println("If some IO error occurred, try to change SOURCE_FILE_PATH in me.lensfrex.vegetables.Tools :P"); |
||||
} |
||||
|
||||
/** |
||||
* 解析Vegetable的信息字符串,并返回相应的蔬菜信息数据 |
||||
* |
||||
* @param vegetableString 蔬菜信息字符串, |
||||
* 格式应为 "蔬菜名 最低价 平均价 最高价 信息日期" |
||||
* 其中信息日期格式应为 "yyyy-mm-dd hh:mm:ss" |
||||
* @return 蔬菜信息数据类 |
||||
*/ |
||||
private Vegetable parseVegetableInfo(String vegetableString) { |
||||
String[] spilt = vegetableString.split(" "); |
||||
Vegetable vegetable = new Vegetable(); |
||||
|
||||
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]); |
||||
|
||||
return vegetable; |
||||
} |
||||
} |
||||
|
@ -1,9 +1,7 @@ |
||||
package me.lensfrex.vegetables; |
||||
|
||||
public class VegetablesMain { |
||||
public static final String SOURCE_FILE_PATH = "resources/vegetables.txt"; |
||||
|
||||
public static void main(String[] args) { |
||||
|
||||
} |
||||
} |
||||
|
@ -0,0 +1,50 @@ |
||||
package me.lensfrex.vegetables.data; |
||||
|
||||
import java.util.Arrays; |
||||
|
||||
/** |
||||
* Vegetable的信息数据类,仅用于存储相关的数据结构 |
||||
*/ |
||||
public class Vegetable { |
||||
public static final int PRICE_MIN = 0; |
||||
public static final int PRICE_AVG = 1; |
||||
public static final int PRICE_MAX = 2; |
||||
|
||||
private String name; |
||||
private double[] price; |
||||
|
||||
private String priceDate; |
||||
|
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
public void setName(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
public double getPrice(int priceType) { |
||||
return price[priceType]; |
||||
} |
||||
|
||||
public void setPrice(double price, int priceType) { |
||||
this.price[priceType] = price; |
||||
} |
||||
|
||||
public String getPriceDate() { |
||||
return priceDate; |
||||
} |
||||
|
||||
public void setPriceDate(String priceDate) { |
||||
this.priceDate = priceDate; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "Vegetable{" + |
||||
"name='" + name + '\'' + |
||||
", price=" + Arrays.toString(price) + |
||||
", priceDate='" + priceDate + '\'' + |
||||
'}'; |
||||
} |
||||
} |
@ -1,34 +0,0 @@ |
||||
package me.lensfrex.vegetables.data; |
||||
|
||||
import java.util.Date; |
||||
|
||||
public class Vegetables { |
||||
private String name; |
||||
private double[] price; |
||||
|
||||
private String date; |
||||
|
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
public void setName(String name) { |
||||
this.name = name; |
||||
} |
||||
|
||||
public double getPrice(int priceType) { |
||||
return price[priceType]; |
||||
} |
||||
|
||||
public void setPrice(double price, int priceType) { |
||||
this.price[priceType] = price; |
||||
} |
||||
|
||||
public String getDate() { |
||||
return date; |
||||
} |
||||
|
||||
public void setDate(String date) { |
||||
this.date = date; |
||||
} |
||||
} |
@ -0,0 +1,10 @@ |
||||
package me.lensfrex.vegetables.exceptions; |
||||
|
||||
/** |
||||
* 数据页请求数据非法异常 |
||||
*/ |
||||
public class IllegalPageParamException extends Exception{ |
||||
public IllegalPageParamException(String message) { |
||||
super("IllegalPageRequest: " + message); |
||||
} |
||||
} |
@ -0,0 +1,10 @@ |
||||
package me.lensfrex.vegetables.exceptions; |
||||
|
||||
/** |
||||
* 用于当请求数据的页数已经超过最大页数时抛出的异常 |
||||
*/ |
||||
public class RequestPageTooLargeException extends Exception { |
||||
public RequestPageTooLargeException() { |
||||
super("请求结果的页数已超过最大数量"); |
||||
} |
||||
} |
@ -0,0 +1,24 @@ |
||||
package me.lensfrex.vegetables.resopnse; |
||||
|
||||
public class ResponseTool { |
||||
/** |
||||
* 构建请求成功的响应返回类 |
||||
* @param data 请求到的数据 |
||||
* @param message 请求信息 |
||||
* @param <T> 请求的数据类型 |
||||
* @return 构建出的请求响应类 |
||||
*/ |
||||
public static <T> UnityResponse<T> success(T data, String message) { |
||||
return new UnityResponse<>(UnityResponse.ResultCode.SUCCESS, message, data); |
||||
} |
||||
|
||||
/** |
||||
* 构建请求失败的响应返回类 |
||||
* @param message 失败信息 |
||||
* @param code 失败状态码,应从UnityResponse.ResultCode中定义的常量中选择 |
||||
* @return 构建出的请求响应类 |
||||
*/ |
||||
public static UnityResponse<String> error(String message, int code) { |
||||
return new UnityResponse<>(code, message, "null"); |
||||
} |
||||
} |
@ -0,0 +1,92 @@ |
||||
package me.lensfrex.vegetables.resopnse; |
||||
|
||||
/** |
||||
* 统一的返回数据结构,含有字段code, message, data |
||||
* |
||||
* @param <T> data的类型 |
||||
*/ |
||||
public class UnityResponse<T> { |
||||
/** |
||||
* 返回码。其值为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; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "UnityResponse{" + |
||||
"code=" + code + |
||||
", message='" + message + '\'' + |
||||
", data=" + data + |
||||
'}'; |
||||
} |
||||
|
||||
/** |
||||
* 状态码的常量 |
||||
*/ |
||||
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; |
||||
} |
||||
} |
Loading…
Reference in new issue