diff --git a/ChildrensPrice.java b/ChildrensPrice.java new file mode 100644 index 00000000..da8dd100 --- /dev/null +++ b/ChildrensPrice.java @@ -0,0 +1,15 @@ +public class ChildrensPrice extends Price { + + @Override + public int getPriceCode() { + return Movie.CHILDRENS; + } + + @Override + public double getCharge(int daysRented) { + double valor = 1.5; + if (daysRented > 3) + valor += (daysRented - 3) * 1.5; + return valor; + } +} diff --git a/Customer.java b/Customer.java new file mode 100644 index 00000000..bf140920 --- /dev/null +++ b/Customer.java @@ -0,0 +1,69 @@ +import java.util.Enumeration; +import java.util.Vector; + +public class Customer { + + private String nome; + private Vector alugueis = new Vector(); + + public Customer(String name) { + this.nome = name; + } + + public void addRental(Rental arg) { + alugueis.addElement(arg); + } + + public String getName() { + return nome; + } + + public String statement() { + Enumeration rentals = alugueis.elements(); + String result = "Registro de Aluguel para " + getName() + "\n"; + + while (rentals.hasMoreElements()) { + Rental each = (Rental) rentals.nextElement(); + result += "\t" + each.getMovie().getTitle() + "\t" + each.getCharge() + "\n"; + } + + result += "Total devido é " + getTotalCharge() + "\n"; + result += "Você ganhou " + getTotalFrequentRenterPoints() + " pontos de fidelidade"; + return result; + } + + public String htmlStatement() { + Enumeration rentals = alugueis.elements(); + String result = "

Aluguéis de " + getName() + "

\n"; + + while (rentals.hasMoreElements()) { + Rental each = (Rental) rentals.nextElement(); + result += each.getMovie().getTitle() + ": " + each.getCharge() + "
\n"; + } + + result += "

Você deve " + getTotalCharge() + "

\n"; + result += "Neste aluguel você ganhou " + getTotalFrequentRenterPoints() + + " pontos de fidelidade

"; + return result; + } + + private double getTotalCharge() { + double result = 0; + Enumeration rentals = alugueis.elements(); + while (rentals.hasMoreElements()) { + Rental each = (Rental) rentals.nextElement(); + result += each.getCharge(); + } + return result; + } + + private int getTotalFrequentRenterPoints() { + int result = 0; + Enumeration rentals = alugueis.elements(); + while (rentals.hasMoreElements()) { + Rental each = (Rental) rentals.nextElement(); + result += each.getFrequentRenterPoints(); + } + return result; + } +} diff --git a/Movie.java b/Movie.java new file mode 100644 index 00000000..7ce1dbff --- /dev/null +++ b/Movie.java @@ -0,0 +1,46 @@ +public class Movie { + + public static final int CHILDRENS = 2; + public static final int REGULAR = 0; + public static final int NEW_RELEASE = 1; + + private String titulo; + private Price preco; + + public Movie(String title, int priceCode) { + this.titulo = title; + setPriceCode(priceCode); + } + + public String getTitle() { + return titulo; + } + + public int getPriceCode() { + return preco.getPriceCode(); + } + + public void setPriceCode(int arg) { + switch (arg) { + case REGULAR: + preco = new RegularPrice(); + break; + case CHILDRENS: + preco = new ChildrensPrice(); + break; + case NEW_RELEASE: + preco = new NewReleasePrice(); + break; + default: + throw new IllegalArgumentException("Código de preço incorreto"); + } + } + + public double getCharge(int daysRented) { + return preco.getCharge(daysRented); + } + + public int getFrequentRenterPoints(int daysRented) { + return preco.getFrequentRenterPoints(daysRented); + } +} diff --git a/NewReleasePrice.java b/NewReleasePrice.java new file mode 100644 index 00000000..d9129d5f --- /dev/null +++ b/NewReleasePrice.java @@ -0,0 +1,17 @@ +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; + } +} diff --git a/Price.java b/Price.java new file mode 100644 index 00000000..2b5525d6 --- /dev/null +++ b/Price.java @@ -0,0 +1,8 @@ +public abstract class Price { + public abstract int getPriceCode(); + public abstract double getCharge(int daysRented); + + public int getFrequentRenterPoints(int daysRented) { + return 1; + } +} diff --git a/RegularPrice.java b/RegularPrice.java new file mode 100644 index 00000000..92daeae8 --- /dev/null +++ b/RegularPrice.java @@ -0,0 +1,15 @@ +public class RegularPrice extends Price { + + @Override + public int getPriceCode() { + return Movie.REGULAR; + } + + @Override + public double getCharge(int daysRented) { + double valor = 2; + if (daysRented > 2) + valor += (daysRented - 2) * 1.5; + return valor; + } +} diff --git a/Rental.java b/Rental.java new file mode 100644 index 00000000..f000d47f --- /dev/null +++ b/Rental.java @@ -0,0 +1,26 @@ +public class Rental { + + private Movie filme; + private int diasAlugados; + + public Rental(Movie movie, int daysRented) { + this.filme = movie; + this.diasAlugados = daysRented; + } + + public int getDaysRented() { + return diasAlugados; + } + + public Movie getMovie() { + return filme; + } + + public double getCharge() { + return filme.getCharge(diasAlugados); + } + + public int getFrequentRenterPoints() { + return filme.getFrequentRenterPoints(diasAlugados); + } +}