diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..7ebf60e5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# Archivos del sistema macOS +.DS_Store + +# Configuración de Visual Studio Code +.vscode/ +/bin/ +.classpath +.project diff --git a/informe.html b/informe.html new file mode 100644 index 00000000..fd999096 --- /dev/null +++ b/informe.html @@ -0,0 +1 @@ +Rental Report

Rental Record for Manuel

Amount owed is 29.0

You earned 4 frequent renter points

\ No newline at end of file diff --git a/src/ubu/gii/dass/refactoring/ChildrensPrice.java b/src/ubu/gii/dass/refactoring/ChildrensPrice.java new file mode 100644 index 00000000..00f33582 --- /dev/null +++ b/src/ubu/gii/dass/refactoring/ChildrensPrice.java @@ -0,0 +1,11 @@ +package ubu.gii.dass.refactoring; + +public class ChildrensPrice extends Price { + @Override + public double getCharge(int daysRented) { + double result = 1.5; + if (daysRented > 3) + result += (daysRented - 3) * 1.5; + return result; + } +} diff --git a/src/ubu/gii/dass/refactoring/Customer.java b/src/ubu/gii/dass/refactoring/Customer.java index a5631232..6ef76782 100644 --- a/src/ubu/gii/dass/refactoring/Customer.java +++ b/src/ubu/gii/dass/refactoring/Customer.java @@ -11,6 +11,9 @@ * */ import java.util.*; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Iterator; public class Customer { private String _name; @@ -31,45 +34,53 @@ public String getName() { }; public String statement() { - double totalAmount = 0; - int frequentRenterPoints = 0; - Iterator rentals = _rentals.iterator(); - String result = "Rental Record for " + getName() + "\n"; - while (rentals.hasNext()) { - double thisAmount = 0; - Rental each = rentals.next(); - // 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 - frequentRenterPoints++; - // add bonus for a two day new release rental - if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) - && each.getDaysRented() > 1) - frequentRenterPoints++; - // show figures for this rental - result += "\t" + each.getMovie().getTitle() + "\t" - + String.valueOf(thisAmount) + "\n"; - totalAmount += thisAmount; - } - // add footer lines - result += "Amount owed is " + String.valueOf(totalAmount) + "\n"; - result += "You earned " + String.valueOf(frequentRenterPoints) - + " frequent renter points"; - return result; + double totalAmount = 0; + int frequentRenterPoints = 0; + Iterator rentals = _rentals.iterator(); + String result = "Rental Record for " + getName() + "\n"; + StringBuilder htmlContent = new StringBuilder(); + + htmlContent.append("Rental Report"); + htmlContent.append("

Rental Record for " + getName() + "

Amount owed is " + totalAmount + "

"); + htmlContent.append("

You earned " + frequentRenterPoints + " frequent renter points

"); + htmlContent.append(""); + + // Write the HTML content to a file + try (FileWriter writer = new FileWriter("informe.html")) { + writer.write(htmlContent.toString()); + } catch (IOException e) { + e.printStackTrace(); + } + + return result; + } + + + private int updatePoints(int frequentRenterPoints, Rental each) { + return each.getMovie().getUpdatePoints(frequentRenterPoints); } } diff --git a/src/ubu/gii/dass/refactoring/Movie.java b/src/ubu/gii/dass/refactoring/Movie.java index c5e768f2..a2161a8c 100644 --- a/src/ubu/gii/dass/refactoring/Movie.java +++ b/src/ubu/gii/dass/refactoring/Movie.java @@ -17,21 +17,40 @@ public class Movie { public static final int NEW_RELEASE = 1; private String _title; - private int _priceCode; + private Price _price; public Movie(String title, int priceCode) { _title = title; - _priceCode = priceCode; + setPriceCode(priceCode); } - public int getPriceCode() { - return _priceCode; + public Price getPriceCode() { + return _price; } - public void setPriceCode(int arg) { - _priceCode = arg; + public void setPriceCode(int priceCode) { + switch (priceCode) { + case REGULAR: + _price = new RegularPrice(); + break; + case NEW_RELEASE: + _price = new NewReleasePrice(); + break; + case CHILDRENS: + _price = new ChildrensPrice(); + break; + + } } + public Double getCharge(int daysRented) { + return _price.getCharge(daysRented); + } + + public int getUpdatePoints(int daysRented) { + return _price.getUpdatePoints(daysRented); + } + public String getTitle() { return _title; } diff --git a/src/ubu/gii/dass/refactoring/NewReleasePrice.java b/src/ubu/gii/dass/refactoring/NewReleasePrice.java new file mode 100644 index 00000000..fc0e9271 --- /dev/null +++ b/src/ubu/gii/dass/refactoring/NewReleasePrice.java @@ -0,0 +1,13 @@ +package ubu.gii.dass.refactoring; + +public class NewReleasePrice extends Price { + @Override + public double getCharge(int daysRented) { + return daysRented * 3; + } + + @Override + public int getFrequentRenterPoints(int daysRented) { + return (daysRented > 1) ? 2 : 1; + } +} diff --git a/src/ubu/gii/dass/refactoring/Price.java b/src/ubu/gii/dass/refactoring/Price.java new file mode 100644 index 00000000..f86612e7 --- /dev/null +++ b/src/ubu/gii/dass/refactoring/Price.java @@ -0,0 +1,8 @@ +package ubu.gii.dass.refactoring; + +public abstract class Price { + public abstract double getCharge(int daysRented); + public int getUpdatePoints(int daysRented) { + return 1; + } +} diff --git a/src/ubu/gii/dass/refactoring/RegularPrice.java b/src/ubu/gii/dass/refactoring/RegularPrice.java new file mode 100644 index 00000000..fd852730 --- /dev/null +++ b/src/ubu/gii/dass/refactoring/RegularPrice.java @@ -0,0 +1,11 @@ +package ubu.gii.dass.refactoring; + +public class RegularPrice extends Price { + @Override + public double getCharge(int daysRented) { + double result = 2; + if (daysRented > 2) + result += (daysRented - 2) * 1.5; + return result; + } +} diff --git a/src/ubu/gii/dass/refactoring/Rental.java b/src/ubu/gii/dass/refactoring/Rental.java index 265c5fca..e5dcbc0a 100644 --- a/src/ubu/gii/dass/refactoring/Rental.java +++ b/src/ubu/gii/dass/refactoring/Rental.java @@ -27,4 +27,8 @@ public Movie getMovie() { return _movie; } + public double getCharge() { + return _movie.getCharge(_daysRented); + } + } diff --git a/src/ubu/gii/dass/refactoring/VideoClubTest.java b/src/ubu/gii/dass/refactoring/VideoClubTest.java index 82205d33..03e6f1ec 100644 --- a/src/ubu/gii/dass/refactoring/VideoClubTest.java +++ b/src/ubu/gii/dass/refactoring/VideoClubTest.java @@ -2,6 +2,10 @@ import static org.junit.Assert.*; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -56,4 +60,31 @@ public void testAlquiler() { } + @Test + public void testHTMLStatementGeneration() throws IOException { + Customer customer = new Customer("HTMLClient"); + Movie movie = new Movie("HTML Movie", Movie.NEW_RELEASE); + Rental rental = new Rental(movie, 3); + customer.addRental(rental); + + // Llama a statement(), que genera el archivo HTML + customer.statement(); + + // Verifica que el archivo se ha creado + File htmlFile = new File("informe.html"); + assertTrue("El archivo HTML debería haberse creado.", htmlFile.exists()); + + // Lee el contenido del archivo HTML + String content = new String(Files.readAllBytes(htmlFile.toPath())); + + // Comprueba que el contenido esperado está presente + assertTrue("Debe contener etiqueta

.", content.contains("

Rental Record for HTMLClient

")); + assertTrue("Debe contener el título de la película dentro de
  • .", content.contains("
  • HTML Movie - 9.0
  • ")); + assertTrue("Debe mostrar el total adeudado.", content.contains("

    Amount owed is 9.0

    ")); + assertTrue("Debe mostrar puntos de cliente frecuente.", content.contains("frequent renter points")); + + // Limpia el archivo generado + htmlFile.delete(); + } + }