visibility of variables of classes are private

This commit is contained in:
schwarz
2021-04-30 10:12:52 +02:00
parent 3ea5ab3182
commit e9458859bf
4 changed files with 42 additions and 18 deletions

View File

@@ -2,11 +2,19 @@ import java.util.List;
public class Invoice {
public String customer;
public List<Performance> performances;
private String customer;
private List<Performance> performances;
public Invoice(String customer, List<Performance> performances) {
this.customer = customer;
this.performances = performances;
}
public String getCustomer() {
return customer;
}
public List<Performance> getPerformances() {
return performances;
}
}

View File

@@ -1,10 +1,18 @@
public class Performance {
public String playID;
public int audience;
private String playID;
private int audience;
public Performance(String playID, int audience) {
this.playID = playID;
this.audience = audience;
}
public String getPlayID() {
return playID;
}
public int getAudience() {
return audience;
}
}

View File

@@ -1,10 +1,18 @@
public class Play {
public String name;
public String type;
private String name;
private String type;
public Play(String name, String type) {
this.name = name;
this.type = type;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
}

View File

@@ -7,39 +7,39 @@ 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);
var result = String.format("Statement for %s\n", invoice.getCustomer());
NumberFormat frmt = NumberFormat.getCurrencyInstance(Locale.US);
for (var perf : invoice.performances) {
var play = plays.get(perf.playID);
for (var perf : invoice.getPerformances()) {
var play = plays.get(perf.getPlayID());
var thisAmount = 0;
switch (play.type) {
switch (play.getType()) {
case "tragedy":
thisAmount = 40000;
if (perf.audience > 30) {
thisAmount += 1000 * (perf.audience - 30);
if (perf.getAudience() > 30) {
thisAmount += 1000 * (perf.getAudience() - 30);
}
break;
case "comedy":
thisAmount = 30000;
if (perf.audience > 20) {
thisAmount += 10000 + 500 * (perf.audience - 20);
if (perf.getAudience() > 20) {
thisAmount += 10000 + 500 * (perf.getAudience() - 20);
}
thisAmount += 300 * perf.audience;
thisAmount += 300 * perf.getAudience();
break;
default:
throw new Error("unknown type: ${play.type}");
}
// add volume credits
volumeCredits += Math.max(perf.audience - 30, 0);
volumeCredits += Math.max(perf.getAudience() - 30, 0);
// add extra credit for every ten comedy attendees
if ("comedy".equals(play.type)) volumeCredits += Math.floor(perf.audience / 5);
if ("comedy".equals(play.getType())) volumeCredits += Math.floor(perf.getAudience() / 5);
// print line for this order
result += String.format(" %s: %s (%s seats)\n", play.name, frmt.format(thisAmount / 100), perf.audience);
result += String.format(" %s: %s (%s seats)\n", play.getName(), frmt.format(thisAmount / 100), perf.getAudience());
totalAmount += thisAmount;
}
result += String.format("Amount owed is %s\n", frmt.format(totalAmount / 100));