private String formatLine(String name, Integer amount, Integer audience) {
return String.format(" %s: %s (%s seats)%n", name, format(amount), audience);
}
private String formatStatement(Invoice invoice, Statement pc) {
return String.format("Statement for %s%n%sAmount owed is %s%nYou earned %s credits",
invoice.customer, pc.line, format(pc.amount), pc.credits);
}
private Statement makeStatement(Performance perf, Play play) {
int amount = PlayAmounts.forTypeAndAudience(play.type, perf.audience);
int volumeCredits = computeCredits(play.type, perf.audience);
String line = formatLine(play.name, amount, perf.audience);
return new Statement(line, amount, volumeCredits);
}
public String print(Invoice invoice, java.util.Map<String, Play> plays) {
return Vector.ofAll(invoice.performances)
// for each performance make a statement
.map(perf -> makeStatement(perf, plays.get(perf.playID)))
// compute total amount and credit
.reduceOption(Statement::add)
// make the final statement to be printed
.map(p -> formatStatement(invoice, p))
.getOrElse("");
}
StatementPrinter.java
public class StatementPrinter implements StatementPrinter {
private String formatLine(String name, Integer amount, Integer audience) {
return String.format(" %s: %s (%s seats)%n", name, format(amount), audience);
}
private String formatStatement(Invoice invoice, Statement pc) {
return String.format("Statement for %s%n%sAmount owed is %s%nYou earned %s credits",
invoice.customer, pc.line, format(pc.amount), pc.credits);
}
private Statement makeStatement(Performance perf, Play play) {
int amount = PlayAmounts.forTypeAndAudience(play.type, perf.audience);
int volumeCredits = computeCredits(play.type, perf.audience);
String line = formatLine(play.name, amount, perf.audience);
return new Statement(line, amount, volumeCredits);
}
private int computeCredits(String type, Integer audience) {
return Math.max(audience - 30, 0)
+ ("comedy".equals(type) ? (audience / 5) : 0);
}
public String print(Invoice invoice, java.util.Map<String, Play> plays) {
return Vector.ofAll(invoice.performances)
// for each performance make a statement
.map(perf -> makeStatement(perf, plays.get(perf.playID)))
// compute total amount and credit
.reduceOption(Statement::add)
// make the final statement to be printed
.map(p -> formatStatement(invoice, p))
.getOrElse("");
}
}