Skip to content
11 changes: 11 additions & 0 deletions ChildrensPrice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class ChildrensPrice extends Price {
public int getPriceCode() {
return Movie.CHILDRENS;
}
public double getCharge(int daysRented) {
double result = 1.5;
if (daysRented > 3)
result += (daysRented - 3) * 1.5;
return result;
}
}
44 changes: 44 additions & 0 deletions Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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 Enumeration getRentals() {
return _rentals.elements();
}
public String statement() {
return new TextStatement().value(this);
}
public String htmlStatement() {
return new HtmlStatement().value(this);
}
public double getTotalCharge() {
double result = 0;
Enumeration rentals = _rentals.elements();
while (rentals.hasMoreElements()) {
Rental each = (Rental) rentals.nextElement();
result += each.getCharge();
}
return result;
}
public int getTotalFrequentRenterPoints() {
int result = 0;
Enumeration rentals = _rentals.elements();
while (rentals.hasMoreElements()) {
Rental each = (Rental) rentals.nextElement();
result += each.getFrequentRenterPoints();
}
return result;
}
}
12 changes: 12 additions & 0 deletions HtmlStatement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class HtmlStatement extends Statement {
public String headerString(Customer aCustomer) {
return "<H1>Rentals for <EM>" + aCustomer.getName() + "</EM></H1><P>\n";
}
public String eachRentalString(Rental aRental) {
return aRental.getMovie().getTitle() + ": " + String.valueOf(aRental.getCharge()) + "<BR>\n";
}
public String footerString(Customer aCustomer) {
return "<P>You owe <EM>" + String.valueOf(aCustomer.getTotalCharge()) + "</EM><P>\n" +
"On this rental you earned <EM>" + String.valueOf(aCustomer.getTotalFrequentRenterPoints()) + "</EM> frequent renter points<P>";
}
}
39 changes: 39 additions & 0 deletions Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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);
}
}
11 changes: 11 additions & 0 deletions NewReleasePrice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class NewReleasePrice extends Price {
public int getPriceCode() {
return Movie.NEW_RELEASE;
}
public double getCharge(int daysRented) {
return daysRented * 3;
}
public int getFrequentRenterPoints(int daysRented) {
return (daysRented > 1) ? 2 : 1;
}
}
7 changes: 7 additions & 0 deletions Price.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public abstract class Price {
public abstract int getPriceCode();
public abstract double getCharge(int daysRented);
public int getFrequentRenterPoints(int daysRented) {
return 1;
}
}
11 changes: 11 additions & 0 deletions RegularPrice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class RegularPrice extends Price {
public int getPriceCode() {
return Movie.REGULAR;
}
public double getCharge(int daysRented) {
double result = 2;
if (daysRented > 2)
result += (daysRented - 2) * 1.5;
return result;
}
}
21 changes: 21 additions & 0 deletions Rental.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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);
}
}
18 changes: 18 additions & 0 deletions Statement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.Enumeration;

public abstract class Statement {
public abstract String headerString(Customer aCustomer);
public abstract String eachRentalString(Rental aRental);
public abstract String footerString(Customer aCustomer);

public String value(Customer aCustomer) {
Enumeration rentals = aCustomer.getRentals();
String result = headerString(aCustomer);
while (rentals.hasMoreElements()) {
Rental each = (Rental) rentals.nextElement();
result += eachRentalString(each);
}
result += footerString(aCustomer);
return result;
}
}
12 changes: 12 additions & 0 deletions TextStatement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class TextStatement extends Statement {
public String headerString(Customer aCustomer) {
return "Rental Record for " + aCustomer.getName() + "\n";
}
public String eachRentalString(Rental aRental) {
return "\t" + aRental.getMovie().getTitle() + "\t" + String.valueOf(aRental.getCharge()) + "\n";
}
public String footerString(Customer aCustomer) {
return "Amount owed is " + String.valueOf(aCustomer.getTotalCharge()) + "\n" +
"You earned " + String.valueOf(aCustomer.getTotalFrequentRenterPoints()) + " frequent renter points";
}
}