Utilisation du design pattern Strategy et ajout de tests unitaires
This commit is contained in:
parent
2a5a8b0a7c
commit
365875bef2
4 changed files with 76 additions and 23 deletions
|
|
@ -26,24 +26,7 @@ public class Customer {
|
|||
String result = "Rental Record for " + getName() + "\n";
|
||||
|
||||
for (Rental each : _rentals) {
|
||||
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;
|
||||
}
|
||||
double thisAmount = each.calculatePrice();
|
||||
|
||||
// add frequent renter points
|
||||
frequentRenterPoints++;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package movierental;
|
||||
|
||||
import movierental.MoviePriceCalculation.MoviePriceCalculationStrategy;
|
||||
|
||||
public class Movie {
|
||||
|
||||
public static final int CHILDRENS = 2;
|
||||
|
|
@ -8,10 +10,22 @@ public class Movie {
|
|||
|
||||
private String _title;
|
||||
private int _priceCode;
|
||||
private MoviePriceCalculationStrategy moviePriceCalculationStrategy;
|
||||
|
||||
public Movie(String title, int priceCode) {
|
||||
_title = title;
|
||||
_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() {
|
||||
|
|
@ -26,5 +40,8 @@ public class Movie {
|
|||
return _title;
|
||||
}
|
||||
|
||||
public double calculatePrice(int daysRented) {
|
||||
return moviePriceCalculationStrategy.calculatePrice(daysRented);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,4 +20,8 @@ public class Rental {
|
|||
public Movie getMovie() {
|
||||
return _movie;
|
||||
}
|
||||
|
||||
public double calculatePrice() {
|
||||
return _movie.calculatePrice(_daysRented);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,21 +1,22 @@
|
|||
package movierental;
|
||||
|
||||
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.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class CustomerTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
public void testSingleRegularMovie() {
|
||||
// Given
|
||||
Customer customer = new Customer("Bob");
|
||||
Movie mockMovie = mock(Movie.class);
|
||||
Movie mockMovie = Mockito.mock(Movie.class);
|
||||
|
||||
when(mockMovie.getTitle()).thenReturn("Mock Movie");
|
||||
when(mockMovie.getPriceCode()).thenReturn(Movie.REGULAR);
|
||||
when(mockMovie.calculatePrice(2)).thenReturn(2.0);
|
||||
customer.addRental(new Rental(mockMovie, 2));
|
||||
|
||||
// When
|
||||
|
|
@ -28,6 +29,54 @@ public class CustomerTest {
|
|||
"Amount owed is 2.0\n" +
|
||||
"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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue