Utilisation du design pattern Strategy et ajout de tests unitaires

This commit is contained in:
Feror 2025-02-05 11:50:07 +01:00
parent 2a5a8b0a7c
commit 365875bef2
4 changed files with 76 additions and 23 deletions

View file

@ -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++;

View file

@ -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);
}
} }

View file

@ -20,4 +20,8 @@ public class Rental {
public Movie getMovie() { public Movie getMovie() {
return _movie; return _movie;
} }
public double calculatePrice() {
return _movie.calculatePrice(_daysRented);
}
} }

View file

@ -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);
} }
} }