项目:批处理器
Coddy 面向 Kotlin 开发者的 Java 课程的第 46 课,共 48 课。
挑战
简单保留命令 1、2 和 3。添加命令 4:下一行是会员 yes/no,之后是零行或多行原始订单行。实现 Processor(Pricing).summary(List<String>):使用 OrderParser 解析每一行,统计有效行和无效行,并分别计算每个有效订单的策略总价后求和。分三行返回有效行数、无效行数和总价。也必须支持传入自定义 Pricing;不要在 Processor 中选择策略。总价在 int 范围内。
从 Exercise.solve(List<String> input) 返回不带末尾换行符的准确结果文本。可见的锁定文件 Main.java 会读取所有输入行,并在输出返回的文本时添加一个末尾换行符。不要在可编辑的方法中打印内容。除非另有说明,所有整数和算术结果都在 Java int 范围内。
可编辑文件:Order.java、Pricing.java、StandardPricing.java、MemberPricing.java、OrderParser.java、Processor.java、Exercise.java、Text.java。Text.java 提供可重复使用的解析和格式化辅助工具。所有文件都在未命名包中,并会一起编译。Main.java 是唯一入口点,不能编辑。
可见的锁定驱动程序还会在运行输入用例之前,使用独立的新对象调用所需的类 API。请实现这些 API;仅对最终输出进行硬编码是不够的。
亲手试试
import java.io.*;
import java.util.*;
public class Main {
private static void check(boolean condition) {
if (!condition) throw new AssertionError("Required public API behavior failed");
}
public static void main(String[] args) throws Exception {
List<String> input = new ArrayList<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = reader.readLine()) != null) input.add(line);
int stage=Integer.parseInt(input.get(0));
check(new Order("probe",7,3).subtotal()==21 && new Order("Ada",0,2).customer().equals("Ada"));
if(stage>=2){Pricing standard=new StandardPricing(),member=new MemberPricing();check(standard.total(13)==13&&member.total(13)==11&&member.total(0)==0);}
if(stage>=3){check(OrderParser.parse("Ada 7 3").subtotal()==21);boolean rejected=false;try{OrderParser.parse("Ada 7 0");}catch(IllegalArgumentException e){rejected=true;}check(rejected);}
if(stage>=4){Processor processor=new Processor(n->n+1);check(processor.summary(Arrays.asList("Ada 7 3","bad","Bo 0 1")).equals("2\n1\n23"));check(processor.summary(Collections.emptyList()).equals("0\n0\n0"));}
System.out.println(Exercise.solve(input));
}
}
面向 Kotlin 开发者的 Java中的所有课
自己练习: Java 在线编译器