Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Archivos del sistema macOS
.DS_Store

# Configuración de Visual Studio Code
.vscode/
/bin/
.classpath
.project
1 change: 1 addition & 0 deletions informe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><head><title>Rental Report</title></head><body><h1>Rental Record for Manuel</h1><ul><li>Sky Captain - 15.0</li><li>Accion Mutante - 2.0</li><li>Hermano Oso - 12.0</li></ul><p>Amount owed is 29.0</p><p>You earned 4 frequent renter points</p></body></html>
11 changes: 11 additions & 0 deletions src/ubu/gii/dass/refactoring/ChildrensPrice.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
91 changes: 51 additions & 40 deletions src/ubu/gii/dass/refactoring/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,45 +34,53 @@ public String getName() {
};

public String statement() {
double totalAmount = 0;
int frequentRenterPoints = 0;
Iterator<Rental> 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<Rental> rentals = _rentals.iterator();
String result = "Rental Record for " + getName() + "\n";
StringBuilder htmlContent = new StringBuilder();

htmlContent.append("<html><head><title>Rental Report</title></head><body>");
htmlContent.append("<h1>Rental Record for " + getName() + "</h1><ul>");

while (rentals.hasNext()) {
double thisAmount = 0;
Rental each = rentals.next();
// determine amounts for each line
thisAmount = each.getCharge();

// add frequent renter points
frequentRenterPoints = updatePoints(frequentRenterPoints, each);

// show figures for this rental
result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(thisAmount) + "\n";
totalAmount += thisAmount;

// add to HTML content
htmlContent.append("<li>" + each.getMovie().getTitle() + " - " + thisAmount + "</li>");
}

// add footer lines
result += "Amount owed is " + String.valueOf(totalAmount) + "\n";
result += "You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points";

// add to HTML content
htmlContent.append("</ul><p>Amount owed is " + totalAmount + "</p>");
htmlContent.append("<p>You earned " + frequentRenterPoints + " frequent renter points</p>");
htmlContent.append("</body></html>");

// 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);
}
}
31 changes: 25 additions & 6 deletions src/ubu/gii/dass/refactoring/Movie.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 13 additions & 0 deletions src/ubu/gii/dass/refactoring/NewReleasePrice.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
8 changes: 8 additions & 0 deletions src/ubu/gii/dass/refactoring/Price.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
11 changes: 11 additions & 0 deletions src/ubu/gii/dass/refactoring/RegularPrice.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
4 changes: 4 additions & 0 deletions src/ubu/gii/dass/refactoring/Rental.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ public Movie getMovie() {
return _movie;
}

public double getCharge() {
return _movie.getCharge(_daysRented);
}

}
31 changes: 31 additions & 0 deletions src/ubu/gii/dass/refactoring/VideoClubTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 <h1>.", content.contains("<h1>Rental Record for HTMLClient</h1>"));
assertTrue("Debe contener el título de la película dentro de <li>.", content.contains("<li>HTML Movie - 9.0</li>"));
assertTrue("Debe mostrar el total adeudado.", content.contains("<p>Amount owed is 9.0</p>"));
assertTrue("Debe mostrar puntos de cliente frecuente.", content.contains("frequent renter points"));

// Limpia el archivo generado
htmlFile.delete();
}

}