Skip to content
Open
12 changes: 12 additions & 0 deletions ChildrensPrice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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;
}
}
51 changes: 51 additions & 0 deletions Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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;
}
}
20 changes: 20 additions & 0 deletions HtmlStatement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class HtmlStatement extends Statement {

protected String headerString(Customer aCustomer) {
return "<H1>Rentals for <EM>" + aCustomer.getName() +
"</EM></H1><P>\n";
}

protected String eachRentalString(Rental aRental) {
return aRental.getMovie().getTitle()+ ": " +
String.valueOf(aRental.getCharge()) + "<BR>\n";
}

protected 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>";
}
}
46 changes: 46 additions & 0 deletions Movie.java
Original file line number Diff line number Diff line change
@@ -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 _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);
}
}
13 changes: 13 additions & 0 deletions NewReleasePrice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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;
}
}
8 changes: 8 additions & 0 deletions Price.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
12 changes: 12 additions & 0 deletions RegularPrice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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;
}
}
26 changes: 26 additions & 0 deletions Rental.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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);
}
}
19 changes: 19 additions & 0 deletions Statement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.Enumeration;

public abstract class Statement {

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;
}

protected abstract String headerString(Customer aCustomer);
protected abstract String eachRentalString(Rental aRental);
protected abstract String footerString(Customer aCustomer);
}
19 changes: 19 additions & 0 deletions TextStatement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class TextStatement extends Statement {

protected String headerString(Customer aCustomer) {
return "Rental Record for " + aCustomer.getName() + "\n";
}

protected String eachRentalString(Rental aRental) {
return "\t" + aRental.getMovie().getTitle()+ "\t" +
String.valueOf(aRental.getCharge()) + "\n";
}

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