Compare commits
4 commits
9137e7e133
...
a95bf5e7a2
| Author | SHA1 | Date | |
|---|---|---|---|
| a95bf5e7a2 | |||
| 73de7805c6 | |||
| 365875bef2 | |||
| 2a5a8b0a7c |
16 changed files with 225 additions and 23 deletions
BIN
.DS_Store
vendored
BIN
.DS_Store
vendored
Binary file not shown.
BIN
src/.DS_Store
vendored
BIN
src/.DS_Store
vendored
Binary file not shown.
BIN
src/main/.DS_Store
vendored
Normal file
BIN
src/main/.DS_Store
vendored
Normal file
Binary file not shown.
BIN
src/main/java/.DS_Store
vendored
Normal file
BIN
src/main/java/.DS_Store
vendored
Normal file
Binary file not shown.
|
|
@ -26,24 +26,7 @@ public class Customer {
|
||||||
String result = "Rental Record for " + getName() + "\n";
|
String result = "Rental Record for " + getName() + "\n";
|
||||||
|
|
||||||
for (Rental each : _rentals) {
|
for (Rental each : _rentals) {
|
||||||
double thisAmount = 0;
|
double thisAmount = each.calculatePrice();
|
||||||
|
|
||||||
//determine amounts for each line
|
|
||||||
switch (each.getMovie().getPriceCode()) {
|
|
||||||
case Movie.REGULAR:
|
|
||||||
thisAmount += 2;
|
|
||||||
if (each.getDaysRented() > 2)
|
|
||||||
thisAmount += (each.getDaysRented() - 2) * 1.5;
|
|
||||||
break;
|
|
||||||
case Movie.NEW_RELEASE:
|
|
||||||
thisAmount += each.getDaysRented() * 3;
|
|
||||||
break;
|
|
||||||
case Movie.CHILDRENS:
|
|
||||||
thisAmount += 1.5;
|
|
||||||
if (each.getDaysRented() > 3)
|
|
||||||
thisAmount += (each.getDaysRented() - 3) * 1.5;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// add frequent renter points
|
// add frequent renter points
|
||||||
frequentRenterPoints++;
|
frequentRenterPoints++;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
package movierental;
|
package movierental;
|
||||||
|
|
||||||
|
import movierental.MoviePriceCalculation.MoviePriceCalculationStrategy;
|
||||||
|
|
||||||
public class Movie {
|
public class Movie {
|
||||||
|
|
||||||
public static final int CHILDRENS = 2;
|
public static final int CHILDRENS = 2;
|
||||||
|
|
@ -8,10 +10,22 @@ public class Movie {
|
||||||
|
|
||||||
private String _title;
|
private String _title;
|
||||||
private int _priceCode;
|
private int _priceCode;
|
||||||
|
private MoviePriceCalculationStrategy moviePriceCalculationStrategy;
|
||||||
|
|
||||||
public Movie(String title, int priceCode) {
|
public Movie(String title, int priceCode) {
|
||||||
_title = title;
|
_title = title;
|
||||||
_priceCode = priceCode;
|
_priceCode = priceCode;
|
||||||
|
switch (priceCode) {
|
||||||
|
case REGULAR:
|
||||||
|
moviePriceCalculationStrategy = new movierental.MoviePriceCalculation.RegularMoviePriceCalculation();
|
||||||
|
break;
|
||||||
|
case NEW_RELEASE:
|
||||||
|
moviePriceCalculationStrategy = new movierental.MoviePriceCalculation.NewReleaseMoviePriceCalculation();
|
||||||
|
break;
|
||||||
|
case CHILDRENS:
|
||||||
|
moviePriceCalculationStrategy = new movierental.MoviePriceCalculation.ChildrenMoviePriceCalculation();
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPriceCode() {
|
public int getPriceCode() {
|
||||||
|
|
@ -26,5 +40,8 @@ public class Movie {
|
||||||
return _title;
|
return _title;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double calculatePrice(int daysRented) {
|
||||||
|
return moviePriceCalculationStrategy.calculatePrice(daysRented);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
package movierental.MoviePriceCalculation;
|
||||||
|
|
||||||
|
public class ChildrenMoviePriceCalculation implements MoviePriceCalculationStrategy {
|
||||||
|
@Override
|
||||||
|
public double calculatePrice(int daysRented) {
|
||||||
|
double thisAmount = 1.5;
|
||||||
|
if (daysRented > 3) {
|
||||||
|
thisAmount += (daysRented - 3) * 1.5;
|
||||||
|
}
|
||||||
|
return thisAmount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
package movierental.MoviePriceCalculation;
|
||||||
|
|
||||||
|
public interface MoviePriceCalculationStrategy {
|
||||||
|
double calculatePrice(int daysRented);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
package movierental.MoviePriceCalculation;
|
||||||
|
|
||||||
|
public class NewReleaseMoviePriceCalculation implements MoviePriceCalculationStrategy {
|
||||||
|
@Override
|
||||||
|
public double calculatePrice(int daysRented) {
|
||||||
|
return daysRented * 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
package movierental.MoviePriceCalculation;
|
||||||
|
|
||||||
|
public class RegularMoviePriceCalculation implements MoviePriceCalculationStrategy {
|
||||||
|
@Override
|
||||||
|
public double calculatePrice(int daysRented) {
|
||||||
|
double thisAmount = 2;
|
||||||
|
if (daysRented > 2) {
|
||||||
|
thisAmount += (daysRented - 2) * 1.5;
|
||||||
|
}
|
||||||
|
return thisAmount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -20,4 +20,8 @@ public class Rental {
|
||||||
public Movie getMovie() {
|
public Movie getMovie() {
|
||||||
return _movie;
|
return _movie;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double calculatePrice() {
|
||||||
|
return _movie.calculatePrice(_daysRented);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
BIN
src/test/.DS_Store
vendored
Normal file
BIN
src/test/.DS_Store
vendored
Normal file
Binary file not shown.
BIN
src/test/java/.DS_Store
vendored
Normal file
BIN
src/test/java/.DS_Store
vendored
Normal file
Binary file not shown.
|
|
@ -1,21 +1,22 @@
|
||||||
package movierental;
|
package movierental;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
|
|
||||||
public class CustomerTest {
|
public class CustomerTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void testSingleRegularMovie() {
|
||||||
// Given
|
// Given
|
||||||
Customer customer = new Customer("Bob");
|
Customer customer = new Customer("Bob");
|
||||||
Movie mockMovie = mock(Movie.class);
|
Movie mockMovie = Mockito.mock(Movie.class);
|
||||||
|
|
||||||
when(mockMovie.getTitle()).thenReturn("Mock Movie");
|
when(mockMovie.getTitle()).thenReturn("Mock Movie");
|
||||||
when(mockMovie.getPriceCode()).thenReturn(Movie.REGULAR);
|
when(mockMovie.getPriceCode()).thenReturn(Movie.REGULAR);
|
||||||
|
when(mockMovie.calculatePrice(2)).thenReturn(2.0);
|
||||||
customer.addRental(new Rental(mockMovie, 2));
|
customer.addRental(new Rental(mockMovie, 2));
|
||||||
|
|
||||||
// When
|
// When
|
||||||
|
|
@ -28,6 +29,54 @@ public class CustomerTest {
|
||||||
"Amount owed is 2.0\n" +
|
"Amount owed is 2.0\n" +
|
||||||
"You earned 1 frequent renter points";
|
"You earned 1 frequent renter points";
|
||||||
|
|
||||||
assertThat(statement).isEqualTo(expected);
|
assertEquals(expected, statement);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSingleNewReleaseMovie() {
|
||||||
|
// Given
|
||||||
|
Customer customer = new Customer("Bob");
|
||||||
|
Movie mockMovie = Mockito.mock(Movie.class);
|
||||||
|
|
||||||
|
when(mockMovie.getTitle()).thenReturn("Mock Movie");
|
||||||
|
when(mockMovie.getPriceCode()).thenReturn(Movie.NEW_RELEASE);
|
||||||
|
when(mockMovie.calculatePrice(3)).thenReturn(9.0);
|
||||||
|
customer.addRental(new Rental(mockMovie, 3));
|
||||||
|
|
||||||
|
// When
|
||||||
|
String statement = customer.statement();
|
||||||
|
|
||||||
|
// Then
|
||||||
|
String expected = "" +
|
||||||
|
"Rental Record for Bob\n" +
|
||||||
|
"\tMock Movie\t9.0\n" +
|
||||||
|
"Amount owed is 9.0\n" +
|
||||||
|
"You earned 2 frequent renter points";
|
||||||
|
|
||||||
|
assertEquals(expected, statement);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSingleChildrenMovie() {
|
||||||
|
// Given
|
||||||
|
Customer customer = new Customer("Bob");
|
||||||
|
Movie mockMovie = Mockito.mock(Movie.class);
|
||||||
|
|
||||||
|
when(mockMovie.getTitle()).thenReturn("Mock Movie");
|
||||||
|
when(mockMovie.getPriceCode()).thenReturn(Movie.CHILDRENS);
|
||||||
|
when(mockMovie.calculatePrice(4)).thenReturn(3.0);
|
||||||
|
customer.addRental(new Rental(mockMovie, 4));
|
||||||
|
|
||||||
|
// When
|
||||||
|
String statement = customer.statement();
|
||||||
|
|
||||||
|
// Then
|
||||||
|
String expected = "" +
|
||||||
|
"Rental Record for Bob\n" +
|
||||||
|
"\tMock Movie\t3.0\n" +
|
||||||
|
"Amount owed is 3.0\n" +
|
||||||
|
"You earned 1 frequent renter points";
|
||||||
|
|
||||||
|
assertEquals(expected, statement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
81
src/test/java/movierental/MovieTest.java
Normal file
81
src/test/java/movierental/MovieTest.java
Normal file
|
|
@ -0,0 +1,81 @@
|
||||||
|
package movierental;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class MovieTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_return_2_if_movie_is_regular_no_overtime() {
|
||||||
|
// Given
|
||||||
|
Movie movie = new Movie("Mock Movie", Movie.REGULAR);
|
||||||
|
|
||||||
|
// When
|
||||||
|
double price = movie.calculatePrice(2);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertEquals(2.0, price);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_return_3_and_half_if_movie_is_regular_with_overtime() {
|
||||||
|
// Given
|
||||||
|
Movie movie = new Movie("Mock Movie", Movie.REGULAR);
|
||||||
|
|
||||||
|
// When
|
||||||
|
double price = movie.calculatePrice(3);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertEquals(3.5, price);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_return_3_if_movie_is_new_release_for_one_day() {
|
||||||
|
// Given
|
||||||
|
Movie movie = new Movie("Mock Movie", Movie.NEW_RELEASE);
|
||||||
|
|
||||||
|
// When
|
||||||
|
double price = movie.calculatePrice(1);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertEquals(3.0, price);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_return_6_if_movie_is_new_release_for_two_days() {
|
||||||
|
// Given
|
||||||
|
Movie movie = new Movie("Mock Movie", Movie.NEW_RELEASE);
|
||||||
|
|
||||||
|
// When
|
||||||
|
double price = movie.calculatePrice(2);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertEquals(6.0, price);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_return_1_5_if_movie_is_children_no_overtime() {
|
||||||
|
// Given
|
||||||
|
Movie movie = new Movie("Mock Movie", Movie.CHILDRENS);
|
||||||
|
|
||||||
|
// When
|
||||||
|
double price = movie.calculatePrice(3);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertEquals(1.5, price);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_return_3_if_movie_is_children_with_overtime() {
|
||||||
|
// Given
|
||||||
|
Movie movie = new Movie("Mock Movie", Movie.CHILDRENS);
|
||||||
|
|
||||||
|
// When
|
||||||
|
double price = movie.calculatePrice(4);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertEquals(3.0, price);
|
||||||
|
}
|
||||||
|
}
|
||||||
31
src/test/java/movierental/RentalTest.java
Normal file
31
src/test/java/movierental/RentalTest.java
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
package movierental;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.CsvSource;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class RentalTest {
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@CsvSource({
|
||||||
|
"2, 0, 2.0", // regular movie, no overtime
|
||||||
|
"3, 0, 3.5", // regular movie, with overtime
|
||||||
|
"1, 1, 3.0", // new release movie, one day
|
||||||
|
"2, 1, 6.0", // new release movie, two days
|
||||||
|
"3, 2, 1.5", // children movie, no overtime
|
||||||
|
"4, 2, 3.0" // children movie, with overtime
|
||||||
|
})
|
||||||
|
void should_return_correct_price_for_movie(int daysRented, int movie_type, double expected) {
|
||||||
|
// Given
|
||||||
|
Movie movie = new Movie("Mock Movie", movie_type);
|
||||||
|
Rental rental = new Rental(movie, daysRented);
|
||||||
|
|
||||||
|
// When
|
||||||
|
double price = rental.calculatePrice();
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertEquals(expected, price);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue