Added java version of the excersise

This commit is contained in:
Doménique Tilleuil
2019-08-08 10:20:39 +02:00
parent ca4afc497b
commit 45aed9314d
13 changed files with 437 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
import javax.swing.*;
import java.util.List;
public class Invoice {
public String customer;
public List<Performance> performances;
public Invoice(String customer, List<Performance> performances) {
this.customer = customer;
this.performances = performances;
}
}

View File

@@ -0,0 +1,10 @@
public class Performance {
public String playID;
public int audience;
public Performance(String playID, int audience) {
this.playID = playID;
this.audience = audience;
}
}

View File

@@ -0,0 +1,10 @@
public class Play {
public String name;
public String type;
public Play(String name, String type) {
this.name = name;
this.type = type;
}
}

View File

@@ -0,0 +1,50 @@
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Map;
public class StatementPrinter {
public String print(Invoice invoice, Map<String, Play> plays) {
var totalAmount = 0;
var volumeCredits = 0;
var result = String.format("Statement for %s\n", invoice.customer);
NumberFormat frmt = NumberFormat.getCurrencyInstance(Locale.US);
for (var perf : invoice.performances) {
var play = plays.get(perf.playID);
var thisAmount = 0;
switch (play.type) {
case "tragedy":
thisAmount = 40000;
if (perf.audience > 30) {
thisAmount += 1000 * (perf.audience - 30);
}
break;
case "comedy":
thisAmount = 30000;
if (perf.audience > 20) {
thisAmount += 10000 + 500 * (perf.audience - 20);
}
thisAmount += 300 * perf.audience;
break;
default:
throw new Error("unknown type: ${play.type}");
}
// add volume credits
volumeCredits += Math.max(perf.audience - 30, 0);
// add extra credit for every ten comedy attendees
if ("comedy" == play.type) volumeCredits += Math.floor(perf.audience / 5);
// print line for this order
result += String.format(" %s: %s (%s seats)\n", play.name, frmt.format(thisAmount / 100), perf.audience);
totalAmount += thisAmount;
}
result += String.format("Amount owed is %s\n", frmt.format(totalAmount / 100));
result += String.format("You earned %s credits\n", volumeCredits);
return result;
}
}

View File

@@ -0,0 +1,30 @@
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
public class StatementPrinterTests {
@Test
void printsStatements() {
Map<String, Play> plays = Map.of("hamlet", new Play("Hamlet", "tragedy"),
"as-like", new Play("As You Like It", "comedy"),
"othello", new Play("Othello", "tragedy"));
Invoice invoice = new Invoice("BigCo", List.of(new Performance("hamlet", 55),
new Performance("as-like", 35),
new Performance("othello", 40)));
StatementPrinter statementPrinter = new StatementPrinter();
var result = statementPrinter.print(invoice, plays);
assertThat(result).isEqualTo("Statement for BigCo\n" +
" Hamlet: $650.00 (55 seats)\n" +
" As You Like It: $580.00 (35 seats)\n" +
" Othello: $500.00 (40 seats)\n" +
"Amount owed is $1,730.00\n" +
"You earned 47 credits\n");
}
}