diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 00000000..2ee03b50
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/log.txt b/log.txt
index e69de29b..3441ddff 100644
--- a/log.txt
+++ b/log.txt
@@ -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
\ No newline at end of file
diff --git a/refactoring.iml b/refactoring.iml
new file mode 100644
index 00000000..9e3449c9
--- /dev/null
+++ b/refactoring.iml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/java/theater/Constants.java b/src/main/java/theater/Constants.java
index f7846461..b53e3e82 100644
--- a/src/main/java/theater/Constants.java
+++ b/src/main/java/theater/Constants.java
@@ -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;
diff --git a/src/main/java/theater/Performance.java b/src/main/java/theater/Performance.java
index b53a6475..ad9246de 100644
--- a/src/main/java/theater/Performance.java
+++ b/src/main/java/theater/Performance.java
@@ -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;
+ }
}
+
diff --git a/src/main/java/theater/Play.java b/src/main/java/theater/Play.java
index 95a0b037..c4461990 100644
--- a/src/main/java/theater/Play.java
+++ b/src/main/java/theater/Play.java
@@ -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
diff --git a/src/main/java/theater/StatementPrinter.java b/src/main/java/theater/StatementPrinter.java
index 130497b5..8e6ac3b6 100644
--- a/src/main/java/theater/StatementPrinter.java
+++ b/src/main/java/theater/StatementPrinter.java
@@ -8,8 +8,8 @@
* This class generates a statement for a given invoice of performances.
*/
public class StatementPrinter {
- public Invoice invoice;
- public Map plays;
+ private final Invoice invoice;
+ private final Map plays;
public StatementPrinter(Invoice invoice, Map plays) {
this.invoice = invoice;
@@ -22,47 +22,104 @@ public StatementPrinter(Invoice invoice, Map 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);
+ }
+
}