Commit 7f0b02cd authored by Colin DAMON's avatar Colin DAMON

Merge branch '73-movie-rental-kata' into 'master'

Resolve "Movie rental kata"

Closes #73

See merge request !59
parents a013cda3 753ee91b
......@@ -24,3 +24,4 @@ include:
- local: "/h2g2/.gitlab-ci.yml"
- local: "/java-streams/.gitlab-ci.yml"
- local: "/game-of-life/.gitlab-ci.yml"
- local: "/movie-rental/.gitlab-ci.yml"
......@@ -49,3 +49,4 @@ Ce dépôt Git a pour but de partager les différents ateliers pouvant être ré
| [Java streams](/java-streams) | Code&coffee | Moyenne |
| [CodingGame Mars Landing Party](https://www.youtube.com/watch?v=FeJa3UnmeHw) | CodingGame | Moyenne |
| [Game of life](/game-of-life) | Kata | Enervé |
| [Movie rental](/movie-rental) | Kata | Moyenne |
package-movie-rental:
variables:
PROJECT_FOLDER: "movie-rental"
extends: .java
only:
refs:
- master
- merge_requests
changes:
- ".gitlab-common-ci.yml"
- "movie-rental/**/*"
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<version>1.0.0</version>
<groupId>fr.ippon.kata</groupId>
<artifactId>java-parent</artifactId>
<relativePath>../java-parent</relativePath>
</parent>
<version>1.0.0-SNAPSHOT</version>
<artifactId>movie-rental</artifactId>
<name>MovieRental</name>
<developers>
<developer>
<email>arey@ippon.fr</email>
<name>Anthony REY</name>
</developer>
<developer>
<email>cdamon@ippon.fr</email>
<name>Colin DAMON</name>
</developer>
</developers>
</project>
# Movie rental
Résolution du kata [Movie rental](https://gitlab.com/azae/craft/movie-rental)
- **Auteurs** : Anthony REY et Colin DAMON
- **Date** : 28/01/2021
- **Langage** : Java
- **Niveau** : Moyen
- **Replay** : TODO
package fr.ippon.rental;
public class ChildrensMovie extends Movie {
public ChildrensMovie(String title) {
super(title);
}
public double getAmount(int daysRented) {
if (daysRented > 3) {
return 1.5 + (daysRented - 3) * 1.5;
}
return 1.5;
}
}
package fr.ippon.rental;
public class ConsoleRenderer implements Renderer {
@Override
public String header(String name) {
return "Rental Record for " + name + "\n";
}
@Override
public String movie(Rental rental, double amount) {
return "\t" + rental.getMovie().getTitle() + "\t" + String.valueOf(amount) + "\n";
}
@Override
public String footer(double totalAmount, int frequentRenterPoints) {
return new StringBuilder()
.append("Amount owed is ")
.append(totalAmount)
.append("\n")
.append("You earned ")
.append(frequentRenterPoints)
.append(" frequent renter points")
.toString();
}
}
package fr.ippon.rental;
import java.util.ArrayList;
import java.util.List;
public class Customer {
private final String name;
private final List<Rental> rentals = new ArrayList<Rental>();
public Customer(String name) {
this.name = name;
}
public void addRental(Rental arg) {
rentals.add(arg);
}
public String getName() {
return name;
}
public String statement(Renderer renderer) {
StringBuilder result = new StringBuilder(renderer.header(name));
double totalAmount = 0;
for (Rental rental : rentals) {
double amount = calculateAmount(rental);
result.append(renderer.movie(rental, amount));
totalAmount += amount;
}
result.append(renderer.footer(totalAmount, calculateFrequentRenterPoints()));
return result.toString();
}
private int calculateFrequentRenterPoints() {
return rentals.stream().mapToInt(Rental::getRenterPoints).sum();
}
private double calculateAmount(Rental rental) {
return rental.amount();
}
}
package fr.ippon.rental;
public class HtmlRenderer implements Renderer {
@Override
public String header(String name) {
return "<h1>Rental Record for " + name + "</h1>\n<table>";
}
@Override
public String movie(Rental rental, double amount) {
return "<tr><td>" + rental.getMovie().getTitle() + "</td><td>" + String.valueOf(amount) + "</td></tr>\n";
}
@Override
public String footer(double totalAmount, int frequentRenterPoints) {
return new StringBuilder()
.append("</table><p>Amount owed is <em>")
.append(totalAmount)
.append("</em></p>\n")
.append("<p>You earned <em>")
.append(frequentRenterPoints)
.append("</em> frequent renter points</p>")
.toString();
}
}
package fr.ippon.rental;
public abstract class Movie {
private final String title;
public Movie(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public abstract double getAmount(int daysRented);
public boolean isNewRelease() {
return false;
}
}
package fr.ippon.rental;
public class Movies {
public static Movie regular(String title) {
return new RegularMovie(title);
}
public static Movie newRelease(String title) {
return new NewReleaseMovie(title);
}
public static Movie childrens(String title) {
return new ChildrensMovie(title);
}
}
package fr.ippon.rental;
public class NewReleaseMovie extends Movie {
public NewReleaseMovie(String title) {
super(title);
}
public double getAmount(int daysRented) {
return daysRented * 3;
}
@Override
public boolean isNewRelease() {
return true;
}
}
package fr.ippon.rental;
public class RegularMovie extends Movie {
public RegularMovie(String title) {
super(title);
}
public double getAmount(int daysRented) {
if (daysRented > 2) {
return 2 + (daysRented - 2) * 1.5;
}
return 2;
}
}
package fr.ippon.rental;
public interface Renderer {
String header(String name);
String movie(Rental rental, double amount);
String footer(double totalAmount, int frequentRenterPoints);
}
package fr.ippon.rental;
/**
* The rental class represents a customer renting a movie.
*/
public class Rental {
private final Movie movie;
private final int daysRented;
public Rental(Movie movie, int daysRented) {
this.movie = movie;
this.daysRented = daysRented;
}
public int getDaysRented() {
return daysRented;
}
public Movie getMovie() {
return movie;
}
public int getRenterPoints() {
if (movie.isNewRelease() && daysRented > 1) {
return 2;
}
return 1;
}
public double amount() {
return movie.getAmount(daysRented);
}
}
package fr.ippon.rental;
import static fr.ippon.rental.Movies.*;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class CustomerTest {
@Test
public void shouldRenderConsole() {
String expected =
"" +
"Rental Record for Bob\n" +
"\tJaws\t2.0\n" +
"\tGolden Eye\t3.5\n" +
"\tShort New\t3.0\n" +
"\tLong New\t6.0\n" +
"\tBambi\t1.5\n" +
"\tToy Story\t3.0\n" +
"Amount owed is 19.0\n" +
"You earned 7 frequent renter points";
assertThat(customer().statement(new ConsoleRenderer())).isEqualTo(expected);
}
@Test
void shouldRenderHtml() {
String expected =
"" +
"<h1>Rental Record for Bob</h1>\n" +
"<table>" +
"<tr><td>Jaws</td><td>2.0</td></tr>\n" +
"<tr><td>Golden Eye</td><td>3.5</td></tr>\n" +
"<tr><td>Short New</td><td>3.0</td></tr>\n" +
"<tr><td>Long New</td><td>6.0</td></tr>\n" +
"<tr><td>Bambi</td><td>1.5</td></tr>\n" +
"<tr><td>Toy Story</td><td>3.0</td></tr>\n" +
"</table>" +
"<p>Amount owed is <em>19.0</em></p>\n" +
"<p>You earned <em>7</em> frequent renter points</p>";
assertThat(customer().statement(new HtmlRenderer())).isEqualTo(expected);
}
private Customer customer() {
Customer customer = new Customer("Bob");
customer.addRental(new Rental(regular("Jaws"), 2));
customer.addRental(new Rental(regular("Golden Eye"), 3));
customer.addRental(new Rental(newRelease("Short New"), 1));
customer.addRental(new Rental(newRelease("Long New"), 2));
customer.addRental(new Rental(childrens("Bambi"), 3));
customer.addRental(new Rental(childrens("Toy Story"), 4));
return customer;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment