@@ -140,4 +140,56 @@ public class CustomerTest { | |||
System.out.println(invoice_text); | |||
} | |||
/* | |||
@Test | |||
public void test_htmlStatement() { | |||
String expected_text = | |||
"Rentals for Customer_A\n" + | |||
"\n" + | |||
"Regular_Movie_1 : 2.0\n" + | |||
"Regular_Movie_2 : 3.5\n" + | |||
"Children_Movie_1 : 1.5\n" + | |||
"Children_Movie_2 : 3.0\n" + | |||
"NewRelease_Movie_1 : 9.0\n" + | |||
"NewRelease_Movie_2 : 12.0\n" + | |||
"\n" + | |||
"Amount owed is 31.0\n" + | |||
"\n" + | |||
"On this rental you earned 8 frequent renter points"; | |||
Movie regular_movie_1 = new Movie ("regular_movie_1", Movie.REGULAR ); | |||
Rental rental_1 = new Rental(regular_movie_1, 1); | |||
customer.addRental(rental_1); | |||
Movie regular_movie_2 = new Movie ("regular_movie_2", Movie.REGULAR ); | |||
Rental rental_2 = new Rental(regular_movie_2, 3); | |||
customer.addRental(rental_2); | |||
Movie childrens_movie_1 = new Movie ("childrens_movie_1", Movie.CHILDRENS ); | |||
Rental rental_3 = new Rental(childrens_movie_1, 1); | |||
customer.addRental(rental_3); | |||
Movie childrens_movie_2 = new Movie ("childrens_movie_2", Movie.CHILDRENS ); | |||
Rental rental_4 = new Rental(childrens_movie_2, 4); | |||
customer.addRental(rental_4); | |||
Movie new_release_movie_1 = new Movie ("new_release_movie_1", Movie.NEW_RELEASE ); | |||
Rental rental_5 = new Rental(new_release_movie_1, 3); | |||
customer.addRental(rental_5); | |||
Movie new_release_movie_2 = new Movie ("new_release_movie_2", Movie.NEW_RELEASE ); | |||
Rental rental_6 = new Rental(new_release_movie_2, 4); | |||
customer.addRental(rental_6); | |||
String invoice_text = customer.statement(); | |||
assertEquals(expected_text, invoice_text); | |||
System.out.println("test_statement_no_rentals"); | |||
System.out.println(invoice_text); | |||
} | |||
*/ | |||
} |
@@ -38,6 +38,7 @@ public class Application { | |||
customer_1.addRental( new Rental( movies.get( 5 ), 4 ) ); | |||
String statement = customer_1.statement(); | |||
System.out.println( statement ); | |||
System.out.println( customer_1.htmlStatement() ); | |||
} | |||
public void done() { |
@@ -0,0 +1,18 @@ | |||
public class ChildrensPrice implements Price { | |||
@Override | |||
public int getPriceCode() { | |||
return Movie.CHILDRENS; | |||
} | |||
@Override | |||
public double getFee(int daysRented) { | |||
double thisAmount = 1.5; | |||
if(daysRented > 3) { | |||
thisAmount += ( daysRented -3 ) * 1.5; | |||
} | |||
return thisAmount; | |||
} | |||
@Override | |||
public int getBonus( int daysRented ) { | |||
return 1; | |||
} | |||
} |
@@ -25,47 +25,36 @@ public class Customer { | |||
// add line for each rental | |||
for( Rental curRental : rentals ) { | |||
// show figures for this rental | |||
result.append("\t").append(curRental.getMovie().getTitle()).append("\t days rented: ").append(curRental.getDaysRented()).append(" = ").append(String.valueOf( calculateAmountFor(curRental) )).append("\n"); | |||
result.append("\t").append(curRental.getMovie().getTitle()).append("\t days rented: ").append(curRental.getDaysRented()).append(" = ").append(String.valueOf( curRental.calculateFee() )).append("\n"); | |||
} | |||
// add footer lines | |||
result.append("Amount owed is ").append(String.valueOf( getTotalAmount() )).append("\n"); | |||
result.append("You earned ").append(String.valueOf( getFrequentRenterPoints() )).append(" frequent renter points"); | |||
return result.toString(); | |||
} | |||
public double calculateAmountFor (Rental curRental) { | |||
double thisAmount = 0; | |||
// determine amounts for each line | |||
switch( curRental.getMovie().getPriceCode() ) { | |||
case Movie.REGULAR: | |||
thisAmount += 2; | |||
if( curRental.getDaysRented() > 2 ) | |||
thisAmount += ( curRental.getDaysRented() - 2 ) * 1.5; | |||
break; | |||
case Movie.NEW_RELEASE: | |||
thisAmount += curRental.getDaysRented() * 3; | |||
break; | |||
case Movie.CHILDRENS: | |||
thisAmount += 1.5; | |||
if( curRental.getDaysRented() > 3 ) | |||
thisAmount += ( curRental.getDaysRented() - 3 ) * 1.5; | |||
break; | |||
public String htmlStatement() { | |||
StringBuilder result = new StringBuilder(); | |||
result.append("<html>").append("\n"); | |||
result.append("<h1>").append("Rental Record for ").append(getName()).append("</h1>").append("\n"); | |||
for(Rental curRental : rentals) { | |||
result.append("<p>"); | |||
result.append("\t").append(curRental.getMovie().getTitle()); | |||
result.append("\t days rented: ").append(curRental.getDaysRented()); | |||
result.append(" = ").append(String.valueOf(curRental.calculateFee())).append("\n"); | |||
result.append("</p>\n"); | |||
} | |||
return thisAmount; | |||
} | |||
public int calculateFrequentRenterPointsFor (Rental curRental){ | |||
int frequentRenterPoints = 1; | |||
// add bonus for a two day new release rental | |||
if( ( curRental.getMovie().getPriceCode() == Movie.NEW_RELEASE ) && curRental.getDaysRented() > 1 ) | |||
frequentRenterPoints++; | |||
return frequentRenterPoints; | |||
result.append("<h3>Amount owed is ").append(String.valueOf(getTotalAmount())).append("</h3>\n"); | |||
result.append("<h3>You earned ").append(String.valueOf(getFrequentRenterPoints())).append(" frequent renter points</h3></html>"); | |||
return(result.toString()); | |||
} | |||
private int getFrequentRenterPoints() { | |||
int frequentRenterPoints = 0; | |||
for( Rental curRental : rentals ) { | |||
// add frequent renter points | |||
frequentRenterPoints += calculateFrequentRenterPointsFor(curRental); | |||
frequentRenterPoints += curRental.calculateBonus(); | |||
} | |||
return frequentRenterPoints; | |||
} | |||
@@ -73,7 +62,7 @@ public class Customer { | |||
private double getTotalAmount() { | |||
double totalAmount = 0; | |||
for( Rental curRental : rentals ) { | |||
double thisAmount = calculateAmountFor(curRental); | |||
double thisAmount = curRental.calculateFee(); | |||
totalAmount += thisAmount; | |||
} | |||
return totalAmount; |
@@ -3,13 +3,14 @@ public class Movie { | |||
public static final int CHILDRENS = 2; | |||
public static final int REGULAR = 0; | |||
public static final int NEW_RELEASE = 1; | |||
public static final int STUDENTS = 3; | |||
private final String title; | |||
private int priceCode; | |||
private Price price; | |||
public Movie( String title, int priceCode ) { | |||
this.title = title; | |||
this.priceCode = priceCode; | |||
setPriceCode( priceCode ); | |||
} | |||
public String getTitle() { | |||
@@ -17,10 +18,64 @@ public class Movie { | |||
} | |||
public int getPriceCode() { | |||
return priceCode; | |||
return price.getPriceCode(); | |||
} | |||
public void setPriceCode( int priceCode ) { | |||
this.priceCode = priceCode; | |||
switch( priceCode ) { | |||
case Movie.REGULAR: | |||
this.price = new RegularPrice(); | |||
break; | |||
case Movie.CHILDRENS: | |||
this.price = new ChildrensPrice(); | |||
break; | |||
case Movie.NEW_RELEASE: | |||
this.price = new NewReleasePrice(); | |||
break; | |||
case Movie.STUDENTS: | |||
this.price = new StudentsPrice(); | |||
break; | |||
default: | |||
throw new IllegalArgumentException("Incorrect Price Code"); | |||
} | |||
} | |||
public double calculateFee ( int daysRented) { | |||
return price.getFee( daysRented); | |||
} | |||
public int calculateBonus( int daysRented) { | |||
return price.getBonus( daysRented ); | |||
} | |||
/* public double calculateFee (int daysRented) { | |||
double thisAmount = 0; | |||
// determine amounts for each line | |||
switch( getPriceCode() ) { | |||
case Movie.REGULAR: | |||
thisAmount += 2; | |||
if( daysRented > 2 ) | |||
thisAmount += ( daysRented - 2 ) * 1.5; | |||
break; | |||
case Movie.NEW_RELEASE: | |||
thisAmount += daysRented * 3; | |||
break; | |||
case Movie.CHILDRENS: | |||
thisAmount += 1.5; | |||
if( daysRented > 3 ) | |||
thisAmount += ( daysRented - 3 ) * 1.5; | |||
break; | |||
} | |||
return thisAmount; | |||
} | |||
*/ | |||
/* public int calculateBonus (int daysRented){ | |||
int frequentRenterPoints = 1; | |||
// add bonus for a two day new release rental | |||
if( ( getPriceCode() == Movie.NEW_RELEASE ) && daysRented > 1 ) | |||
frequentRenterPoints++; | |||
return frequentRenterPoints; | |||
} | |||
*/ | |||
} |
@@ -0,0 +1,18 @@ | |||
public class NewReleasePrice implements Price { | |||
@Override | |||
public int getPriceCode() { | |||
return Movie.NEW_RELEASE; | |||
} | |||
@Override | |||
public double getFee(int daysRented) { | |||
double thisAmount = daysRented * 3; | |||
return thisAmount; | |||
} | |||
@Override | |||
public int getBonus( int daysRented ) { | |||
int frequentRenterPoints = 1; | |||
if( daysRented > 1 ) | |||
frequentRenterPoints++; | |||
return frequentRenterPoints; | |||
} | |||
} |
@@ -0,0 +1,5 @@ | |||
public interface Price { | |||
int getPriceCode(); | |||
double getFee( int daysRented ); | |||
int getBonus( int daysRented ); | |||
} |
@@ -0,0 +1,18 @@ | |||
public class RegularPrice implements Price { | |||
@Override | |||
public int getPriceCode() { | |||
return Movie.REGULAR; | |||
} | |||
@Override | |||
public double getFee(int daysRented) { | |||
double thisAmount = 2; | |||
if(daysRented > 2) { | |||
thisAmount += (daysRented -2) * 1.5; | |||
} | |||
return thisAmount; | |||
} | |||
@Override | |||
public int getBonus( int daysRented ) { | |||
return 1; | |||
} | |||
} |
@@ -15,4 +15,12 @@ public class Rental { | |||
public int getDaysRented() { | |||
return daysRented; | |||
} | |||
public double calculateFee () { | |||
return getMovie().calculateFee(getDaysRented()); | |||
} | |||
public int calculateBonus (){ | |||
return getMovie().calculateBonus(getDaysRented()); | |||
} | |||
} |
@@ -0,0 +1,18 @@ | |||
public class StudentsPrice implements Price { | |||
@Override | |||
public int getPriceCode() { | |||
return Movie.CHILDRENS; | |||
} | |||
@Override | |||
public double getFee(int daysRented) { | |||
double thisAmount = 1; | |||
if(daysRented > 2) { | |||
thisAmount += ( daysRented -2 ); | |||
} | |||
return thisAmount; | |||
} | |||
@Override | |||
public int getBonus( int daysRented ) { | |||
return 1; | |||
} | |||
} |