Question: JAVA REFACTORING TO STRATEGY DESIGN PATTERNS In this homework, you are required to use the code from previous homework for the movie rental application and

JAVA REFACTORING TO STRATEGY DESIGN PATTERNS In this homework, you are required to use the code from previous homework for the movie rental application and add the following features: 1) You are required to modify the code to make the function of computing frequent rental points into a strategy design pattern (That is, you will have strategy classes to compute frequent rental points). 2) After create a strategy class for the current strategy to compute frequent rental points, you are required to add two new strategies: a. The first new frequent rental point computation gives double regular points to any customer who is renting more than two types of categories b. The first new frequent rental point computation gives double regular points to any customer who is 18-22 and renting one or multiple new release movies. 3) Please submit your resulting code and a short Word document explaining the rationale behind your code.

My movie rental from previous homework:

///////////////////////////////////////////////////////////////////////// /////////////////// Movie Class /////////////////////////////////// ////////////////////////////////////////////////////////////////////////

public class Movie { private String _title; private double _price;

public Movie(String title, double price) { _title = title; _price = price; }

public double getPrice() { return _price; }

public void setPrice(double price) { _price = price; }

public String getTitle() { return _title; }

}

----------

Movie class refactored into 3 new subclasses as shown below :

// Childrens class

public class Childrens extends Movie {

public Childrens(String title, double price) { super(title, price); // Constructor for Childrens class }

public class Regular extends Movie {

public Regular(String title, double price) { super(title, price); // Constructor for Regular class }

public class NewRelease extends Movie {

public NewRelease(String title, double price) { super(title, price); // Constructor for Regular class }

////////////////////////////////////////////////////////

///////////////Customer Class////////////////////////

///////////////////////////////////////////////////////

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() + " "; double thisAmount = 0; while (rentals.hasMoreElements()) { Rental each = (Rental) rentals.nextElement(); thisAmount = thisAmount + each.ComputPrice() ; frequentRenterPoints++;

// add bonus for a two day new release rental

if ((each.getMovie() instanceof NewRelease ) && (each.getDaysRented() > 1)) { frequentRenterPoints++; } // show figures for this rental

result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(thisAmount) + " ";

totalAmount += thisAmount; }

// add footer lines

result += "Amount owed is " + String.valueOf(totalAmount) + " "; result += "You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points"; return result;

} }

Rental class with compute function:

///////////////////////////////////////////////// ///////////// Rental Class //////////////////// ////////////////////////////////////////////////

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 ComputPrice(){ double thisAmount=0.0; if( _movie instanceof Regular) { thisAmount += 2; if ( _daysRented > 2) { thisAmount += (_daysRented - 2) * _movie.getPrice(); } } if(_movie instanceof NewRelease) { thisAmount += _daysRented * _movie.getPrice(); } if( _movie instanceof Childrens ) { thisAmount += 1.5; if ( _daysRented > 3) { thisAmount += (_daysRented - 3) * _movie.getPrice(); }

} return thisAmount; }

}

Sample Test class with main :

public class Main {

public static void main(String[] args) { Childrens cmovie= new Childrens("Comic",1.5); Regular rmovie = new Regular("HarryPotter",1.5); NewRelease nrmovie = new NewRelease("Movie2017",3.0); Rental rental1 = new Rental(cmovie, 10); Rental rental2 = new Rental(rmovie, 5); Rental rental3 = new Rental(nrmovie, 7); Customer customer = new Customer("John"); customer.addRental(rental1); customer.addRental(rental2); customer.addRental(rental3); System.out.println(customer.statement());

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!