算是写完了?

主要修改:
- 修改数据文件路径指定方式(Tools.java)
- 更改部分异常处理方式及消息(Tools.java, RequestPageTooLargeException.java)
- 为idea配置相关运行参数
main
lensfrex 3 years ago
parent d345ac03be
commit 3bf1449635
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 1
      .idea/runConfigurations/vegetables.xml
  2. 38
      vegetables/src/me/lensfrex/vegetables/ResponseProcessor.java
  3. 14
      vegetables/src/me/lensfrex/vegetables/Tools.java
  4. 61
      vegetables/src/me/lensfrex/vegetables/VegetablesMain.java
  5. 4
      vegetables/src/me/lensfrex/vegetables/data/Vegetable.java
  6. 4
      vegetables/src/me/lensfrex/vegetables/exceptions/RequestPageTooLargeException.java
  7. 4
      vegetables/src/me/lensfrex/vegetables/resopnse/ResponseTool.java
  8. 6
      vegetables/src/me/lensfrex/vegetables/resopnse/UnityResponse.java
  9. 28
      vegetables/src/me/lensfrex/vegetables/utils/IOUtils.java

@ -4,6 +4,7 @@
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" /> <option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
<option name="MAIN_CLASS_NAME" value="me.lensfrex.vegetables.VegetablesMain" /> <option name="MAIN_CLASS_NAME" value="me.lensfrex.vegetables.VegetablesMain" />
<module name="vegetables" /> <module name="vegetables" />
<option name="WORKING_DIRECTORY" value="./vegetables" />
<method v="2"> <method v="2">
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
<option name="com.soywiz.korge.intellij.UpdateResourceBeforeRunTask" enabled="false" /> <option name="com.soywiz.korge.intellij.UpdateResourceBeforeRunTask" enabled="false" />

@ -1,22 +1,56 @@
package me.lensfrex.vegetables; package me.lensfrex.vegetables;
import me.lensfrex.vegetables.data.Vegetable; 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.resopnse.UnityResponse;
import me.lensfrex.vegetables.utils.IOUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
/** /**
* 请求处理类上层处理请求主要为对返回数据的封装 * 请求处理类上层处理请求主要为对返回数据的封装
* 由于功能简单仅有处理分页数据 * 由于功能简单仅有处理分页数据
*/ */
public class ResponseProcessor { public class ResponseProcessor {
public final String sourceFilePath;
/**
* 初始化ResponseProcessor实例并指定源数据文件的路径
* @param sourceFilePath 数据源文件路径
*/
public ResponseProcessor(String sourceFilePath) {
this.sourceFilePath = sourceFilePath;
}
/**
* 从文件中读取并根据参数进行分页
* @param pageNum 页码从1开始
* @param pageSize 每页结果的数量
* @return UnityResponse<ArrayList<Vegetable>> 统一的响应类
*/
public UnityResponse<ArrayList<Vegetable>> getListPage(int pageNum, int pageSize) { public UnityResponse<ArrayList<Vegetable>> getListPage(int pageNum, int pageSize) {
Tools tools = new Tools();
try { try {
LinkedList<String> sourceData = IOUtils.getListFromFile(sourceFilePath);
LinkedList<Vegetable> allVegetables = tools.getAllVegetables(sourceData);
ArrayList<Vegetable> result = new ArrayList<>(tools.getListPage(pageNum, pageSize, allVegetables));
return ResponseTool.success(result, "成功");
} catch (RequestPageTooLargeException e) {
e.printStackTrace();
return ResponseTool.error("请求的页码超过最大值", UnityResponse.ResultCode.PAGE_OUT_OF_MAX);
} catch (IllegalPageParamException e) {
e.printStackTrace();
return ResponseTool.error("请求的分页参数有误", UnityResponse.ResultCode.PAGE_PARAM_WORN);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return ResponseTool.error("未知错误", UnityResponse.ResultCode.UNKNOWN_ERROR);
} }
return null;
} }
} }

@ -13,7 +13,7 @@ import java.util.List;
*/ */
public class Tools { public class Tools {
// 测试用路径,使用前记得改 :P // 测试用路径,使用前记得改 :P
public static final String SOURCE_FILE_PATH = "C:\\Users\\lenfrex\\projects\\AmberGround\\vegetables\\resources\\vegetables.txt"; // public static final String SOURCE_FILE_PATH = "C:\\Users\\lenfrex\\projects\\AmberGround\\vegetables\\resources\\vegetables.txt";
/** /**
* 根据给定的页索引以及页大小对全部蔬菜数据进行分页并返回分页后的List * 根据给定的页索引以及页大小对全部蔬菜数据进行分页并返回分页后的List
@ -38,7 +38,7 @@ public class Tools {
// 判断上一页的记录是不是已经超过最后一条记录,防止页码超过最大值 // 判断上一页的记录是不是已经超过最后一条记录,防止页码超过最大值
if ((pageNum - 1)*pageSize >= resultSize) { if ((pageNum - 1)*pageSize >= resultSize) {
throw new RequestPageTooLargeException(); throw new RequestPageTooLargeException("请求页码:" + pageNum);
} }
LinkedList<Vegetable> pagedVegetableList = new LinkedList<>(); LinkedList<Vegetable> pagedVegetableList = new LinkedList<>();
@ -52,7 +52,7 @@ public class Tools {
/** /**
* 根据数据字符串解析并获取蔬菜数据 * 根据数据字符串解析并获取蔬菜数据
* *
* @param vegetableStrings 蔬菜数据源字符串列表字符串信息格式应为 * @param vegetableStrings 蔬菜数据源字符串列表每条字符串信息格式应为
* "蔬菜名 最低价 平均价 最高价 信息日期" * "蔬菜名 最低价 平均价 最高价 信息日期"
* 其中信息日期格式应为 "yyyy-mm-dd hh:mm:ss" * 其中信息日期格式应为 "yyyy-mm-dd hh:mm:ss"
* @return LinkedList<Vegetable> 解析后的蔬菜信息列表 * @return LinkedList<Vegetable> 解析后的蔬菜信息列表
@ -66,12 +66,6 @@ public class Tools {
return vegetables; 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的信息字符串并返回相应的蔬菜信息数据 * 解析Vegetable的信息字符串并返回相应的蔬菜信息数据
* *
@ -88,7 +82,7 @@ public class Tools {
vegetable.setPrice(Double.parseDouble(spilt[1]), Vegetable.PRICE_MIN); vegetable.setPrice(Double.parseDouble(spilt[1]), Vegetable.PRICE_MIN);
vegetable.setPrice(Double.parseDouble(spilt[2]), Vegetable.PRICE_AVG); vegetable.setPrice(Double.parseDouble(spilt[2]), Vegetable.PRICE_AVG);
vegetable.setPrice(Double.parseDouble(spilt[3]), Vegetable.PRICE_MAX); vegetable.setPrice(Double.parseDouble(spilt[3]), Vegetable.PRICE_MAX);
vegetable.setPriceDate(spilt[4] + spilt[5]); vegetable.setPriceDate(spilt[4] + ' ' + spilt[5]);
return vegetable; return vegetable;
} }

@ -1,7 +1,68 @@
package me.lensfrex.vegetables; package me.lensfrex.vegetables;
import me.lensfrex.vegetables.data.Vegetable;
import me.lensfrex.vegetables.resopnse.UnityResponse;
import java.util.ArrayList;
public class VegetablesMain { public class VegetablesMain {
// 如果要使用其他目录的测试数据文件,请在命令行指定参数(第一个参数)
// 否则将会使用默认的目录("/{运行目录,一般为工程目录}/vegetables/resources/vegetables.txt")
// 此项目的idea runConfigure已经设置好运行目录,一般情况下可以直接读取到
public static void main(String[] args) { public static void main(String[] args) {
new VegetablesMain().run(args);
}
private void run(String[] args) {
UnityResponse<ArrayList<Vegetable>> response = null;
ResponseProcessor responseProcessor = null;
if (args.length != 0) {
System.out.println("Read source data from: " + args[0]);
responseProcessor = new ResponseProcessor(args[0]);
} else {
String sourceDataLocation = System.getProperty("user.dir") + "/resources/vegetables.txt";
System.out.println("Command line param not specify, will read source data from: " + sourceDataLocation);
responseProcessor = new ResponseProcessor(sourceDataLocation);
}
// 测试正常情况
response = responseProcessor.getListPage(1,10);
System.out.println("------------------------------------");
System.out.println("测试:正常情况(page 1, pageSize 10)");
System.out.println(response);
response = responseProcessor.getListPage(2,10);
System.out.println("------------------------------------");
System.out.println("测试:正常情况(page 2, pageSize 10)");
System.out.println(response);
response = responseProcessor.getListPage(1,15);
System.out.println("------------------------------------");
System.out.println("测试:正常情况(page 1, pageSize 15)");
System.out.println(response);
response = responseProcessor.getListPage(2,15);
System.out.println("------------------------------------");
System.out.println("测试:正常情况(page 2, pageSize 15)");
System.out.println(response);
// 测试页码超限
response = responseProcessor.getListPage(100000, 100);
System.out.println("------------------------------------");
System.out.println("测试:页码超限");
System.out.println(response);
// 测试分页参数有误
response = responseProcessor.getListPage(-1, 150);
System.out.println("------------------------------------");
System.out.println("测试:参数有误");
System.out.println(response);
// 测试炒饭...
System.out.println("------------------------------------");
System.out.println("测试:点炒饭");
System.out.println("炸了");
} }
} }

@ -11,7 +11,7 @@ public class Vegetable {
public static final int PRICE_MAX = 2; public static final int PRICE_MAX = 2;
private String name; private String name;
private double[] price; private double[] price = new double[3];
private String priceDate; private String priceDate;
@ -41,7 +41,7 @@ public class Vegetable {
@Override @Override
public String toString() { public String toString() {
return "Vegetable{" + return "{" +
"name='" + name + '\'' + "name='" + name + '\'' +
", price=" + Arrays.toString(price) + ", price=" + Arrays.toString(price) +
", priceDate='" + priceDate + '\'' + ", priceDate='" + priceDate + '\'' +

@ -4,7 +4,7 @@ package me.lensfrex.vegetables.exceptions;
* 用于当请求数据的页数已经超过最大页数时抛出的异常 * 用于当请求数据的页数已经超过最大页数时抛出的异常
*/ */
public class RequestPageTooLargeException extends Exception { public class RequestPageTooLargeException extends Exception {
public RequestPageTooLargeException() { public RequestPageTooLargeException(String additionalMessage) {
super("请求结果的页数已超过最大数量"); super("请求结果的页数已超过最大数量" + additionalMessage);
} }
} }

@ -18,7 +18,7 @@ public class ResponseTool {
* @param code 失败状态码应从UnityResponse.ResultCode中定义的常量中选择 * @param code 失败状态码应从UnityResponse.ResultCode中定义的常量中选择
* @return 构建出的请求响应类 * @return 构建出的请求响应类
*/ */
public static UnityResponse<String> error(String message, int code) { public static <T> UnityResponse<T> error(String message, int code) {
return new UnityResponse<>(code, message, "null"); return new UnityResponse<>(code, message, null);
} }
} }

@ -55,9 +55,11 @@ public class UnityResponse<T> {
this.data = data; this.data = data;
} }
// 手动拼成json格式...
// 当然真正生成json还是看gson之类的吧...
@Override @Override
public String toString() { public String toString() {
return "UnityResponse{" + return "{" +
"code=" + code + "code=" + code +
", message='" + message + '\'' + ", message='" + message + '\'' +
", data=" + data + ", data=" + data +
@ -67,7 +69,7 @@ public class UnityResponse<T> {
/** /**
* 状态码的常量 * 状态码的常量
*/ */
static class ResultCode { public static class ResultCode {
/** /**
* 请求成功 * 请求成功
*/ */

@ -1,6 +1,7 @@
package me.lensfrex.vegetables.utils; package me.lensfrex.vegetables.utils;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.util.LinkedList; import java.util.LinkedList;
@ -10,25 +11,24 @@ public class IOUtils {
/** /**
* 从文件中读取每行数据 * 从文件中读取每行数据
* 为保证性能此处使用LinkedList * 为保证性能此处使用LinkedList
*
* @param path 文件的存储路径 * @param path 文件的存储路径
* @return LinkedList<String> 读取到的每行文本 * @return LinkedList<String> 读取到的每行文本
*/ */
public static LinkedList<String> getListFromFile(String path) { public static LinkedList<String> getListFromFile(String path) throws IOException {
LinkedList<String> list = new LinkedList<>(); LinkedList<String> list = new LinkedList<>();
try{
//读取文件内容 //读取文件内容
//创建一个新的 FileReader ,给定要读取的文件的名称。 //创建一个新的 FileReader ,给定要读取的文件的名称。
//创建使用默认大小的输入缓冲区的缓冲字符输入流 //创建使用默认大小的输入缓冲区的缓冲字符输入流
BufferedReader br = new BufferedReader(new FileReader(path)); BufferedReader br = new BufferedReader(new FileReader(path));
String data = ""; String data;
while ( ( data = br.readLine() ) != null ) { // 一次读取一行 while ((data = br.readLine()) != null) { // 一次读取一行
list.add(data); list.add(data);
}
//关闭资源
br.close();
} catch(IOException e) {
e.printStackTrace();
} }
//关闭资源
br.close();
return list; return list;
} }
} }

Loading…
Cancel
Save