Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
zhuyi@zhuyideMacBook-Air-5 refactoring1 % git log --oneline
aa3e2af (HEAD -> test, origin/test) finish task 1.1 1.2 2.1
6857948 ver 1 m4
2a862c5 ver 1 m4
2b4f164 (origin/main, origin/HEAD, main) fixed tests for the not-for-credit tasks; parameter ordering and ensuring consistent line endings.
ff5004e adding shortcut keys for Inline Variable (Re: Piazza question)
b545fe2 clarifying extracted method names in README.md
a0124c9 fixing starter file
c8d9d38 adding starter files
cbe4517 first commit

6bd0e97 (HEAD -> test) finish task 2.1
aa3e2af (origin/test) finish task 1.1 1.2 2.1
6857948 ver 1 m4
2a862c5 ver 1 m4
2b4f164 (origin/main, origin/HEAD, main) fixed tests for the not-for-credit tasks; parameter ordering and ensuring consistent line endings.
ff5004e adding shortcut keys for Inline Variable (Re: Piazza question)
b545fe2 clarifying extracted method names in README.md
a0124c9 fixing starter file
c8d9d38 adding starter files
cbe4517 first commit

zhuyi@zhuyideMacBook-Air-5 refactoring1 % git log --oneline
05c4223 (HEAD -> test) finish task 2.2
6bd0e97 finish task 2.1
aa3e2af (origin/test) finish task 1.1 1.2 2.1
6857948 ver 1 m4
2a862c5 ver 1 m4
2b4f164 (origin/main, origin/HEAD, main) fixed tests for the not-for-credit tasks; parameter ordering and ensuring consistent line endings.
ff5004e adding shortcut keys for Inline Variable (Re: Piazza question)
b545fe2 clarifying extracted method names in README.md
a0124c9 fixing starter file
c8d9d38 adding starter files
cbe4517 first commit

a197717 (HEAD -> test, origin/test) finish task 1.2
594f23d finish task 2.2
2450799 finish task 2.2
05c4223 finish task 2.2
6bd0e97 finish task 2.1
aa3e2af finish task 1.1 1.2 2.1
6857948 ver 1 m4
2a862c5 ver 1 m4
2b4f164 (origin/main, origin/HEAD, main) fixed tests for the not-for-credit tasks; parameter ordering and ensuring consistent line endings.
ff5004e adding shortcut keys for Inline Variable (Re: Piazza question)
b545fe2 clarifying extracted method names in README.md
a0124c9 fixing starter file
c8d9d38 adding starter files
cbe4517 first commit

392f98f (HEAD -> test, origin/test) finish task 2.3
5d8410f finish task 1.2
a197717 finish task 1.2
594f23d finish task 2.2
2450799 finish task 2.2
05c4223 finish task 2.2
6bd0e97 finish task 2.1
aa3e2af finish task 1.1 1.2 2.1
6857948 ver 1 m4
2a862c5 ver 1 m4
2b4f164 (origin/main, origin/HEAD, main) fixed tests for the not-for-credit tasks; parameter ordering and ensuring consistent line endings.
ff5004e adding shortcut keys for Inline Variable (Re: Piazza question)
b545fe2 clarifying extracted method names in README.md
a0124c9 fixing starter file
c8d9d38 adding starter files
cbe4517 first commit

57f3c40 (HEAD -> test, origin/test) finish task 2.4
25ea068 finish task 2.3
392f98f finish task 2.3
5d8410f finish task 1.2
a197717 finish task 1.2
594f23d finish task 2.2
2450799 finish task 2.2
05c4223 finish task 2.2
6bd0e97 finish task 2.1
aa3e2af finish task 1.1 1.2 2.1
6857948 ver 1 m4
2a862c5 ver 1 m4
2b4f164 (origin/main, origin/HEAD, main) fixed tests for the not-for-credit tasks; parameter ordering and ensuring consistent line endings.
ff5004e adding shortcut keys for Inline Variable (Re: Piazza question)
b545fe2 clarifying extracted method names in README.md
a0124c9 fixing starter file
c8d9d38 adding starter files
6 changes: 6 additions & 0 deletions refactoring.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="CheckStyle-IDEA-Module" serialisationVersion="2">
<option name="activeLocationsIds" />
</component>
</module>
2 changes: 1 addition & 1 deletion src/main/java/theater/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public final class Constants {
public static final int COMEDY_EXTRA_VOLUME_FACTOR = 5;
// comedy amount constants
public static final int COMEDY_AMOUNT_PER_AUDIENCE = 300;
public final static int COMEDY_AUDIENCE_THRESHOLD = 20;
public static final int COMEDY_AUDIENCE_THRESHOLD = 20;
public static final int COMEDY_BASE_AMOUNT = 30000;
public static final int COMEDY_OVER_BASE_CAPACITY_AMOUNT = 10000;
public static final int COMEDY_OVER_BASE_CAPACITY_PER_PERSON = 500;
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/theater/Performance.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@
*/
public class Performance {

public String playID;
public int audience;
private final String playID;
private final int audience;

public Performance(String playID, int audience) {
this.playID = playID;
this.audience = audience;
}

public String getPlayID() {
return playID;
}

public int getAudience() {
return audience;
}
}

18 changes: 16 additions & 2 deletions src/main/java/theater/Play.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package theater;

/**
* Class representing a play.
*/

public class Play {

public String name;
public String type;
private final String name;
private final String type;

public Play(String name, String type) {
this.name = name;
this.type = type;
}

public String getName() {
return name;
}

public String getType() {
return type;
}
}

// 1.2 done
141 changes: 99 additions & 42 deletions src/main/java/theater/StatementPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* This class generates a statement for a given invoice of performances.
*/
public class StatementPrinter {
public Invoice invoice;
public Map<String, Play> plays;
private final Invoice invoice;
private final Map<String, Play> plays;

public StatementPrinter(Invoice invoice, Map<String, Play> plays) {
this.invoice = invoice;
Expand All @@ -22,47 +22,104 @@ public StatementPrinter(Invoice invoice, Map<String, Play> plays) {
* @throws RuntimeException if one of the play types is not known
*/
public String statement() {
int totalAmount = 0;
int volumeCredits = 0;
StringBuilder result = new StringBuilder("Statement for " + invoice.getCustomer() + System.lineSeparator());

NumberFormat frmt = NumberFormat.getCurrencyInstance(Locale.US);

for (Performance p : invoice.getPerformances()) {
Play play = plays.get(p.playID);

int thisAmount = 0;
switch (play.type) {
case "tragedy":
thisAmount = 40000;
if (p.audience > Constants.TRAGEDY_AUDIENCE_THRESHOLD) {
thisAmount += 1000 * (p.audience - 30);
}
break;
case "comedy":
thisAmount = Constants.COMEDY_BASE_AMOUNT;
if (p.audience > Constants.COMEDY_AUDIENCE_THRESHOLD) {
thisAmount += Constants.COMEDY_OVER_BASE_CAPACITY_AMOUNT
+ (Constants.COMEDY_OVER_BASE_CAPACITY_PER_PERSON
* (p.audience - Constants.COMEDY_AUDIENCE_THRESHOLD));
}
thisAmount += Constants.COMEDY_AMOUNT_PER_AUDIENCE * p.audience;
break;
default:
throw new RuntimeException(String.format("unknown type: %s", play.type));
}

// add volume credits
volumeCredits += Math.max(p.audience - Constants.BASE_VOLUME_CREDIT_THRESHOLD, 0);
// add extra credit for every five comedy attendees
if ("comedy".equals(play.type)) volumeCredits += p.audience / Constants.COMEDY_EXTRA_VOLUME_FACTOR;

// print line for this order
result.append(String.format(" %s: %s (%s seats)%n", play.name, frmt.format(thisAmount / 100), p.audience));
totalAmount += thisAmount;
final StringBuilder result = new StringBuilder(
"Statement for " + invoice.getCustomer()
+ System.lineSeparator());

// build per-performance lines
for (final Performance performance : invoice.getPerformances()) {
result.append(String.format(" %s: %s (%s seats)%n",
getPlay(performance).getName(),
usd(getAmount(performance)),
performance.getAudience()));
}
result.append(String.format("Amount owed is %s%n", frmt.format(totalAmount / 100)));
result.append(String.format("You earned %s credits%n", volumeCredits));

// use query methods instead of local temps
result.append(String.format("Amount owed is %s%n",
usd(getTotalAmount())));
result.append(String.format("You earned %s credits%n",
getTotalVolumeCredits()));

return result.toString();
}

private Play getPlay(Performance performance) {
return plays.get(performance.getPlayID());
}

private int getAmount(Performance performance) {
int amount = 0;
final Play play = getPlay(performance);

switch (play.getType()) {
case "tragedy":
amount = Constants.TRAGEDY_BASE_AMOUNT;
if (performance.getAudience()
> Constants.TRAGEDY_AUDIENCE_THRESHOLD) {
amount += Constants.HISTORY_OVER_BASE_CAPACITY_PER_PERSON
* (performance.getAudience()
- Constants.TRAGEDY_AUDIENCE_THRESHOLD);
}
break;
case "comedy":
amount = Constants.COMEDY_BASE_AMOUNT;
if (performance.getAudience()
> Constants.COMEDY_AUDIENCE_THRESHOLD) {
amount += Constants.COMEDY_OVER_BASE_CAPACITY_AMOUNT
+ (Constants.COMEDY_OVER_BASE_CAPACITY_PER_PERSON
* (performance.getAudience()
- Constants.COMEDY_AUDIENCE_THRESHOLD));
}
amount += Constants.COMEDY_AMOUNT_PER_AUDIENCE
* performance.getAudience();
break;
default:
throw new RuntimeException(String.format(
"unknown type: %s", play.getType()));
}

return amount;

}

// task 2.2-8
private int getVolumeCredits(Performance performance) {
int result = 0;

result += Math.max(
performance.getAudience()
- Constants.BASE_VOLUME_CREDIT_THRESHOLD, 0);

if ("comedy".equals(getPlay(performance).getType())) {
result += performance.getAudience()
/ Constants.COMEDY_EXTRA_VOLUME_FACTOR;
}
return result;
}

// total volume credits from all performence
private int getTotalVolumeCredits() {
int re = 0;
for (final Performance performance : invoice.getPerformances()) {
re += getVolumeCredits(performance);
}
return re;
}

// get total amount of money from performence (cents)
private int getTotalAmount() {
int re = 0;
for (final Performance performance : invoice.getPerformances()) {
re += getAmount(performance);
}
return re;
}

// change cents to usd
private String usd(int amountInCents) {
final NumberFormat formatter =
NumberFormat.getCurrencyInstance(Locale.US);
return formatter.format(amountInCents / Constants.PERCENT_FACTOR);
}

}