diff --git a/src/main/java/video/domain/Customer.java b/src/main/java/video/domain/Customer.java index fd4af2f..ee09a48 100644 --- a/src/main/java/video/domain/Customer.java +++ b/src/main/java/video/domain/Customer.java @@ -27,7 +27,7 @@ public RentalResult rentalMovies() { int totalAmount = 0; int frequentRenterPoints = 0; for (Rental each : rentals) { - int thisAmount = amount(each); + int thisAmount = each.amount(); // レンタルポイントを加算 frequentRenterPoints++; // 新作を二日以上借りた場合はボーナスポイント @@ -39,24 +39,4 @@ public RentalResult rentalMovies() { return new RentalResult(totalAmount, frequentRenterPoints); } - private int amount(Rental each) { - int thisAmount = 0; - // 金額を計算 - switch (each.getMovie().getPriceType()) { - case REGULAR: - thisAmount += 200; - if (each.getDaysRented() > 2) - thisAmount += (each.getDaysRented() - 2) * 150; - break; - case NEW_RELEASE: - thisAmount += each.getDaysRented() * 300; - break; - case CHILDREN: - thisAmount += 150; - if (each.getDaysRented() > 3) - thisAmount += (each.getDaysRented() - 3) * 150; - break; - } - return thisAmount; - } } \ No newline at end of file diff --git a/src/main/java/video/domain/Rental.java b/src/main/java/video/domain/Rental.java index 7e33b3f..796858a 100644 --- a/src/main/java/video/domain/Rental.java +++ b/src/main/java/video/domain/Rental.java @@ -16,4 +16,25 @@ public int getDaysRented() { public Movie getMovie() { return movie; } + + int amount() { + int thisAmount = 0; + // 金額を計算 + switch (getMovie().getPriceType()) { + case REGULAR: + thisAmount += 200; + if (getDaysRented() > 2) + thisAmount += (getDaysRented() - 2) * 150; + break; + case NEW_RELEASE: + thisAmount += getDaysRented() * 300; + break; + case CHILDREN: + thisAmount += 150; + if (getDaysRented() > 3) + thisAmount += (getDaysRented() - 3) * 150; + break; + } + return thisAmount; + } } \ No newline at end of file diff --git a/src/test/java/video/domain/RentalTest.java b/src/test/java/video/domain/RentalTest.java new file mode 100644 index 0000000..bf868bc --- /dev/null +++ b/src/test/java/video/domain/RentalTest.java @@ -0,0 +1,57 @@ +package video.domain; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class RentalTest { + + Movie 子供用作品_となりのトトロ = new Movie("となりのトトロ", PriceType.CHILDREN); + Movie 旧作_トップガン = new Movie("トップガン", PriceType.REGULAR); + Movie 新作_君たちはどう生きるか = new Movie("君たちはどう生きるか", PriceType.NEW_RELEASE); + @Test + void 新作を4日のレンタル料金() { + Rental sut = new Rental(新作_君たちはどう生きるか, 4); + + double result = sut.amount(); + + assertEquals(1200, result); + } + + + @Test + void 旧作を2日のレンタル料金() { + Rental sut = new Rental(旧作_トップガン, 2); + + double result = sut.amount(); + + assertEquals(200, result); + } + + @Test + void 旧作を3日のレンタル料金() { + Rental sut = new Rental(旧作_トップガン, 3); + + double result = sut.amount(); + + assertEquals(350, result); + } + + @Test + void 子供用作品を4日のレンタル料金() { + Rental sut = new Rental(子供用作品_となりのトトロ, 4); + + double result = sut.amount(); + + assertEquals(300, result); + } + + @Test + void 子供用作品を3日のレンタル料金() { + Rental sut = new Rental(子供用作品_となりのトトロ, 3); + + double result = sut.amount(); + + assertEquals(150, result); + } +} \ No newline at end of file