Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: alexandreaquiles/the_rental_example
base: master
...
head fork: yuanmai/the_rental_example
compare: start
Checking mergeability… Don't worry, you can still create the pull request.
  • 1 commit
  • 7 files changed
  • 0 commit comments
  • 1 contributor
Commits on Nov 18, 2011
@yuanmai yuanmai Project File f7e327b
View
2  .gitignore
@@ -2,3 +2,5 @@ bin
.classpath
.project
.settings
+.idea
+out
View
22 The_rental_example.iml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+ <component name="NewModuleRootManager" inherit-compiler-output="true">
+ <exclude-output />
+ <content url="file://$MODULE_DIR$">
+ <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+ <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
+ </content>
+ <orderEntry type="inheritedJdk" />
+ <orderEntry type="sourceFolder" forTests="false" />
+ <orderEntry type="module-library" scope="TEST">
+ <library>
+ <CLASSES>
+ <root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.8.jar!/" />
+ </CLASSES>
+ <JAVADOC />
+ <SOURCES />
+ </library>
+ </orderEntry>
+ </component>
+</module>
+
View
108 src/com/aquiles/alexandre/Customer.java
@@ -4,61 +4,61 @@
import java.util.Collection;
public class Customer {
-
- private String name;
- private Collection<Rental> rentals;
-
- public Customer(String name) {
- super();
- rentals = new ArrayList<Rental>();
- this.name = name;
- }
-
- public String getName() {
- return name;
- }
- public void addRental(Rental rental) {
- rentals.add(rental);
- }
+ private String name;
+ private Collection<Rental> rentals;
- public String statement() {
- double totalAmount = 0;
- int frequentRenterPoints = 0;
- String result = "Rental record for " + getName() + "\n";
- for(Rental rental : rentals) {
- double thisAmount = 0;
- switch(rental.getMovie().getPriceCode()) {
- case Movie.REGULAR:
- thisAmount += 2;
- if(rental.getDaysRented() > 2)
- thisAmount += (rental.getDaysRented() - 2) * 1.5;
- break;
- case Movie.NEW_RELEASE:
- thisAmount += rental.getDaysRented() * 3;
- break;
- case Movie.CHILDRENS:
- thisAmount += 1.5;
- if(rental.getDaysRented() > 3)
- thisAmount += (rental.getDaysRented() - 3) * 1.5;
- break;
- }
-
- //add frequent renter points
- frequentRenterPoints++;
+ public Customer(String name) {
+ super();
+ rentals = new ArrayList<Rental>();
+ this.name = name;
+ }
- //add bonus for a two day new release rental
- if(rental.getMovie().getPriceCode() == Movie.NEW_RELEASE && rental.getDaysRented() > 1)
- frequentRenterPoints++;
-
- //show figures for this rental
- result += "\t" + rental.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;
- }
+ public String getName() {
+ return name;
+ }
+
+ public void addRental(Rental rental) {
+ rentals.add(rental);
+ }
+
+ public String statement() {
+ double totalAmount = 0;
+ int frequentRenterPoints = 0;
+ String result = "Rental record for " + getName() + "\n";
+ for (Rental rental : rentals) {
+ double thisAmount = 0;
+ switch (rental.getMovie().getPriceCode()) {
+ case REGULAR:
+ thisAmount += 2;
+ if (rental.getDaysRented() > 2)
+ thisAmount += (rental.getDaysRented() - 2) * 1.5;
+ break;
+ case NEW_RELEASE:
+ thisAmount += rental.getDaysRented() * 3;
+ break;
+ case CHILDRENS:
+ thisAmount += 1.5;
+ if (rental.getDaysRented() > 3)
+ thisAmount += (rental.getDaysRented() - 3) * 1.5;
+ break;
+ }
+
+ //add frequent renter points
+ frequentRenterPoints++;
+
+ //add bonus for a two day new release rental
+ if (rental.getMovie().getPriceCode() == Movie.PriceCode.NEW_RELEASE && rental.getDaysRented() > 1)
+ frequentRenterPoints++;
+
+ //show figures for this rental
+ result += "\t" + rental.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;
+ }
}
View
36 src/com/aquiles/alexandre/Movie.java
@@ -1,32 +1,22 @@
package com.aquiles.alexandre;
public class Movie {
+ public enum PriceCode {REGULAR, NEW_RELEASE, CHILDRENS}
- public static final int REGULAR = 0;
- public static final int NEW_RELEASE = 1;
- public static final int CHILDRENS = 2;
+ private final String title;
+ private final PriceCode priceCode;
- private String title;
- private Integer priceCode;
-
- public Movie(String title, Integer priceCode) {
- super();
- this.title = title;
- this.priceCode = priceCode;
- }
+ public Movie(String title, PriceCode priceCode) {
+ this.title = title;
+ this.priceCode = priceCode;
+ }
- public Integer getPriceCode() {
- return priceCode;
- }
+ public PriceCode getPriceCode() {
+ return priceCode;
+ }
- public void setPriceCode(Integer priceCode) {
- this.priceCode = priceCode;
- }
+ public String getTitle() {
+ return title;
+ }
- public String getTitle() {
- return title;
- }
-
-
-
}
View
32 src/com/aquiles/alexandre/Rental.java
@@ -1,24 +1,22 @@
package com.aquiles.alexandre;
-
public class Rental {
- private Movie movie;
- private Integer daysRented;
-
- public Rental(Movie movie, Integer daysRented) {
- super();
- this.movie = movie;
- this.daysRented = daysRented;
- }
+ private Movie movie;
+ private Integer daysRented;
+
+ public Rental(Movie movie, Integer daysRented) {
+ super();
+ this.movie = movie;
+ this.daysRented = daysRented;
+ }
+
+ public Movie getMovie() {
+ return movie;
+ }
- public Movie getMovie() {
- return movie;
- }
+ public Integer getDaysRented() {
+ return daysRented;
+ }
- public Integer getDaysRented() {
- return daysRented;
- }
-
-
}
View
194 src/com/aquiles/alexandre/RentalTest.java
@@ -1,194 +0,0 @@
-package com.aquiles.alexandre;
-
-import java.util.Formatter;
-import java.util.LinkedHashMap;
-import java.util.Locale;
-import java.util.Map;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-public class RentalTest {
-
- @Test
- public void shortRegularRental() {
- Customer customer = createCustomer();
- addRental(customer, "Groundhog Day", Movie.REGULAR, 2);
-
- String expected =
- new StatementConfig()
- .addPartialAmount("Groundhog Day", 2.0)
- .addTotalAmount(2.0)
- .addFrequentRenterPoints(1)
- .expectedStatement();
-
- Assert.assertEquals(expected, customer.statement());
- }
-
- @Test
- public void longRegularRental() {
- Customer customer = createCustomer();
- addRental(customer, "Groundhog Day", Movie.REGULAR, 3);
-
- String expected =
- new StatementConfig()
- .addPartialAmount("Groundhog Day", 3.5)
- .addTotalAmount(3.5)
- .addFrequentRenterPoints(1)
- .expectedStatement();
-
- Assert.assertEquals(expected, customer.statement());
- }
-
- @Test
- public void shortNewReleaseRental() {
- Customer customer = createCustomer();
- addRental(customer, "X-Men X", Movie.NEW_RELEASE, 1);
-
- String expected =
- new StatementConfig()
- .addPartialAmount("X-Men X", 3.0)
- .addTotalAmount(3.0)
- .addFrequentRenterPoints(1)
- .expectedStatement();
-
- Assert.assertEquals(expected, customer.statement());
- }
-
- @Test
- public void longNewReleaseRental() {
- Customer customer = createCustomer();
- addRental(customer, "X-Men X", Movie.NEW_RELEASE, 2);
-
- String expected =
- new StatementConfig()
- .addPartialAmount("X-Men X", 6.0)
- .addTotalAmount(6.0)
- .addFrequentRenterPoints(2)
- .expectedStatement();
-
- Assert.assertEquals(expected, customer.statement());
- }
-
- @Test
- public void shortChildrensRental() {
- Customer customer = createCustomer();
- addRental(customer, "Toy Story", Movie.CHILDRENS, 3);
-
- String expected =
- new StatementConfig()
- .addPartialAmount("Toy Story", 1.5)
- .addTotalAmount(1.5)
- .addFrequentRenterPoints(1)
- .expectedStatement();
-
- Assert.assertEquals(expected, customer.statement());
- }
-
- @Test
- public void longChildrensRental() {
- Customer customer = createCustomer();
- addRental(customer, "Toy Story", Movie.CHILDRENS, 4);
-
- String expected =
- new StatementConfig()
- .addPartialAmount("Toy Story", 3.0)
- .addTotalAmount(3.0)
- .addFrequentRenterPoints(1)
- .expectedStatement();
-
- Assert.assertEquals(expected, customer.statement());
- }
-
- @Test
- public void variousRentals() {
- Customer customer = createCustomer();
- addRental(customer, "Groundhog Day", Movie.REGULAR, 3);
- addRental(customer, "X-Men X", Movie.NEW_RELEASE, 2);
- addRental(customer, "Toy Story", Movie.CHILDRENS, 4);
-
- String expected =
- new StatementConfig()
- .addPartialAmount("Groundhog Day", 3.5)
- .addPartialAmount("X-Men X", 6.0)
- .addPartialAmount("Toy Story", 3.0)
- .addTotalAmount(12.5)
- .addFrequentRenterPoints(4)
- .expectedStatement();
-
- Assert.assertEquals(expected, customer.statement());
- }
-
- @Test
- public void expectedStatementTest() {
- String result = new StatementConfig().addPartialAmount("Groundhog Day", 3.0).addTotalAmount(3.0).addFrequentRenterPoints(1).expectedStatement();
- String expected = "Rental record for Luke\n";
- expected += "\tGroundhog Day\t3.0\n";
- expected += "Amount owed is 3.0\n";
- expected += "You earned 1 frequent renter points";
- Assert.assertEquals(expected, result);
- }
-
- private Customer createCustomer(){
- return new Customer("Luke");
- }
-
- private void addRental(Customer customer, String movieName, Integer priceCode, Integer daysRented) {
- Movie movie = new Movie(movieName, priceCode);
- Rental rental = new Rental(movie, daysRented);
- customer.addRental(rental);
- }
-
-}
-
-class StatementConfig {
- private Map<String, Double> partialAmountsByMovieName;
- private Double totalAmount;
- private Integer frequentRenterPoints;
-
- public StatementConfig() {
- this.partialAmountsByMovieName = new LinkedHashMap<String, Double>();
- }
-
- public StatementConfig addPartialAmount(String movieName, Double partialAmount) {
- partialAmountsByMovieName.put(movieName, partialAmount);
- return this;
- }
-
- public StatementConfig addTotalAmount(Double totalAmount) {
- this.totalAmount = totalAmount;
- return this;
- }
-
- public StatementConfig addFrequentRenterPoints(Integer frequentRenterPoints) {
- this.frequentRenterPoints = frequentRenterPoints;
- return this;
- }
-
- public String expectedStatement() {
- String format = "Rental record for Luke\n";
- for (int i= 0; i < partialAmountsByMovieName.size(); i++) {
- format += "\t%s\t%.1f\n";
- }
- format += "Amount owed is %.1f\n";
- format += "You earned %d frequent renter points";
-
- Formatter formatter = new Formatter();
- formatter.format(Locale.US, format, getParameters());
-
- return formatter.toString();
- }
-
- private Object[] getParameters() {
- Object[] parameters = new Object[2*partialAmountsByMovieName.size()+2];
- int i = -1;
- for(String movieName : partialAmountsByMovieName.keySet()) {
- parameters[++i] = movieName;
- parameters[++i] = partialAmountsByMovieName.get(movieName);
- }
- parameters[++i] = totalAmount;
- parameters[++i] = frequentRenterPoints;
- return parameters;
- }
-
-}
View
194 test/com/aquiles/alexandre/RentalTest.java
@@ -0,0 +1,194 @@
+package com.aquiles.alexandre;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Formatter;
+import java.util.LinkedHashMap;
+import java.util.Locale;
+import java.util.Map;
+
+public class RentalTest {
+
+ @Test
+ public void shortRegularRental() {
+ Customer customer = createCustomer();
+ addRental(customer, "Groundhog Day", Movie.PriceCode.REGULAR, 2);
+
+ String expected =
+ new StatementConfig()
+ .addPartialAmount("Groundhog Day", 2.0)
+ .addTotalAmount(2.0)
+ .addFrequentRenterPoints(1)
+ .expectedStatement();
+
+ Assert.assertEquals(expected, customer.statement());
+ }
+
+ @Test
+ public void longRegularRental() {
+ Customer customer = createCustomer();
+ addRental(customer, "Groundhog Day", Movie.PriceCode.REGULAR, 3);
+
+ String expected =
+ new StatementConfig()
+ .addPartialAmount("Groundhog Day", 3.5)
+ .addTotalAmount(3.5)
+ .addFrequentRenterPoints(1)
+ .expectedStatement();
+
+ Assert.assertEquals(expected, customer.statement());
+ }
+
+ @Test
+ public void shortNewReleaseRental() {
+ Customer customer = createCustomer();
+ addRental(customer, "X-Men X", Movie.PriceCode.NEW_RELEASE, 1);
+
+ String expected =
+ new StatementConfig()
+ .addPartialAmount("X-Men X", 3.0)
+ .addTotalAmount(3.0)
+ .addFrequentRenterPoints(1)
+ .expectedStatement();
+
+ Assert.assertEquals(expected, customer.statement());
+ }
+
+ @Test
+ public void longNewReleaseRental() {
+ Customer customer = createCustomer();
+ addRental(customer, "X-Men X", Movie.PriceCode.NEW_RELEASE, 2);
+
+ String expected =
+ new StatementConfig()
+ .addPartialAmount("X-Men X", 6.0)
+ .addTotalAmount(6.0)
+ .addFrequentRenterPoints(2)
+ .expectedStatement();
+
+ Assert.assertEquals(expected, customer.statement());
+ }
+
+ @Test
+ public void shortChildrensRental() {
+ Customer customer = createCustomer();
+ addRental(customer, "Toy Story", Movie.PriceCode.CHILDRENS, 3);
+
+ String expected =
+ new StatementConfig()
+ .addPartialAmount("Toy Story", 1.5)
+ .addTotalAmount(1.5)
+ .addFrequentRenterPoints(1)
+ .expectedStatement();
+
+ Assert.assertEquals(expected, customer.statement());
+ }
+
+ @Test
+ public void longChildrensRental() {
+ Customer customer = createCustomer();
+ addRental(customer, "Toy Story", Movie.PriceCode.CHILDRENS, 4);
+
+ String expected =
+ new StatementConfig()
+ .addPartialAmount("Toy Story", 3.0)
+ .addTotalAmount(3.0)
+ .addFrequentRenterPoints(1)
+ .expectedStatement();
+
+ Assert.assertEquals(expected, customer.statement());
+ }
+
+ @Test
+ public void variousRentals() {
+ Customer customer = createCustomer();
+ addRental(customer, "Groundhog Day", Movie.PriceCode.REGULAR, 3);
+ addRental(customer, "X-Men X", Movie.PriceCode.NEW_RELEASE, 2);
+ addRental(customer, "Toy Story", Movie.PriceCode.CHILDRENS, 4);
+
+ String expected =
+ new StatementConfig()
+ .addPartialAmount("Groundhog Day", 3.5)
+ .addPartialAmount("X-Men X", 6.0)
+ .addPartialAmount("Toy Story", 3.0)
+ .addTotalAmount(12.5)
+ .addFrequentRenterPoints(4)
+ .expectedStatement();
+
+ Assert.assertEquals(expected, customer.statement());
+ }
+
+ @Test
+ public void expectedStatementTest() {
+ String result = new StatementConfig().addPartialAmount("Groundhog Day", 3.0).addTotalAmount(3.0).addFrequentRenterPoints(1).expectedStatement();
+ String expected = "Rental record for Luke\n";
+ expected += "\tGroundhog Day\t3.0\n";
+ expected += "Amount owed is 3.0\n";
+ expected += "You earned 1 frequent renter points";
+ Assert.assertEquals(expected, result);
+ }
+
+ private Customer createCustomer() {
+ return new Customer("Luke");
+ }
+
+ private void addRental(Customer customer, String movieName, Movie.PriceCode priceCode, Integer daysRented) {
+ Movie movie = new Movie(movieName, priceCode);
+ Rental rental = new Rental(movie, daysRented);
+ customer.addRental(rental);
+ }
+
+}
+
+class StatementConfig {
+ private Map<String, Double> partialAmountsByMovieName;
+ private Double totalAmount;
+ private Integer frequentRenterPoints;
+
+ public StatementConfig() {
+ this.partialAmountsByMovieName = new LinkedHashMap<String, Double>();
+ }
+
+ public StatementConfig addPartialAmount(String movieName, Double partialAmount) {
+ partialAmountsByMovieName.put(movieName, partialAmount);
+ return this;
+ }
+
+ public StatementConfig addTotalAmount(Double totalAmount) {
+ this.totalAmount = totalAmount;
+ return this;
+ }
+
+ public StatementConfig addFrequentRenterPoints(Integer frequentRenterPoints) {
+ this.frequentRenterPoints = frequentRenterPoints;
+ return this;
+ }
+
+ public String expectedStatement() {
+ String format = "Rental record for Luke\n";
+ for (int i = 0; i < partialAmountsByMovieName.size(); i++) {
+ format += "\t%s\t%.1f\n";
+ }
+ format += "Amount owed is %.1f\n";
+ format += "You earned %d frequent renter points";
+
+ Formatter formatter = new Formatter();
+ formatter.format(Locale.US, format, getParameters());
+
+ return formatter.toString();
+ }
+
+ private Object[] getParameters() {
+ Object[] parameters = new Object[2 * partialAmountsByMovieName.size() + 2];
+ int i = -1;
+ for (String movieName : partialAmountsByMovieName.keySet()) {
+ parameters[++i] = movieName;
+ parameters[++i] = partialAmountsByMovieName.get(movieName);
+ }
+ parameters[++i] = totalAmount;
+ parameters[++i] = frequentRenterPoints;
+ return parameters;
+ }
+
+}

No commit comments for this range

Something went wrong with that request. Please try again.