主要修改: - 修改数据文件路径指定方式(Tools.java) - 更改部分异常处理方式及消息(Tools.java, RequestPageTooLargeException.java) - 为idea配置相关运行参数main
parent
d345ac03be
commit
3bf1449635
@ -1,22 +1,56 @@ |
||||
package me.lensfrex.vegetables; |
||||
|
||||
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.utils.IOUtils; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.LinkedList; |
||||
|
||||
/** |
||||
* 请求处理类,上层处理请求,主要为对返回数据的封装。 |
||||
* 由于功能简单,仅有处理分页数据 |
||||
*/ |
||||
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) { |
||||
Tools tools = new Tools(); |
||||
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) { |
||||
e.printStackTrace(); |
||||
return ResponseTool.error("未知错误", UnityResponse.ResultCode.UNKNOWN_ERROR); |
||||
} |
||||
|
||||
return null; |
||||
} |
||||
} |
||||
|
@ -1,7 +1,68 @@ |
||||
package me.lensfrex.vegetables; |
||||
|
||||
import me.lensfrex.vegetables.data.Vegetable; |
||||
import me.lensfrex.vegetables.resopnse.UnityResponse; |
||||
|
||||
import java.util.ArrayList; |
||||
|
||||
public class VegetablesMain { |
||||
|
||||
// 如果要使用其他目录的测试数据文件,请在命令行指定参数(第一个参数)
|
||||
// 否则将会使用默认的目录("/{运行目录,一般为工程目录}/vegetables/resources/vegetables.txt")
|
||||
// 此项目的idea runConfigure已经设置好运行目录,一般情况下可以直接读取到
|
||||
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("炸了"); |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue