Compare commits

...

4 commits

Author SHA1 Message Date
a95bf5e7a2 Ajout de tests paramétrés sur Rental
All checks were successful
Gradle Test / test (push) Successful in 1m9s
2025-02-05 11:59:39 +01:00
73de7805c6 Ajout de tests unitaire pour Movie 2025-02-05 11:54:46 +01:00
365875bef2 Utilisation du design pattern Strategy et ajout de tests unitaires 2025-02-05 11:50:07 +01:00
2a5a8b0a7c Ajout des stratégies 2025-02-05 11:40:29 +01:00
16 changed files with 225 additions and 23 deletions

BIN
.DS_Store vendored

Binary file not shown.

BIN
src/.DS_Store vendored

Binary file not shown.

BIN
src/main/.DS_Store vendored Normal file

Binary file not shown.

BIN
src/main/java/.DS_Store vendored Normal file

Binary file not shown.

View file

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

View file

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

View file

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

View file

@ -0,0 +1,5 @@
package movierental.MoviePriceCalculation;
public interface MoviePriceCalculationStrategy {
double calculatePrice(int daysRented);
}

View file

@ -0,0 +1,8 @@
package movierental.MoviePriceCalculation;
public class NewReleaseMoviePriceCalculation implements MoviePriceCalculationStrategy {
@Override
public double calculatePrice(int daysRented) {
return daysRented * 3;
}
}

View file

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

View file

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

BIN
src/test/.DS_Store vendored Normal file

Binary file not shown.

BIN
src/test/java/.DS_Store vendored Normal file

Binary file not shown.

View file

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

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

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