diff --git a/src/main/java/org/fundacionjala/coding/cynthia/Video/Children.java b/src/main/java/org/fundacionjala/coding/cynthia/Video/Children.java new file mode 100644 index 0000000..d98ec9e --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/cynthia/Video/Children.java @@ -0,0 +1,25 @@ +package org.fundacionjala.coding.cynthia.Video; + +/** + * movie class. + */ +public class Children extends Movie { + + /** + * movie constructor. + * + * @param title movie. + * @param priceCode movie. + */ + public Children(String title, int priceCode) { + super(title, priceCode); + } + + /** + * @return Cost of the days rented by all movie. + */ + @Override + public double costDaysRented(Rental rental) { + return rental.getDaysRented() > 2 ? ((rental.getDaysRented() - 3) * 1.5) + 1.5 : 1.5; + } +} diff --git a/src/main/java/org/fundacionjala/coding/cynthia/Video/Customer.java b/src/main/java/org/fundacionjala/coding/cynthia/Video/Customer.java new file mode 100644 index 0000000..f4974bb --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/cynthia/Video/Customer.java @@ -0,0 +1,83 @@ +package org.fundacionjala.coding.cynthia.Video; + +import java.util.ArrayList; +import java.util.List; + +/** + * costumer class. + */ +public class Customer { + private String customerName; + private List movieRentals; + private double totalAmount = 0; + private int frequentRenterPoints; + + /** + * constumer constructor. + * + * @param name of the client. + */ + public Customer(String name) { + customerName = name; + movieRentals = new ArrayList<>(); + } + + /** + * add a new rental fr the customer. + * + * @param arg rental that will added for the customer. + */ + public void addRental(Rental arg) { + movieRentals.add(arg); + } + + /** + * @return name of the customer. + */ + public String getName() { + return customerName; + } + + /** + * @return all movies rented. + */ + public String moviesRented() { + StringBuffer result = new StringBuffer(); + movieRentals.forEach(rental -> { + result.append("\t" + rental.getMovie().getTitle() + "\t" + String.valueOf(rental.costDaysRented()) + "\n"); + }); + return result.toString(); + } + + /** + * @return total cost for all movies rented. + */ + public double totalCostRented() { + movieRentals.forEach(rental -> { + totalAmount += rental.costDaysRented(); + }); + return totalAmount; + } + + /** + * @return return the bonus for all movies rented. + */ + public int totalBonusFrequencyRented() { + movieRentals.forEach(rental -> { + frequentRenterPoints += 1 + rental.bonusTwoDaysNewReleaseRental(); + }); + return frequentRenterPoints; + } + + /** + * @return all information about movies rented for the customer. + */ + public String statement() { + StringBuffer result = new StringBuffer(); + result.append("Rental Record for " + getName() + "\n"); + result.append(this.moviesRented()); + result.append("Amount owed is " + String.valueOf(this.totalCostRented()) + "\n"); + result.append("You earned " + String.valueOf(this.totalBonusFrequencyRented()) + " frequent renter points"); + return result.toString(); + } +} diff --git a/src/main/java/org/fundacionjala/coding/cynthia/Video/Movie.java b/src/main/java/org/fundacionjala/coding/cynthia/Video/Movie.java new file mode 100644 index 0000000..2ca4057 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/cynthia/Video/Movie.java @@ -0,0 +1,57 @@ +package org.fundacionjala.coding.cynthia.Video; + +/** + * movie class. + */ +abstract class Movie { + private static final int DEFAULT_BONUS_DAY_RENTED = 0; + private String titleMovie; + private int priceCodeMovie; + + /** + * movie constructor. + * @param title movie. + * @param priceCode movie. + */ + Movie(String title, int priceCode) { + titleMovie = title; + priceCodeMovie = priceCode; + } + + /** + * @return price code of the movie. + */ + public int getPriceCode() { + return priceCodeMovie; + } + + /** + * @param arg set the price code for the movie. + */ + public void setPriceCode(int arg) { + priceCodeMovie = arg; + } + + /** + * @return the title for the movie. + */ + public String getTitle() { + return titleMovie; + } + + /** + * + * @param rental to get the cost of days rented by movie. + * @return cost days by movies rented. + */ + public abstract double costDaysRented(Rental rental); + + /** + * + * @param rental movie. + * @return bonus by movie rented. + */ + public int bonusTwoDaysNewReleaseRental(Rental rental) { + return DEFAULT_BONUS_DAY_RENTED; + } +} diff --git a/src/main/java/org/fundacionjala/coding/cynthia/Video/Regular.java b/src/main/java/org/fundacionjala/coding/cynthia/Video/Regular.java new file mode 100644 index 0000000..adc89d9 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/cynthia/Video/Regular.java @@ -0,0 +1,27 @@ +package org.fundacionjala.coding.cynthia.Video; + +/** + * movie class. + */ +public class Regular extends Movie { + public static final int REGULAR = 0; + + /** + * movie constructor. + * + * @param title movie. + * @param priceCode movie. + */ + public Regular(String title, int priceCode) { + super(title, priceCode); + } + + /** + * @return Cost of the days rented by all movie. + */ + @Override + public double costDaysRented(Rental rental) { + return rental.getDaysRented() > 2 ? ((rental.getDaysRented() - 2) * 1.5) + 2 : 2; + } + +} diff --git a/src/main/java/org/fundacionjala/coding/cynthia/Video/Release.java b/src/main/java/org/fundacionjala/coding/cynthia/Video/Release.java new file mode 100644 index 0000000..ff278a7 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/cynthia/Video/Release.java @@ -0,0 +1,38 @@ +package org.fundacionjala.coding.cynthia.Video; + +/** + * movie class. + */ +public class Release extends Movie { + + /** + * movie constructor. + * + * @param title movie. + * @param priceCode movie. + */ + public Release(String title, int priceCode) { + super(title, priceCode); + } + + /** + * @return Cost of the days rented by all movie. + */ + @Override + public double costDaysRented(Rental rental) { + return rental.getDaysRented() * 3; + } + + /** + * + * @param rental movie. + * @return bonus by movie rented. + */ + @Override + public int bonusTwoDaysNewReleaseRental(Rental rental) { + if (rental.getDaysRented() > 1) { + return 1; + } + return 0; + } +} diff --git a/src/main/java/org/fundacionjala/coding/cynthia/Video/Rental.java b/src/main/java/org/fundacionjala/coding/cynthia/Video/Rental.java new file mode 100644 index 0000000..6936b1c --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/cynthia/Video/Rental.java @@ -0,0 +1,50 @@ +package org.fundacionjala.coding.cynthia.Video; + +/** + * Rental Class. + */ +public class Rental { + private Movie movie; + private int daysRented; + + /** + * Rental constructor. + * + * @param movie rental. + * @param daysRented rental. + */ + public Rental(Movie movie, int daysRented) { + this.movie = movie; + this.daysRented = daysRented; + } + + /** + * get days rented. + * + * @return number of days by movie rented. + */ + public int getDaysRented() { + return daysRented; + } + + /** + * @return movie rented. + */ + public Movie getMovie() { + return movie; + } + + /** + * @return Cost of the days rented by all movie. + */ + public double costDaysRented() { + return movie.costDaysRented(this); + } + + /** + * @return bonus for frequency. + */ + public int bonusTwoDaysNewReleaseRental() { + return movie.bonusTwoDaysNewReleaseRental(this); + } +} diff --git a/src/test/java/org/fundacionjala/coding/cynthia/VideoTest.java b/src/test/java/org/fundacionjala/coding/cynthia/VideoTest.java new file mode 100644 index 0000000..109b143 --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/cynthia/VideoTest.java @@ -0,0 +1,103 @@ +package org.fundacionjala.coding.cynthia; + +import org.fundacionjala.coding.cynthia.Video.Customer; +import org.fundacionjala.coding.cynthia.Video.Regular; +import org.fundacionjala.coding.cynthia.Video.Release; +import org.fundacionjala.coding.cynthia.Video.Children; +import org.fundacionjala.coding.cynthia.Video.Rental; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Created by USUARIO on 07/06/2017. + */ +public class VideoTest { + + /** + * verify the rental for one customer (RELESEA, CHILDREN). + */ + @Test + public void testVideoOnCustomer() { + Customer customer = new Customer("Test"); + customer.addRental(new Rental(new Release("The Revenant", 1), 2)); + customer.addRental(new Rental(new Regular("Terminator", 0), 2)); + assertEquals("Rental Record for Test\n" + + "\tThe Revenant\t6.0\n" + + "\tTerminator\t2.0\n" + + "Amount owed is 8.0\n" + + "You earned 3 frequent renter points", customer.statement()); + } + + /** + * verify just to one rented movie (RELEASE movie). + */ + @Test + public void testVideoOneVideoRented() { + Customer customer = new Customer("Juan"); + customer.addRental(new Rental(new Release("The Revenant", 1), 4)); + assertEquals("Rental Record for Juan\n" + + "\tThe Revenant\t12.0\n" + + "Amount owed is 12.0\n" + + "You earned 2 frequent renter points", customer.statement()); + } + + /** + * verify the rental whithout bonus. + */ + @Test + public void testVideoWithoutBonus() { + Customer customer = new Customer("Pedro"); + customer.addRental(new Rental(new Release("The Revenant", 1), 1)); + assertEquals("Rental Record for Pedro\n" + + "\tThe Revenant\t3.0\n" + + "Amount owed is 3.0\n" + + "You earned 1 frequent renter points", customer.statement()); + } + + /** + * verify the rental for several movies. + */ + @Test + public void testVideoSeveralMovies() { + Customer customer = new Customer("Oscar"); + customer.addRental(new Rental(new Regular("The Revenant", 0), 1)); + customer.addRental(new Rental(new Release("Cars", 1), 2)); + customer.addRental(new Rental(new Children("king kong", 2), 5)); + customer.addRental(new Rental(new Regular("Titanic", 0), 3)); + customer.addRental(new Rental(new Children("Galaxy", 2), 4)); + assertEquals("Rental Record for Oscar\n" + + "\tThe Revenant\t2.0\n" + + "\tCars\t6.0\n" + + "\tking kong\t4.5\n" + + "\tTitanic\t3.5\n" + + "\tGalaxy\t3.0\n" + + "Amount owed is 19.0\n" + + "You earned 6 frequent renter points", customer.statement()); + } + + /** + * verify rental for two customers. + */ + @Test + public void testVideoTwoCustomers() { + Customer marcan = new Customer("marcan"); + Customer agustin = new Customer("Agustin"); + marcan.addRental(new Rental(new Regular("The Revenant", 0), 1)); + marcan.addRental(new Rental(new Release("Cars", 1), 2)); + marcan.addRental(new Rental(new Children("king kong", 2), 5)); + agustin.addRental(new Rental(new Regular("Titanic", 0), 3)); + agustin.addRental(new Rental(new Children("Galaxy", 2), 4)); + assertEquals("Rental Record for marcan\n" + + "\tThe Revenant\t2.0\n" + + "\tCars\t6.0\n" + + "\tking kong\t4.5\n" + + "Amount owed is 12.5\n" + + "You earned 4 frequent renter points", marcan.statement()); + assertEquals("Rental Record for Agustin\n" + + "\tTitanic\t3.5\n" + + "\tGalaxy\t3.0\n" + + "Amount owed is 6.5\n" + + "You earned 2 frequent renter points", agustin.statement()); + } +}