Compare commits
No commits in common. "a95bf5e7a2d63d88c9c37a66163e8527c6f90f9c" and "9137e7e13370f0e9ee4f0f27c90d13ac9483243f" have entirely different histories.
a95bf5e7a2
...
9137e7e133
16 changed files with 23 additions and 225 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
BIN
src/main/.DS_Store
vendored
Binary file not shown.
BIN
src/main/java/.DS_Store
vendored
BIN
src/main/java/.DS_Store
vendored
Binary file not shown.
|
|
@ -26,7 +26,24 @@ 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 = each.calculatePrice();
|
double thisAmount = 0;
|
||||||
|
|
||||||
|
//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,7 +1,5 @@
|
||||||
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;
|
||||||
|
|
@ -10,22 +8,10 @@ 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() {
|
||||||
|
|
@ -40,8 +26,5 @@ public class Movie {
|
||||||
return _title;
|
return _title;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double calculatePrice(int daysRented) {
|
|
||||||
return moviePriceCalculationStrategy.calculatePrice(daysRented);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,5 +0,0 @@
|
||||||
package movierental.MoviePriceCalculation;
|
|
||||||
|
|
||||||
public interface MoviePriceCalculationStrategy {
|
|
||||||
double calculatePrice(int daysRented);
|
|
||||||
}
|
|
||||||
|
|
@ -1,8 +0,0 @@
|
||||||
package movierental.MoviePriceCalculation;
|
|
||||||
|
|
||||||
public class NewReleaseMoviePriceCalculation implements MoviePriceCalculationStrategy {
|
|
||||||
@Override
|
|
||||||
public double calculatePrice(int daysRented) {
|
|
||||||
return daysRented * 3;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
||||||
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,8 +20,4 @@ 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
BIN
src/test/.DS_Store
vendored
Binary file not shown.
BIN
src/test/java/.DS_Store
vendored
BIN
src/test/java/.DS_Store
vendored
Binary file not shown.
|
|
@ -1,22 +1,21 @@
|
||||||
package movierental;
|
package movierental;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.Mockito;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.mockito.Mockito.mock;
|
||||||
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 testSingleRegularMovie() {
|
public void test() {
|
||||||
// Given
|
// Given
|
||||||
Customer customer = new Customer("Bob");
|
Customer customer = new Customer("Bob");
|
||||||
Movie mockMovie = Mockito.mock(Movie.class);
|
Movie mockMovie = 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
|
||||||
|
|
@ -29,54 +28,6 @@ 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";
|
||||||
|
|
||||||
assertEquals(expected, statement);
|
assertThat(statement).isEqualTo(expected);
|
||||||
}
|
|
||||||
|
|
||||||
@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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,81 +0,0 @@
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,31 +0,0 @@
|
||||||
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