Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 15 additions & 0 deletions index/ChildrensPrice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class ChildrensPrice extends Price {
@Override
public int getPriceCode() {
return Movie.CHILDRENS;
}

@Override
public double getCharge(int daysRented) {
double result = 1.5;
if (daysRented > 3) {
result += (daysRented - 3) * 1.5;
}
return result;
}
}
47 changes: 47 additions & 0 deletions index/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.util.ArrayList;
import java.util.List;

public class Customer {
private String _name;
private List<Rental> _rentals = new ArrayList<>();

public Customer(String name) {
_name = name;
}

public void addRental(Rental arg) {
_rentals.add(arg);
}

public String getName() {
return _name;
}

public String statement() {
return new TextStatement().value(this);
}

public String htmlStatement() {
return new HtmlStatement().value(this);
}

public List<Rental> getRentals() {
return _rentals;
}

public double getTotalCharge() {
double result = 0;
for (Rental each : _rentals) {
result += each.getCharge();
}
return result;
}

public int getTotalFrequentRenterPoints() {
int result = 0;
for (Rental each : _rentals) {
result += each.getFrequentRenterPoints();
}
return result;
}
}
17 changes: 17 additions & 0 deletions index/HtmlStatement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class HtmlStatement extends Statement {
@Override
protected String headerString(Customer customer) {
return "<H1>Rentals for <EM>" + customer.getName() + "</EM></H1><P>\n";
}

@Override
protected String rentalString(Rental rental) {
return rental.getMovie().getTitle() + ": " + rental.getCharge() + "<BR>\n";
}

@Override
protected String footerString(Customer customer) {
return "<P>You owe <EM>" + customer.getTotalCharge() + "</EM><P>\n" +
"On this rental you earned <EM>" + customer.getTotalFrequentRenterPoints() + "</EM> frequent renter points<P>";
}
}
26 changes: 26 additions & 0 deletions index/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class Main {
public static void main(String[] args) {
// Criando filmes
Movie movie1 = new Movie("The Godfather", Movie.REGULAR);
Movie movie2 = new Movie("Frozen", Movie.CHILDRENS);
Movie movie3 = new Movie("Avengers: Endgame", Movie.NEW_RELEASE);

// Criando aluguéis
Rental rental1 = new Rental(movie1, 5);
Rental rental2 = new Rental(movie2, 4);
Rental rental3 = new Rental(movie3, 2);

// Criando cliente
Customer customer = new Customer("John Doe");
customer.addRental(rental1);
customer.addRental(rental2);
customer.addRental(rental3);

// Exibindo os relatórios
System.out.println("Relatório em texto:");
System.out.println(customer.statement());

System.out.println("\nRelatório em HTML:");
System.out.println(customer.htmlStatement());
}
}
45 changes: 45 additions & 0 deletions index/Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
public class Movie {
public static final int CHILDRENS = 2;
public static final int REGULAR = 0;
public static final int NEW_RELEASE = 1;

private String _title;
private Price _price;

public Movie(String title, int priceCode) {
_title = title;
setPriceCode(priceCode);
}

public int getPriceCode() {
return _price.getPriceCode();
}

public void setPriceCode(int arg) {
switch (arg) {
case REGULAR:
_price = new RegularPrice();
break;
case CHILDRENS:
_price = new ChildrensPrice();
break;
case NEW_RELEASE:
_price = new NewReleasePrice();
break;
default:
throw new IllegalArgumentException("Incorrect Price Code");
}
}

public String getTitle() {
return _title;
}

public double getCharge(int daysRented) {
return _price.getCharge(daysRented);
}

public int getFrequentRenterPoints(int daysRented) {
return _price.getFrequentRenterPoints(daysRented);
}
}
16 changes: 16 additions & 0 deletions index/NewReleasePrice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class NewReleasePrice extends Price {
@Override
public int getPriceCode() {
return Movie.NEW_RELEASE;
}

@Override
public double getCharge(int daysRented) {
return daysRented * 3;
}

@Override
public int getFrequentRenterPoints(int daysRented) {
return (daysRented > 1) ? 2 : 1;
}
}
9 changes: 9 additions & 0 deletions index/Price.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public abstract class Price {
public abstract int getPriceCode();

public abstract double getCharge(int daysRented);

public int getFrequentRenterPoints(int daysRented) {
return 1;
}
}
15 changes: 15 additions & 0 deletions index/RegularPrice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class RegularPrice extends Price {
@Override
public int getPriceCode() {
return Movie.REGULAR;
}

@Override
public double getCharge(int daysRented) {
double result = 2;
if (daysRented > 2) {
result += (daysRented - 2) * 1.5;
}
return result;
}
}
25 changes: 25 additions & 0 deletions index/Rental.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public class Rental {
private Movie _movie;
private int _daysRented;

public Rental(Movie movie, int daysRented) {
_movie = movie;
_daysRented = daysRented;
}

public int getDaysRented() {
return _daysRented;
}

public Movie getMovie() {
return _movie;
}

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

public int getFrequentRenterPoints() {
return _movie.getFrequentRenterPoints(_daysRented);
}
}
16 changes: 16 additions & 0 deletions index/Statement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public abstract class Statement {
public String value(Customer customer) {
String result = headerString(customer);
for (Rental rental : customer.getRentals()) {
result += rentalString(rental);
}
result += footerString(customer);
return result;
}

protected abstract String headerString(Customer customer);

protected abstract String rentalString(Rental rental);

protected abstract String footerString(Customer customer);
}
17 changes: 17 additions & 0 deletions index/TextStatement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class TextStatement extends Statement {
@Override
protected String headerString(Customer customer) {
return "Rental Record for " + customer.getName() + "\n";
}

@Override
protected String rentalString(Rental rental) {
return "\t" + rental.getMovie().getTitle() + "\t" + rental.getCharge() + "\n";
}

@Override
protected String footerString(Customer customer) {
return "Amount owed is " + customer.getTotalCharge() + "\n" +
"You earned " + customer.getTotalFrequentRenterPoints() + " frequent renter points";
}
}