From 0fe268eb4260dccecc808cd5aa6bc8a8c2be2fec Mon Sep 17 00:00:00 2001 From: giulliaVilanova Date: Mon, 25 Nov 2024 20:27:55 -0300 Subject: [PATCH 01/18] Commit 1 --- .idea/.gitignore | 8 ++++ .idea/AulaPraticaRefactoring.iml | 37 ++++++++++++++++ .idea/material_theme_project_new.xml | 12 ++++++ .idea/misc.xml | 6 +++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ Customer.java | 64 ++++++++++++++++++++++++++++ Movie.java | 26 +++++++++++ POM.xml | 54 +++++++++++++++++++++++ Rental.java | 18 ++++++++ 10 files changed, 239 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/AulaPraticaRefactoring.iml create mode 100644 .idea/material_theme_project_new.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 Customer.java create mode 100644 Movie.java create mode 100644 POM.xml create mode 100644 Rental.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..13566b81 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/AulaPraticaRefactoring.iml b/.idea/AulaPraticaRefactoring.iml new file mode 100644 index 00000000..75c06680 --- /dev/null +++ b/.idea/AulaPraticaRefactoring.iml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/material_theme_project_new.xml b/.idea/material_theme_project_new.xml new file mode 100644 index 00000000..f593bb3c --- /dev/null +++ b/.idea/material_theme_project_new.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..4444b225 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..e07d8d49 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Customer.java b/Customer.java new file mode 100644 index 00000000..a5e84e82 --- /dev/null +++ b/Customer.java @@ -0,0 +1,64 @@ +import java.util.Enumeration; +import java.util.Vector; + +public class Customer { + private String _name; + private Vector _rentals = new Vector(); + + public Customer (String name){ + _name = name; + } + + public void addRental(Rental arg) { + _rentals.addElement(arg); + } + + public String getName (){ + return _name; + } + + public String statement() { + double totalAmount = 0; + int frequentRenterPoints = 0; + Enumeration rentals = _rentals.elements(); + String result = "Rental Record for " + getName() + "\n"; + while (rentals.hasMoreElements()) { + double thisAmount = 0; + Rental each = (Rental) rentals.nextElement(); + + //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; + } +} \ No newline at end of file diff --git a/Movie.java b/Movie.java new file mode 100644 index 00000000..a7cd7826 --- /dev/null +++ b/Movie.java @@ -0,0 +1,26 @@ +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 int _priceCode; + + public Movie(String title, int priceCode) { + _title = title; + _priceCode = priceCode; + } + + public int getPriceCode() { + return _priceCode; + } + + public void setPriceCode(int arg) { + _priceCode = arg; + } + + public String getTitle (){ + return _title; + } +} \ No newline at end of file diff --git a/POM.xml b/POM.xml new file mode 100644 index 00000000..a23a97f7 --- /dev/null +++ b/POM.xml @@ -0,0 +1,54 @@ + + 4.0.0 + com.example + rental-system + 1.0-SNAPSHOT + + + 17 + 17 + 5.9.3 + + + + + org.junit.jupiter + junit-jupiter + ${junit.jupiter.version} + test + + + + org.slf4j + slf4j-api + 2.0.9 + + + org.slf4j + slf4j-simple + 2.0.9 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.10.1 + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.1.2 + + + + diff --git a/Rental.java b/Rental.java new file mode 100644 index 00000000..85f1f18d --- /dev/null +++ b/Rental.java @@ -0,0 +1,18 @@ +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; + } +} \ No newline at end of file From 7cffd968e83b5e91c1b83dd42825cd8af96ca327 Mon Sep 17 00:00:00 2001 From: giulliaVilanova Date: Mon, 25 Nov 2024 20:49:40 -0300 Subject: [PATCH 02/18] Commit 2 --- Customer.java | 77 +++++++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 33 deletions(-) diff --git a/Customer.java b/Customer.java index a5e84e82..c1e95a69 100644 --- a/Customer.java +++ b/Customer.java @@ -5,7 +5,7 @@ public class Customer { private String _name; private Vector _rentals = new Vector(); - public Customer (String name){ + public Customer(String name) { _name = name; } @@ -13,7 +13,7 @@ public void addRental(Rental arg) { _rentals.addElement(arg); } - public String getName (){ + public String getName() { return _name; } @@ -22,43 +22,54 @@ public String statement() { int frequentRenterPoints = 0; Enumeration rentals = _rentals.elements(); String result = "Rental Record for " + getName() + "\n"; + while (rentals.hasMoreElements()) { - double thisAmount = 0; Rental each = (Rental) rentals.nextElement(); - //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; - } + // Refatorado para usar amountFor + double thisAmount = amountFor(each); - // 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 ++; + // Add frequent renter points + frequentRenterPoints++; + 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"; + // Show figures for this rental + result += "\t" + each.getMovie().getTitle() + "\t" + thisAmount + "\n"; totalAmount += thisAmount; - } - //add footer lines - result += "Amount owed is " + String.valueOf(totalAmount) + "\n"; - result += "You earned " + String.valueOf(frequentRenterPoints) + - " frequent renter points"; + + // Add footer lines + result += "Amount owed is " + totalAmount + "\n"; + result += "You earned " + frequentRenterPoints + " frequent renter points"; return result; } -} \ No newline at end of file + + // Método extraído + private double amountFor(Rental each) { + double thisAmount = 0; + + 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; + } + + return thisAmount; + } +} From 2a2d6ede8a978b4b9361616c747c938699a40608 Mon Sep 17 00:00:00 2001 From: giulliaVilanova Date: Mon, 25 Nov 2024 20:51:07 -0300 Subject: [PATCH 03/18] Commit 3 --- Customer.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Customer.java b/Customer.java index c1e95a69..d28dba65 100644 --- a/Customer.java +++ b/Customer.java @@ -47,25 +47,25 @@ public String statement() { } // Método extraído - private double amountFor(Rental each) { + private double amountFor(Rental aRental) { double thisAmount = 0; - switch (each.getMovie().getPriceCode()) { + switch (aRental.getMovie().getPriceCode()) { case Movie.REGULAR: thisAmount += 2; - if (each.getDaysRented() > 2) { - thisAmount += (each.getDaysRented() - 2) * 1.5; + if (aRental.getDaysRented() > 2) { + thisAmount += (aRental.getDaysRented() - 2) * 1.5; } break; case Movie.NEW_RELEASE: - thisAmount += each.getDaysRented() * 3; + thisAmount += aRental.getDaysRented() * 3; break; case Movie.CHILDRENS: thisAmount += 1.5; - if (each.getDaysRented() > 3) { - thisAmount += (each.getDaysRented() - 3) * 1.5; + if (aRental.getDaysRented() > 3) { + thisAmount += (aRental.getDaysRented() - 3) * 1.5; } break; } From e1af2eaa954a5ed160b38d06b2bcf034b8cde48b Mon Sep 17 00:00:00 2001 From: giulliaVilanova Date: Mon, 25 Nov 2024 20:57:02 -0300 Subject: [PATCH 04/18] Commit 4 --- Customer.java | 31 ++----------------------------- Rental.java | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/Customer.java b/Customer.java index d28dba65..23cd2b6b 100644 --- a/Customer.java +++ b/Customer.java @@ -26,8 +26,8 @@ public String statement() { while (rentals.hasMoreElements()) { Rental each = (Rental) rentals.nextElement(); - // Refatorado para usar amountFor - double thisAmount = amountFor(each); + // Atualizado para usar getCharge + double thisAmount = each.getCharge(); // Add frequent renter points frequentRenterPoints++; @@ -45,31 +45,4 @@ public String statement() { result += "You earned " + frequentRenterPoints + " frequent renter points"; return result; } - - // Método extraído - private double amountFor(Rental aRental) { - double thisAmount = 0; - - switch (aRental.getMovie().getPriceCode()) { - case Movie.REGULAR: - thisAmount += 2; - if (aRental.getDaysRented() > 2) { - thisAmount += (aRental.getDaysRented() - 2) * 1.5; - } - break; - - case Movie.NEW_RELEASE: - thisAmount += aRental.getDaysRented() * 3; - break; - - case Movie.CHILDRENS: - thisAmount += 1.5; - if (aRental.getDaysRented() > 3) { - thisAmount += (aRental.getDaysRented() - 3) * 1.5; - } - break; - } - - return thisAmount; - } } diff --git a/Rental.java b/Rental.java index 85f1f18d..29cd88ce 100644 --- a/Rental.java +++ b/Rental.java @@ -15,4 +15,30 @@ public int getDaysRented() { public Movie getMovie() { return _movie; } + + public double getCharge() { + double thisAmount = 0; + + switch (_movie.getPriceCode()) { + case Movie.REGULAR: + thisAmount += 2; + if (_daysRented > 2) { + thisAmount += (_daysRented - 2) * 1.5; + } + break; + + case Movie.NEW_RELEASE: + thisAmount += _daysRented * 3; + break; + + case Movie.CHILDRENS: + thisAmount += 1.5; + if (_daysRented > 3) { + thisAmount += (_daysRented - 3) * 1.5; + } + break; + } + + return thisAmount; + } } \ No newline at end of file From e386c97e68abfd29061da01a5510952f2b054005 Mon Sep 17 00:00:00 2001 From: giulliaVilanova Date: Mon, 25 Nov 2024 21:01:19 -0300 Subject: [PATCH 05/18] Commit 5 --- Customer.java | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/Customer.java b/Customer.java index 23cd2b6b..5471d844 100644 --- a/Customer.java +++ b/Customer.java @@ -26,23 +26,22 @@ public String statement() { while (rentals.hasMoreElements()) { Rental each = (Rental) rentals.nextElement(); - // Atualizado para usar getCharge - double thisAmount = each.getCharge(); - - // Add frequent renter points - frequentRenterPoints++; - if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1) { - frequentRenterPoints++; - } - - // Show figures for this rental - result += "\t" + each.getMovie().getTitle() + "\t" + thisAmount + "\n"; - totalAmount += thisAmount; + // 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 + (each.getCharge()) + "\n"; + totalAmount += each.getCharge(); } - // Add footer lines - result += "Amount owed is " + totalAmount + "\n"; - result += "You earned " + frequentRenterPoints + " frequent renter points"; + // add footer lines + result += "Amount owed is " + String.valueOf(totalAmount) + "\n"; + result += "You earned " + String.valueOf(frequentRenterPoints) + + " frequent renter points"; return result; } } From 82d96b785237c526c83c479a617d1f55dc761bbf Mon Sep 17 00:00:00 2001 From: giulliaVilanova Date: Tue, 26 Nov 2024 21:29:51 -0300 Subject: [PATCH 06/18] Commit 6 --- Customer.java | 15 +++++---------- Rental.java | 12 +++++++++++- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/Customer.java b/Customer.java index 5471d844..a70222d5 100644 --- a/Customer.java +++ b/Customer.java @@ -26,22 +26,17 @@ public String statement() { while (rentals.hasMoreElements()) { Rental each = (Rental) rentals.nextElement(); - // 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 ++; + // Atualizado para usar getFrequentRenterPoints() + frequentRenterPoints += each.getFrequentRenterPoints(); // show figures for this rental - result += "\t" + each.getMovie().getTitle()+ "\t" + String.valueOf - (each.getCharge()) + "\n"; + result += "\t" + each.getMovie().getTitle() + "\t" + each.getCharge() + "\n"; totalAmount += each.getCharge(); } // add footer lines - result += "Amount owed is " + String.valueOf(totalAmount) + "\n"; - result += "You earned " + String.valueOf(frequentRenterPoints) - + " frequent renter points"; + result += "Amount owed is " + totalAmount + "\n"; + result += "You earned " + frequentRenterPoints + " frequent renter points"; return result; } } diff --git a/Rental.java b/Rental.java index 29cd88ce..79b5110d 100644 --- a/Rental.java +++ b/Rental.java @@ -41,4 +41,14 @@ public double getCharge() { return thisAmount; } -} \ No newline at end of file + + // Novo método getFrequentRenterPoints() + public int getFrequentRenterPoints() { + // Se for um novo lançamento e alugado por mais de 1 dia, retorna 2 + if (_movie.getPriceCode() == Movie.NEW_RELEASE && _daysRented > 1) { + return 2; + } + // Caso contrário, retorna 1 + return 1; + } +} From 1eca4a3f172d2ba23a617d60c0ff98820f1c3628 Mon Sep 17 00:00:00 2001 From: giulliaVilanova Date: Tue, 26 Nov 2024 21:32:40 -0300 Subject: [PATCH 07/18] Commit 7 --- Customer.java | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/Customer.java b/Customer.java index a70222d5..0387e17f 100644 --- a/Customer.java +++ b/Customer.java @@ -18,25 +18,42 @@ public String getName() { } public String statement() { - double totalAmount = 0; - int frequentRenterPoints = 0; Enumeration rentals = _rentals.elements(); String result = "Rental Record for " + getName() + "\n"; while (rentals.hasMoreElements()) { Rental each = (Rental) rentals.nextElement(); - // Atualizado para usar getFrequentRenterPoints() - frequentRenterPoints += each.getFrequentRenterPoints(); + // Mostrar os detalhes de cada aluguel + result += "\t" + each.getMovie().getTitle() + "\t" + + each.getCharge() + "\n"; + } + + // Adicionar o total e os pontos ao rodapé + result += "Amount owed is " + getTotalCharge() + "\n"; + result += "You earned " + getTotalFrequentRenterPoints() + " frequent renter points"; + return result; + } - // show figures for this rental - result += "\t" + each.getMovie().getTitle() + "\t" + each.getCharge() + "\n"; - totalAmount += each.getCharge(); + // Novo método para calcular o valor total dos aluguéis + private double getTotalCharge() { + double result = 0; + Enumeration rentals = _rentals.elements(); + while (rentals.hasMoreElements()) { + Rental each = (Rental) rentals.nextElement(); + result += each.getCharge(); } + return result; + } - // add footer lines - result += "Amount owed is " + totalAmount + "\n"; - result += "You earned " + frequentRenterPoints + " frequent renter points"; + // Novo método para calcular os pontos totais + private int getTotalFrequentRenterPoints() { + int result = 0; + Enumeration rentals = _rentals.elements(); + while (rentals.hasMoreElements()) { + Rental each = (Rental) rentals.nextElement(); + result += each.getFrequentRenterPoints(); + } return result; } } From 457024d3996b14d909f9346db7b4aa64825d5cf4 Mon Sep 17 00:00:00 2001 From: giulliaVilanova Date: Tue, 26 Nov 2024 21:36:16 -0300 Subject: [PATCH 08/18] Commit 8 --- Customer.java | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/Customer.java b/Customer.java index 0387e17f..0bb57809 100644 --- a/Customer.java +++ b/Customer.java @@ -35,7 +35,26 @@ public String statement() { return result; } - // Novo método para calcular o valor total dos aluguéis + // Novo método para gerar o comprovante em HTML + public String htmlStatement() { + Enumeration rentals = _rentals.elements(); + String result = "

Rentals for " + getName() + "

\n"; + + while (rentals.hasMoreElements()) { + Rental each = (Rental) rentals.nextElement(); + + // Adicionar os detalhes de cada aluguel + result += each.getMovie().getTitle() + ": " + each.getCharge() + "
\n"; + } + + // Adicionar o total e os pontos ao rodapé + result += "

You owe " + getTotalCharge() + "

\n"; + result += "On this rental you earned " + + getTotalFrequentRenterPoints() + + " frequent renter points

"; + return result; + } + private double getTotalCharge() { double result = 0; Enumeration rentals = _rentals.elements(); @@ -46,7 +65,6 @@ private double getTotalCharge() { return result; } - // Novo método para calcular os pontos totais private int getTotalFrequentRenterPoints() { int result = 0; Enumeration rentals = _rentals.elements(); From 5f69f71b8255195c0f348f161a4ec30b39c35890 Mon Sep 17 00:00:00 2001 From: giulliaVilanova Date: Tue, 26 Nov 2024 21:37:51 -0300 Subject: [PATCH 09/18] Commit 9 --- Movie.java | 36 ++++++++++++++++++++++++++++++------ Rental.java | 31 +++---------------------------- 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/Movie.java b/Movie.java index a7cd7826..b4282cb5 100644 --- a/Movie.java +++ b/Movie.java @@ -1,8 +1,7 @@ public class Movie { - - public static final int CHILDRENS = 2; - public static final int REGULAR = 0; - public static final int NEW_RELEASE = 1; + public static final int CHILDRENS = 2; + public static final int REGULAR = 0; + public static final int NEW_RELEASE = 1; private String _title; private int _priceCode; @@ -20,7 +19,32 @@ public void setPriceCode(int arg) { _priceCode = arg; } - public String getTitle (){ + public String getTitle() { return _title; } -} \ No newline at end of file + + public double getCharge(int daysRented) { + double thisAmount = 0; + + switch (getPriceCode()) { + case REGULAR: + thisAmount += 2; + if (daysRented > 2) { + thisAmount += (daysRented - 2) * 1.5; + } + break; + + case NEW_RELEASE: + thisAmount += daysRented * 3; + break; + + case CHILDRENS: + thisAmount += 1.5; + if (daysRented > 3) { + thisAmount += (daysRented - 3) * 1.5; + } + break; + } + return thisAmount; + } +} diff --git a/Rental.java b/Rental.java index 79b5110d..a49a5fde 100644 --- a/Rental.java +++ b/Rental.java @@ -1,5 +1,4 @@ public class Rental { - private Movie _movie; private int _daysRented; @@ -17,38 +16,14 @@ public Movie getMovie() { } public double getCharge() { - double thisAmount = 0; - - switch (_movie.getPriceCode()) { - case Movie.REGULAR: - thisAmount += 2; - if (_daysRented > 2) { - thisAmount += (_daysRented - 2) * 1.5; - } - break; - - case Movie.NEW_RELEASE: - thisAmount += _daysRented * 3; - break; - - case Movie.CHILDRENS: - thisAmount += 1.5; - if (_daysRented > 3) { - thisAmount += (_daysRented - 3) * 1.5; - } - break; - } - - return thisAmount; + // Delegar cálculo para a classe Movie + return _movie.getCharge(_daysRented); } - // Novo método getFrequentRenterPoints() public int getFrequentRenterPoints() { - // Se for um novo lançamento e alugado por mais de 1 dia, retorna 2 - if (_movie.getPriceCode() == Movie.NEW_RELEASE && _daysRented > 1) { + if ((getMovie().getPriceCode() == Movie.NEW_RELEASE) && getDaysRented() > 1) { return 2; } - // Caso contrário, retorna 1 return 1; } } From 396bc438e7995834275e34514993645361aaeace Mon Sep 17 00:00:00 2001 From: giulliaVilanova Date: Tue, 26 Nov 2024 21:39:08 -0300 Subject: [PATCH 10/18] Commit 10 --- Movie.java | 8 ++++++++ Rental.java | 7 ++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Movie.java b/Movie.java index b4282cb5..1e61b274 100644 --- a/Movie.java +++ b/Movie.java @@ -47,4 +47,12 @@ public double getCharge(int daysRented) { } return thisAmount; } + + public int getFrequentRenterPoints(int daysRented) { + // Lógica transferida de Rental + if (getPriceCode() == NEW_RELEASE && daysRented > 1) { + return 2; + } + return 1; + } } diff --git a/Rental.java b/Rental.java index a49a5fde..e51a4e70 100644 --- a/Rental.java +++ b/Rental.java @@ -16,14 +16,11 @@ public Movie getMovie() { } public double getCharge() { - // Delegar cálculo para a classe Movie return _movie.getCharge(_daysRented); } public int getFrequentRenterPoints() { - if ((getMovie().getPriceCode() == Movie.NEW_RELEASE) && getDaysRented() > 1) { - return 2; - } - return 1; + // Delegar cálculo para a classe Movie + return _movie.getFrequentRenterPoints(_daysRented); } } From 549d87b16d9c7a504a40c6a0d0e858bd156276ba Mon Sep 17 00:00:00 2001 From: giulliaVilanova Date: Tue, 26 Nov 2024 21:42:31 -0300 Subject: [PATCH 11/18] Commit 11 --- ChildrensPrice.java | 6 ++++++ Movie.java | 47 ++++++++++++-------------------------------- NewReleasePrice.java | 6 ++++++ Price.java | 3 +++ RegularPrice.java | 6 ++++++ 5 files changed, 34 insertions(+), 34 deletions(-) create mode 100644 ChildrensPrice.java create mode 100644 NewReleasePrice.java create mode 100644 Price.java create mode 100644 RegularPrice.java diff --git a/ChildrensPrice.java b/ChildrensPrice.java new file mode 100644 index 00000000..0c9c96c7 --- /dev/null +++ b/ChildrensPrice.java @@ -0,0 +1,6 @@ +public class ChildrensPrice extends Price { + @Override + public int getPriceCode() { + return Movie.CHILDRENS; + } +} diff --git a/Movie.java b/Movie.java index 1e61b274..7c51e5eb 100644 --- a/Movie.java +++ b/Movie.java @@ -4,55 +4,34 @@ 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; + return _price.getPriceCode(); } public void setPriceCode(int arg) { - _priceCode = arg; - } - - public String getTitle() { - return _title; - } - - public double getCharge(int daysRented) { - double thisAmount = 0; - - switch (getPriceCode()) { + switch (arg) { case REGULAR: - thisAmount += 2; - if (daysRented > 2) { - thisAmount += (daysRented - 2) * 1.5; - } - break; - - case NEW_RELEASE: - thisAmount += daysRented * 3; + _price = new RegularPrice(); break; - case CHILDRENS: - thisAmount += 1.5; - if (daysRented > 3) { - thisAmount += (daysRented - 3) * 1.5; - } + _price = new ChildrensPrice(); + break; + case NEW_RELEASE: + _price = new NewReleasePrice(); break; + default: + throw new IllegalArgumentException("Incorrect Price Code"); } - return thisAmount; } - public int getFrequentRenterPoints(int daysRented) { - // Lógica transferida de Rental - if (getPriceCode() == NEW_RELEASE && daysRented > 1) { - return 2; - } - return 1; + public String getTitle() { + return _title; } } diff --git a/NewReleasePrice.java b/NewReleasePrice.java new file mode 100644 index 00000000..e8f832eb --- /dev/null +++ b/NewReleasePrice.java @@ -0,0 +1,6 @@ +public class NewReleasePrice extends Price { + @Override + public int getPriceCode() { + return Movie.NEW_RELEASE; + } +} diff --git a/Price.java b/Price.java new file mode 100644 index 00000000..e63a0774 --- /dev/null +++ b/Price.java @@ -0,0 +1,3 @@ +public abstract class Price { + public abstract int getPriceCode(); +} diff --git a/RegularPrice.java b/RegularPrice.java new file mode 100644 index 00000000..06457b2e --- /dev/null +++ b/RegularPrice.java @@ -0,0 +1,6 @@ +public class RegularPrice extends Price { + @Override + public int getPriceCode() { + return Movie.REGULAR; + } +} From 571b2f7f79dc5185c3ffa027c4d1d836ee65f430 Mon Sep 17 00:00:00 2001 From: giulliaVilanova Date: Tue, 26 Nov 2024 21:44:38 -0300 Subject: [PATCH 12/18] Commit 12 --- Movie.java | 4 ++++ Price.java | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/Movie.java b/Movie.java index 7c51e5eb..9ca6678c 100644 --- a/Movie.java +++ b/Movie.java @@ -34,4 +34,8 @@ public void setPriceCode(int arg) { public String getTitle() { return _title; } + + public double getCharge(int daysRented) { + return _price.getCharge(daysRented); // Chamada ao método movido. + } } diff --git a/Price.java b/Price.java index e63a0774..62202fd1 100644 --- a/Price.java +++ b/Price.java @@ -1,3 +1,25 @@ public abstract class Price { public abstract int getPriceCode(); + + public double getCharge(int daysRented) { + // Código movido de `Movie` para cá. + switch (getPriceCode()) { + case Movie.REGULAR: + double amount = 2; + if (daysRented > 2) { + amount += (daysRented - 2) * 1.5; + } + return amount; + case Movie.NEW_RELEASE: + return daysRented * 3; + case Movie.CHILDRENS: + double childrenAmount = 1.5; + if (daysRented > 3) { + childrenAmount += (daysRented - 3) * 1.5; + } + return childrenAmount; + default: + throw new IllegalArgumentException("Invalid Price Code"); + } + } } From cc942c4f4fb54ae4718fdc6e1eed82e670614196 Mon Sep 17 00:00:00 2001 From: giulliaVilanova Date: Tue, 26 Nov 2024 21:48:27 -0300 Subject: [PATCH 13/18] Commit 13 --- ChildrensPrice.java | 9 +++++++++ Movie.java | 2 +- NewReleasePrice.java | 5 +++++ Price.java | 23 +---------------------- RegularPrice.java | 9 +++++++++ 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/ChildrensPrice.java b/ChildrensPrice.java index 0c9c96c7..c1cf552e 100644 --- a/ChildrensPrice.java +++ b/ChildrensPrice.java @@ -3,4 +3,13 @@ public class ChildrensPrice extends Price { 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; + } } diff --git a/Movie.java b/Movie.java index 9ca6678c..a5937b50 100644 --- a/Movie.java +++ b/Movie.java @@ -36,6 +36,6 @@ public String getTitle() { } public double getCharge(int daysRented) { - return _price.getCharge(daysRented); // Chamada ao método movido. + return _price.getCharge(daysRented); // Delega para o método abstrato. } } diff --git a/NewReleasePrice.java b/NewReleasePrice.java index e8f832eb..f66f9657 100644 --- a/NewReleasePrice.java +++ b/NewReleasePrice.java @@ -3,4 +3,9 @@ public class NewReleasePrice extends Price { public int getPriceCode() { return Movie.NEW_RELEASE; } + + @Override + public double getCharge(int daysRented) { + return daysRented * 3; + } } diff --git a/Price.java b/Price.java index 62202fd1..0c77ca35 100644 --- a/Price.java +++ b/Price.java @@ -1,25 +1,4 @@ public abstract class Price { public abstract int getPriceCode(); - - public double getCharge(int daysRented) { - // Código movido de `Movie` para cá. - switch (getPriceCode()) { - case Movie.REGULAR: - double amount = 2; - if (daysRented > 2) { - amount += (daysRented - 2) * 1.5; - } - return amount; - case Movie.NEW_RELEASE: - return daysRented * 3; - case Movie.CHILDRENS: - double childrenAmount = 1.5; - if (daysRented > 3) { - childrenAmount += (daysRented - 3) * 1.5; - } - return childrenAmount; - default: - throw new IllegalArgumentException("Invalid Price Code"); - } - } + public abstract double getCharge(int daysRented); // Método abstrato. } diff --git a/RegularPrice.java b/RegularPrice.java index 06457b2e..96612bd3 100644 --- a/RegularPrice.java +++ b/RegularPrice.java @@ -3,4 +3,13 @@ public class RegularPrice extends Price { 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; + } } From 4e4534f9c7db111a6ae04a0d08e9de78a5df9360 Mon Sep 17 00:00:00 2001 From: giulliaVilanova Date: Tue, 26 Nov 2024 21:49:56 -0300 Subject: [PATCH 14/18] Commit 14 --- Movie.java | 6 +++++- NewReleasePrice.java | 6 ++++++ Price.java | 7 ++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Movie.java b/Movie.java index a5937b50..dd2384df 100644 --- a/Movie.java +++ b/Movie.java @@ -36,6 +36,10 @@ public String getTitle() { } public double getCharge(int daysRented) { - return _price.getCharge(daysRented); // Delega para o método abstrato. + return _price.getCharge(daysRented); // Delegando à classe `Price`. + } + + public int getFrequentRenterPoints(int daysRented) { + return _price.getFrequentRenterPoints(daysRented); // Nova delegação. } } diff --git a/NewReleasePrice.java b/NewReleasePrice.java index f66f9657..b6a0cb7b 100644 --- a/NewReleasePrice.java +++ b/NewReleasePrice.java @@ -8,4 +8,10 @@ public int getPriceCode() { public double getCharge(int daysRented) { return daysRented * 3; } + + @Override + public int getFrequentRenterPoints(int daysRented) { + // Filmes de lançamento recente dão 2 pontos se alugados por mais de 1 dia. + return (daysRented > 1) ? 2 : 1; + } } diff --git a/Price.java b/Price.java index 0c77ca35..c0fa73ef 100644 --- a/Price.java +++ b/Price.java @@ -1,4 +1,9 @@ public abstract class Price { public abstract int getPriceCode(); - public abstract double getCharge(int daysRented); // Método abstrato. + public abstract double getCharge(int daysRented); // Método já existente. + + public int getFrequentRenterPoints(int daysRented) { + // Lógica base para calcular pontos (comportamento padrão). + return 1; // A maioria dos filmes gera 1 ponto por aluguel. + } } From 5967f871f0e31e3a63969935507d91d97c7710d3 Mon Sep 17 00:00:00 2001 From: giulliaVilanova Date: Tue, 26 Nov 2024 21:59:30 -0300 Subject: [PATCH 15/18] Commit 15 --- Movie.java | 8 ++++---- NewReleasePrice.java | 2 +- Price.java | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Movie.java b/Movie.java index dd2384df..90ced6c0 100644 --- a/Movie.java +++ b/Movie.java @@ -1,7 +1,7 @@ public class Movie { - public static final int CHILDRENS = 2; public static final int REGULAR = 0; - public static final int NEW_RELEASE = 1; + public static final int CHILDRENS = 1; + public static final int NEW_RELEASE = 2; private String _title; private Price _price; @@ -36,10 +36,10 @@ public String getTitle() { } public double getCharge(int daysRented) { - return _price.getCharge(daysRented); // Delegando à classe `Price`. + return _price.getCharge(daysRented); } public int getFrequentRenterPoints(int daysRented) { - return _price.getFrequentRenterPoints(daysRented); // Nova delegação. + return _price.getFrequentRenterPoints(daysRented); } } diff --git a/NewReleasePrice.java b/NewReleasePrice.java index b6a0cb7b..240e4eb9 100644 --- a/NewReleasePrice.java +++ b/NewReleasePrice.java @@ -11,7 +11,7 @@ public double getCharge(int daysRented) { @Override public int getFrequentRenterPoints(int daysRented) { - // Filmes de lançamento recente dão 2 pontos se alugados por mais de 1 dia. + // Sobrescrevendo para tratar o caso especial return (daysRented > 1) ? 2 : 1; } } diff --git a/Price.java b/Price.java index c0fa73ef..231eaa0a 100644 --- a/Price.java +++ b/Price.java @@ -1,9 +1,9 @@ public abstract class Price { public abstract int getPriceCode(); - public abstract double getCharge(int daysRented); // Método já existente. + public abstract double getCharge(int daysRented); public int getFrequentRenterPoints(int daysRented) { - // Lógica base para calcular pontos (comportamento padrão). - return 1; // A maioria dos filmes gera 1 ponto por aluguel. + // Regra padrão para pontos de aluguel + return 1; } } From 16be648ab0e0cc57be3a23e9c4e7eef57a6dc48c Mon Sep 17 00:00:00 2001 From: giulliaVilanova Date: Tue, 26 Nov 2024 22:01:08 -0300 Subject: [PATCH 16/18] Commit 16 --- Customer.java | 50 +++++++++++----------------------------------- HtmlStatement.java | 23 +++++++++++++++++++++ Statement.java | 4 ++++ TextStatement.java | 22 ++++++++++++++++++++ 4 files changed, 61 insertions(+), 38 deletions(-) create mode 100644 HtmlStatement.java create mode 100644 Statement.java create mode 100644 TextStatement.java diff --git a/Customer.java b/Customer.java index 0bb57809..b9933a80 100644 --- a/Customer.java +++ b/Customer.java @@ -17,45 +17,11 @@ public String getName() { return _name; } - public String statement() { - Enumeration rentals = _rentals.elements(); - String result = "Rental Record for " + getName() + "\n"; - - while (rentals.hasMoreElements()) { - Rental each = (Rental) rentals.nextElement(); - - // Mostrar os detalhes de cada aluguel - result += "\t" + each.getMovie().getTitle() + "\t" + - each.getCharge() + "\n"; - } - - // Adicionar o total e os pontos ao rodapé - result += "Amount owed is " + getTotalCharge() + "\n"; - result += "You earned " + getTotalFrequentRenterPoints() + " frequent renter points"; - return result; - } - - // Novo método para gerar o comprovante em HTML - public String htmlStatement() { - Enumeration rentals = _rentals.elements(); - String result = "

Rentals for " + getName() + "

\n"; - - while (rentals.hasMoreElements()) { - Rental each = (Rental) rentals.nextElement(); - - // Adicionar os detalhes de cada aluguel - result += each.getMovie().getTitle() + ": " + each.getCharge() + "
\n"; - } - - // Adicionar o total e os pontos ao rodapé - result += "

You owe " + getTotalCharge() + "

\n"; - result += "On this rental you earned " + - getTotalFrequentRenterPoints() + - " frequent renter points

"; - return result; + public Enumeration getRentals() { + return _rentals.elements(); } - private double getTotalCharge() { + public double getTotalCharge() { double result = 0; Enumeration rentals = _rentals.elements(); while (rentals.hasMoreElements()) { @@ -65,7 +31,7 @@ private double getTotalCharge() { return result; } - private int getTotalFrequentRenterPoints() { + public int getTotalFrequentRenterPoints() { int result = 0; Enumeration rentals = _rentals.elements(); while (rentals.hasMoreElements()) { @@ -74,4 +40,12 @@ private int getTotalFrequentRenterPoints() { } return result; } + + public String statement() { + return new TextStatement().value(this); // Delegação para TextStatement + } + + public String htmlStatement() { + return new HtmlStatement().value(this); // Delegação para HtmlStatement + } } diff --git a/HtmlStatement.java b/HtmlStatement.java new file mode 100644 index 00000000..bb8ebf4c --- /dev/null +++ b/HtmlStatement.java @@ -0,0 +1,23 @@ +import java.util.Enumeration; + +public class HtmlStatement extends Statement { + @Override + public String value(Customer aCustomer) { + Enumeration rentals = aCustomer.getRentals(); + String result = "

Rentals for " + aCustomer.getName() + "

\n"; + + while (rentals.hasMoreElements()) { + Rental each = (Rental) rentals.nextElement(); + // Adiciona detalhes do aluguel + result += each.getMovie().getTitle() + ": " + + String.valueOf(each.getCharge()) + "
\n"; + } + + // Adiciona as linhas finais + result += "

You owe " + String.valueOf(aCustomer.getTotalCharge()) + "

\n"; + result += "On this rental you earned " + + String.valueOf(aCustomer.getTotalFrequentRenterPoints()) + + " frequent renter points

"; + return result; + } +} diff --git a/Statement.java b/Statement.java new file mode 100644 index 00000000..428ab9fe --- /dev/null +++ b/Statement.java @@ -0,0 +1,4 @@ +public abstract class Statement { + // Método abstrato para implementar diferentes formatos de saída + public abstract String value(Customer aCustomer); +} diff --git a/TextStatement.java b/TextStatement.java new file mode 100644 index 00000000..afb7352e --- /dev/null +++ b/TextStatement.java @@ -0,0 +1,22 @@ +import java.util.Enumeration; + +public class TextStatement extends Statement { + @Override + public String value(Customer aCustomer) { + Enumeration rentals = aCustomer.getRentals(); + String result = "Rental Record for " + aCustomer.getName() + "\n"; + + while (rentals.hasMoreElements()) { + Rental each = (Rental) rentals.nextElement(); + // Adiciona detalhes do aluguel + result += "\t" + each.getMovie().getTitle() + "\t" + + String.valueOf(each.getCharge()) + "\n"; + } + + // Adiciona as linhas finais + result += "Amount owed is " + String.valueOf(aCustomer.getTotalCharge()) + "\n"; + result += "You earned " + String.valueOf(aCustomer.getTotalFrequentRenterPoints()) + + " frequent renter points"; + return result; + } +} From ee1dcc3679a94dc3390aa03faa0eaa3a8348704f Mon Sep 17 00:00:00 2001 From: giulliaVilanova Date: Tue, 26 Nov 2024 22:02:43 -0300 Subject: [PATCH 17/18] Commit 17 --- HtmlStatement.java | 28 +++++++++++++--------------- Statement.java | 23 +++++++++++++++++++++-- TextStatement.java | 28 +++++++++++++--------------- 3 files changed, 47 insertions(+), 32 deletions(-) diff --git a/HtmlStatement.java b/HtmlStatement.java index bb8ebf4c..c7f121b6 100644 --- a/HtmlStatement.java +++ b/HtmlStatement.java @@ -1,23 +1,21 @@ -import java.util.Enumeration; - public class HtmlStatement extends Statement { + @Override - public String value(Customer aCustomer) { - Enumeration rentals = aCustomer.getRentals(); - String result = "

Rentals for " + aCustomer.getName() + "

\n"; + protected String headerString(Customer aCustomer) { + return "

Rentals for " + aCustomer.getName() + "

\n"; + } - while (rentals.hasMoreElements()) { - Rental each = (Rental) rentals.nextElement(); - // Adiciona detalhes do aluguel - result += each.getMovie().getTitle() + ": " + - String.valueOf(each.getCharge()) + "
\n"; - } + @Override + protected String rentalString(Rental aRental) { + return aRental.getMovie().getTitle() + ": " + + String.valueOf(aRental.getCharge()) + "
\n"; + } - // Adiciona as linhas finais - result += "

You owe " + String.valueOf(aCustomer.getTotalCharge()) + "

\n"; - result += "On this rental you earned " + + @Override + protected String footerString(Customer aCustomer) { + return "

You owe " + String.valueOf(aCustomer.getTotalCharge()) + "

\n" + + "On this rental you earned " + String.valueOf(aCustomer.getTotalFrequentRenterPoints()) + " frequent renter points

"; - return result; } } diff --git a/Statement.java b/Statement.java index 428ab9fe..519fbd90 100644 --- a/Statement.java +++ b/Statement.java @@ -1,4 +1,23 @@ +import java.util.Enumeration; + public abstract class Statement { - // Método abstrato para implementar diferentes formatos de saída - public abstract String value(Customer aCustomer); + + // Método Template + public String value(Customer aCustomer) { + String result = headerString(aCustomer); + + Enumeration rentals = aCustomer.getRentals(); + while (rentals.hasMoreElements()) { + Rental each = (Rental) rentals.nextElement(); + result += rentalString(each); + } + + result += footerString(aCustomer); + return result; + } + + // Métodos abstratos a serem implementados pelas subclasses + protected abstract String headerString(Customer aCustomer); + protected abstract String rentalString(Rental aRental); + protected abstract String footerString(Customer aCustomer); } diff --git a/TextStatement.java b/TextStatement.java index afb7352e..ce5a7239 100644 --- a/TextStatement.java +++ b/TextStatement.java @@ -1,22 +1,20 @@ -import java.util.Enumeration; - public class TextStatement extends Statement { + @Override - public String value(Customer aCustomer) { - Enumeration rentals = aCustomer.getRentals(); - String result = "Rental Record for " + aCustomer.getName() + "\n"; + protected String headerString(Customer aCustomer) { + return "Rental Record for " + aCustomer.getName() + "\n"; + } - while (rentals.hasMoreElements()) { - Rental each = (Rental) rentals.nextElement(); - // Adiciona detalhes do aluguel - result += "\t" + each.getMovie().getTitle() + "\t" + - String.valueOf(each.getCharge()) + "\n"; - } + @Override + protected String rentalString(Rental aRental) { + return "\t" + aRental.getMovie().getTitle() + "\t" + + String.valueOf(aRental.getCharge()) + "\n"; + } - // Adiciona as linhas finais - result += "Amount owed is " + String.valueOf(aCustomer.getTotalCharge()) + "\n"; - result += "You earned " + String.valueOf(aCustomer.getTotalFrequentRenterPoints()) + + @Override + protected String footerString(Customer aCustomer) { + return "Amount owed is " + String.valueOf(aCustomer.getTotalCharge()) + "\n" + + "You earned " + String.valueOf(aCustomer.getTotalFrequentRenterPoints()) + " frequent renter points"; - return result; } } From bb0ac91335a52362d414d788ddef087bcb20e5ef Mon Sep 17 00:00:00 2001 From: giulliaVilanova Date: Tue, 26 Nov 2024 22:05:51 -0300 Subject: [PATCH 18/18] Commit 18 --- TextStatement.java | 1 + 1 file changed, 1 insertion(+) diff --git a/TextStatement.java b/TextStatement.java index ce5a7239..25c87dd2 100644 --- a/TextStatement.java +++ b/TextStatement.java @@ -18,3 +18,4 @@ protected String footerString(Customer aCustomer) { " frequent renter points"; } } +