public interface Responsor { public Object response(); }
定义实现
final public class PriceResponsor implements Responsor{ private int ticketsCount; public PriceResponsor(int ticketsCount) { this.ticketsCount = ticketsCount; } /** * business implement * @return the unit price according as tickets total number */ public Integer response() { if (ticketsCount < 0) throw new IllegalArgumentException("negative is invalid");
if (ticketsCount < 11) return 20; if (ticketsCount >= 11 && ticketsCount < 21) return 15; if (ticketsCount >= 21 && ticketsCount < 31) return 10; if (ticketsCount >= 31) return 8; return null; }
public Integer getTotalPrice() { Integer I = response(); if (I == null) return null; return ticketsCount*I; } }
演示代码:(从命令行输入一个整数表示票的张数)
final public class TicketPriceDemo { private static final String USAGE = "TicketPriceDemo [a integer represents the ticketsCount]"; private static final String PARAMETER_TYPE="Parameter should be a integer"; public static void main(String[] args) { if (args.length != 1) { System.out.println(USAGE); System.exit(1); }
int c = 0; try { c = Integer.parseInt(args[0]); } catch (NumberFormatException e) { System.out.println(PARAMETER_TYPE+"\r\n"+USAGE); System.exit(1); }