Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brandon 1.1 #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
38 changes: 12 additions & 26 deletions src/main/java/bad/robot/refactoring/chapter1/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,22 @@ public void addRental(Rental rental) {
}

public String statement() {
double totalAmount = 0;
int frequentRenterPoints = 0;

String result = "Rental record for " + getName() + "\n";
for (Rental rental : rentals)
result += "\t" + rental.getMovie().getTitle() + "\t" + String.valueOf(rental.getCharge()) + "\n";
result += "Amount owed is " + String.valueOf(getTotalCharge()) + "\n";
result += "You earned " + String.valueOf(getTotalFrequentRenterPoints()) + " frequent renter points";
return result;
}
for (Rental rental : rentals) {
frequentRenterPoints += rental.calculateFrequentRenterPoints();

public String htmlStatement() {
String result = "<h1>Rental record for <b>" + getName() + "</b></h1>\n";
for (Rental rental : rentals)
result += "<p>" + rental.getMovie().getTitle() + "\t" + String.valueOf(rental.getCharge()) + "</p>\n";
result += "<p>Amount owed is <b>" + String.valueOf(getTotalCharge()) + "</b></p>\n";
result += "<p>You earned <b>" + String.valueOf(getTotalFrequentRenterPoints()) + " frequent renter points</b></p>";
return result;
// show figures for this rental
result += "\t" + rental.getMovie().getTitle() + "\t" + String.valueOf(rental.getChargeFor()) + "\n";

}
totalAmount += rental.getChargeFor();
}

private double getTotalCharge() {
double total = 0;
for (Rental rental : rentals)
total += rental.getCharge();
return total;
}
result += "Amount owed is " + String.valueOf(totalAmount) + "\n";
result += "You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points";

private int getTotalFrequentRenterPoints() {
int total = 0;
for (Rental rental : rentals)
total += rental.getFrequentRenterPoints();
return total;
return result;
}

}
34 changes: 7 additions & 27 deletions src/main/java/bad/robot/refactoring/chapter1/Movie.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,23 @@ public class Movie {
public static final int NEW_RELEASE = 1;

private String title;
Price price;
private int priceCode;

public Movie(String title, int priceCode) {
this.title = title;
setPriceCode(priceCode);
this.priceCode = priceCode;
}

public String getTitle() {
return title;
}

private void setPriceCode(int priceCode) {
switch (priceCode) {
case CHILDREN:
price = new ChildrensPrice();
break;
case NEW_RELEASE:
price = new NewReleasePrice();
break;
case REGULAR:
price = new RegularPrice();
break;
default:
throw new IllegalArgumentException("invalid price code");
}
public int getPriceCode() {
return priceCode;
}

private int getPriceCode() {
return price.getPriceCode();
public void setPriceCode(int priceCode) {
this.priceCode = priceCode;
}

public double getCharge(int daysRented) {
return price.getCharge(daysRented);
}

public int getFrequentRenterPoints(int daysRented) {
return price.getFrequentRenterPoints(daysRented);
}

}
}
36 changes: 29 additions & 7 deletions src/main/java/bad/robot/refactoring/chapter1/Rental.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package bad.robot.refactoring.chapter1;

import java.util.ArrayList;
import java.util.List;

public class Rental {

Movie movie;
private Movie movie;
private int daysRented;

public Rental(Movie movie, int daysRented) {
Expand All @@ -18,11 +21,30 @@ public int getDaysRented() {
return daysRented;
}

public double getCharge() {
return movie.getCharge(daysRented);
}
double getChargeFor() {
double amount = 0;
switch (getMovie().getPriceCode()) {
case Movie.REGULAR:
amount += 2;
if (getDaysRented() > 2)
amount += (getDaysRented() - 2) * 1.5;
break;
case Movie.NEW_RELEASE:
amount += getDaysRented() * 3;
break;
case Movie.CHILDREN:
amount += 1.5;
if (getDaysRented() > 3)
amount += (getDaysRented() - 3) * 1.5;
break;
}
return amount;
}

int calculateFrequentRenterPoints() {
if (getMovie().getPriceCode() == Movie.NEW_RELEASE && getDaysRented() > 1)
return 2;
return 1;
}

public int getFrequentRenterPoints() {
return movie.getFrequentRenterPoints(daysRented);
}
}