






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







// From book: 'Refactoring' by Martin Fowler public class Movie { public static final int CHILDRENS = 2; public static final int NEW_RELEASE = 1; public static final int REGULAR = 0; private String _title; private int _priceCode; public Movie(String title, int priceCode) { _title = title; _priceCode = priceCode; } public int getPriceCode() { }^ return _priceCode; public void setPriceCode(int arg) { _priceCode = arg; } public String getTitle() { return _title; }^ }
public class Rental { private Movie _movie; private int _daysRented; public Rental(Movie movie, int daysRented) { _movie = movie; _daysRented = daysRented; } public int getDaysRented() { return _daysRented; } public Movie getMovie() { return _movie; } }
public String statement() { double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rentals.elements(); String result = "Rental Record for " + getName() + "\n"; while (rentals.hasMoreElements()) { double thisAmount = 0; Rental each = (Rental) rentals.nextElement(); //determine amounts for each line switch (each.getMovie().getPriceCode()) { case Movie.REGULAR: case Movie.NEW_RELEASE: }^ case Movie.CHILDRENS: // add frequent renter points frequentRenterPoints++; // add bonus for a two day new release rental if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1) frequentRenterPoints++; // show figures for this rental result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(thisAmount) + "\n"; }^ totalAmount += thisAmount; // add footer lines result += "Amount owed is " + String.valueOf(totalAmount) + "\n"; result += "You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points"; }^ return result; More code here …
public class CustomerTest extends TestCase { public void testStatementForManyMovies() { Customer customer1 = new Customer("David"); Movie movie1 = new Movie("Madagascar", Movie.CHILDRENS); Rental rental1 = new Rental(movie1, 6); // 6 day rental Movie movie2 = new Movie("Star Wars", Movie.NEW_RELEASE); Rental rental2 = new Rental(movie2, 2); // 2 day rental Movie movie3 = new Movie("Gone with the Wind", Movie.REGULAR); Rental rental3 = new Rental(movie3, 8); // 8 day rental customer1.addRental(rental1); customer1.addRental(rental2); customer1.addRental(rental3); String expected = "Rental Record for David\n" + +^ "\tMadagascar\t6.0\n" + "\tStar Wars\t6.0\n" +"\tGone with the Wind\t11.0\n" "Amount owed is 23.0\n" + "You earned 4 frequent renter points"; String statement = customer1.statement(); assertEquals(expected, statement); }
You Owe " + String.valueOf(getTotalCharge()) +
\n";
";
aCustomer aRental aMovie statement() *[for all rentals] getCharge getPriceCode getTotalCharge getTotalFrequentRenterPoints *[for all rentals] getFrequentRenterPoints getPriceCode
Price getCharge getFrequentRenterPoints(days:int) New Release Price getCharge getFrequentRenterPoints Children’s Price getCharge Regular Price getCharge Movie getCharge (days: int) getFrequentRenterPoints(days:int) Title: String Rental getCharge() getFrequentRenterPoints() daysRented: int Customer Statement() getTotalCharge() getTotalFrequentRenterPoints()
aCustomer aRental aMovie statement() *[for all rentals] getCharge (^) getCharge(days) getTotalCharge getTotalFrequentRenterPoints *[for all rentals] getFrequentRenterPoints getFrequentRenterPoints(days) aPrice getCharge(days) getFrequentRenter(days)