Permalink
Browse files

final

  • Loading branch information...
1 parent 7e194f0 commit 334968dd5bbcc68186dd33262a4e2965c61db15b @unclebob committed May 10, 2012
Showing with 210 additions and 67 deletions.
  1. +17 −0 src/ChildrensMovie.java
  2. +8 −19 src/Movie.java
  3. +13 −0 src/NewReleaseMovie.java
  4. +17 −0 src/RegularMovie.java
  5. +15 −11 src/Rental.java
  6. +70 −0 src/RentalStatement.java
  7. +70 −37 src/VideoStoreTest.java
View
@@ -0,0 +1,17 @@
+public class ChildrensMovie extends Movie {
+ public ChildrensMovie(String title) {
+ super(title);
+ }
+
+ public double determineAmount(int daysRented) {
+ double thisAmount = 1.5;
+ if (daysRented > 3)
+ thisAmount += (daysRented - 3) * 1.5;
+
+ return thisAmount;
+ }
+
+ public int determineFrequentRenterPoints(int daysRented) {
+ return 1;
+ }
+}
View
@@ -1,29 +1,18 @@
-public class Movie
+public abstract class Movie
{
- public static final int CHILDRENS = 2;
- public static final int REGULAR = 0;
- public static final int NEW_RELEASE = 1;
-
private String title;
- private int priceCode;
-
- public Movie (String title, int priceCode) {
+
+ public Movie(String title) {
this.title = title;
- this.priceCode = priceCode;
- }
-
- public int getPriceCode () {
- return priceCode;
- }
-
- public void setPriceCode (int code) {
- priceCode = code;
}
-
+
public String getTitle () {
return title;
}
-
+
+ public abstract double determineAmount(int daysRented);
+
+ public abstract int determineFrequentRenterPoints(int daysRented);
}
View
@@ -0,0 +1,13 @@
+public class NewReleaseMovie extends Movie {
+ public NewReleaseMovie(String title) {
+ super(title);
+ }
+
+ public double determineAmount(int daysRented) {
+ return daysRented * 3.0;
+ }
+
+ public int determineFrequentRenterPoints(int daysRented) {
+ return (daysRented > 1) ? 2 : 1;
+ }
+}
View
@@ -0,0 +1,17 @@
+public class RegularMovie extends Movie {
+ public RegularMovie(String title) {
+ super(title);
+ }
+
+ public double determineAmount(int daysRented) {
+ double thisAmount = 2;
+ if (daysRented > 2)
+ thisAmount += (daysRented - 2) * 1.5;
+
+ return thisAmount;
+ }
+
+ public int determineFrequentRenterPoints(int daysRented) {
+ return 1;
+ }
+}
View
@@ -1,19 +1,23 @@
public class Rental
{
+ private Movie movie;
+ private int daysRented;
+
public Rental (Movie movie, int daysRented) {
this.movie = movie;
this.daysRented = daysRented;
}
-
- public int getDaysRented () {
- return daysRented;
- }
-
- public Movie getMovie () {
- return movie;
- }
-
- private Movie movie;
- private int daysRented;
+
+ public String getTitle() {
+ return movie.getTitle();
+ }
+
+ public double determineAmount() {
+ return movie.determineAmount(daysRented);
+ }
+
+ public int determineFrequentRenterPoints() {
+ return movie.determineFrequentRenterPoints(daysRented);
+ }
}
View
@@ -0,0 +1,70 @@
+import java.util.ArrayList;
+import java.util.List;
+
+public class RentalStatement {
+ private String name;
+ private List<Rental> rentals = new ArrayList<Rental>();
+ private double totalAmount;
+ private int frequentRenterPoints;
+
+ public RentalStatement(String customerName) {
+ this.name = customerName;
+ }
+
+ public void addRental(Rental rental) {
+ rentals.add(rental);
+ }
+
+ public String makeRentalStatement() {
+ clearTotals();
+ return makeHeader() + makeRentalLines() + makeSummary();
+ }
+
+ private void clearTotals() {
+ totalAmount = 0;
+ frequentRenterPoints = 0;
+ }
+
+ private String makeHeader() {
+ return "Rental Record for " + getName() + "\n";
+ }
+
+ private String makeRentalLines() {
+ String rentalLines = "";
+
+ for (Rental rental : rentals)
+ rentalLines += makeRentalLine(rental);
+
+ return rentalLines;
+ }
+
+ private String makeRentalLine(Rental rental) {
+ double thisAmount = rental.determineAmount();
+ frequentRenterPoints += rental.determineFrequentRenterPoints();
+ totalAmount += thisAmount;
+
+ return formatRentalLine(rental, thisAmount);
+ }
+
+ private String formatRentalLine(Rental rental, double thisAmount) {
+ return "\t" + rental.getTitle() + "\t" + thisAmount + "\n";
+ }
+
+ private String makeSummary() {
+ return "You owed " + totalAmount + "\n" +
+ "You earned " + frequentRenterPoints +
+ " frequent renter points\n";
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public double getAmountOwed() {
+ return totalAmount;
+ }
+
+ public int getFrequentRenterPoints() {
+ return frequentRenterPoints;
+ }
+}
View
@@ -1,40 +1,73 @@
+import junit.framework.TestCase;
+public class VideoStoreTest extends TestCase {
+ private RentalStatement statement;
+ private Movie newRelease1;
+ private Movie newRelease2;
+ private Movie childrens;
+ private Movie regular1;
+ private Movie regular2;
+ private Movie regular3;
-import junit.framework.*;
-
-public class VideoStoreTest extends TestCase
-{
- public VideoStoreTest (String name) {
- super (name);
- }
-
- protected void setUp () {
- customer = new Customer ("Fred");
- }
-
- public void testSingleNewReleaseStatement () {
- customer.addRental (new Rental (new Movie ("The Cell", Movie.NEW_RELEASE), 3));
- assertEquals ("Rental Record for Fred\n\tThe Cell\t9.0\nYou owed 9.0\nYou earned 2 frequent renter points\n", customer.statement ());
- }
-
- public void testDualNewReleaseStatement () {
- customer.addRental (new Rental (new Movie ("The Cell", Movie.NEW_RELEASE), 3));
- customer.addRental (new Rental (new Movie ("The Tigger Movie", Movie.NEW_RELEASE), 3));
- assertEquals ("Rental Record for Fred\n\tThe Cell\t9.0\n\tThe Tigger Movie\t9.0\nYou owed 18.0\nYou earned 4 frequent renter points\n", customer.statement ());
- }
-
- public void testSingleChildrensStatement () {
- customer.addRental (new Rental (new Movie ("The Tigger Movie", Movie.CHILDRENS), 3));
- assertEquals ("Rental Record for Fred\n\tThe Tigger Movie\t1.5\nYou owed 1.5\nYou earned 1 frequent renter points\n", customer.statement ());
- }
-
- public void testMultipleRegularStatement () {
- customer.addRental (new Rental (new Movie ("Plan 9 from Outer Space", Movie.REGULAR), 1));
- customer.addRental (new Rental (new Movie ("8 1/2", Movie.REGULAR), 2));
- customer.addRental (new Rental (new Movie ("Eraserhead", Movie.REGULAR), 3));
-
- assertEquals ("Rental Record for Fred\n\tPlan 9 from Outer Space\t2.0\n\t8 1/2\t2.0\n\tEraserhead\t3.5\nYou owed 7.5\nYou earned 3 frequent renter points\n", customer.statement ());
- }
-
- private Customer customer;
+ public VideoStoreTest(String name) {
+ super(name);
+ }
+
+ protected void setUp() {
+ statement = new RentalStatement("Customer Name");
+ newRelease1 = new NewReleaseMovie("New Release 1");
+ newRelease2 = new NewReleaseMovie("New Release 2");
+ childrens = new ChildrensMovie("Childrens");
+ regular1 = new RegularMovie("Regular 1");
+ regular2 = new RegularMovie("Regular 2");
+ regular3 = new RegularMovie("Regular 3");
+ }
+
+ private void assertAmountAndPointsForReport(double expectedAmount, int expectedPoints) {
+ assertEquals(expectedAmount, statement.getAmountOwed());
+ assertEquals(expectedPoints, statement.getFrequentRenterPoints());
+ }
+
+ public void testSingleNewReleaseStatement() {
+ statement.addRental(new Rental(newRelease1, 3));
+ statement.makeRentalStatement();
+ assertAmountAndPointsForReport(9.0, 2);
+ }
+
+ public void testDualNewReleaseStatement() {
+ statement.addRental(new Rental(newRelease1, 3));
+ statement.addRental(new Rental(newRelease2, 3));
+ statement.makeRentalStatement();
+ assertAmountAndPointsForReport(18.0, 4);
+ }
+
+ public void testSingleChildrensStatement() {
+ statement.addRental(new Rental(childrens, 3));
+ statement.makeRentalStatement();
+ assertAmountAndPointsForReport(1.5, 1);
+ }
+
+
+ public void testMultipleRegularStatement() {
+ statement.addRental(new Rental(regular1, 1));
+ statement.addRental(new Rental(regular2, 2));
+ statement.addRental(new Rental(regular3, 3));
+ statement.makeRentalStatement();
+ assertAmountAndPointsForReport(7.5, 3);
+ }
+
+ public void testRentalStatementFormat() {
+ statement.addRental(new Rental(regular1, 1));
+ statement.addRental(new Rental(regular2, 2));
+ statement.addRental(new Rental(regular3, 3));
+
+ assertEquals(
+ "Rental Record for Customer Name\n" +
+ "\tRegular 1\t2.0\n" +
+ "\tRegular 2\t2.0\n" +
+ "\tRegular 3\t3.5\n" +
+ "You owed 7.5\n" +
+ "You earned 3 frequent renter points\n",
+ statement.makeRentalStatement());
+ }
}

0 comments on commit 334968d

Please sign in to comment.