Docsity

Java Refactoring Exercise: Movie, Rental, and Customer Classes, Study notes of Computer Science

Toggle sidebar
bg1
Original Source Code for Refactoring Exercise
Movie.java
package edu.utsa.rentals;
public class Movie {
public static final int CHILDRENS = 2;
public static final int NEW_RELEASE = 1;
public static final int REGULAR = 0;
private int priceCode;
private String title;
public Movie(String aTitle, int aPriceCode) {
title = aTitle;
priceCode = aPriceCode;
}
public int getPriceCode() {
return priceCode;
}
public String getTitle() {
return title;
}
public void setPriceCode(int aPriceCode) {
priceCode = aPriceCode;
}
}
Rental.java
package edu.utsa.rentals;
public class Rental {
private Movie movie;
private int daysRented;
public Rental(Movie aMovie, int aDaysRented) {
movie = aMovie;
daysRented = aDaysRented;
}
public int getDaysRented() {
return daysRented;
}
public Movie getMovie() {
return movie;
}
}
pf3
pf4
pf5

Partial preview of the text

Download Java Refactoring Exercise: Movie, Rental, and Customer Classes and more Study notes Computer Science in PDF only on Docsity!

Original Source Code for Refactoring Exercise

Movie.java

package edu.utsa.rentals;

public class Movie { public static final int CHILDRENS = 2; public static final int NEW_RELEASE = 1; public static final int REGULAR = 0;

private int priceCode; private String title;

public Movie(String aTitle, int aPriceCode) { title = aTitle; priceCode = aPriceCode; }

public int getPriceCode() { return priceCode; }

public String getTitle() { return title; }

public void setPriceCode(int aPriceCode) { priceCode = aPriceCode; } }

Rental.java

package edu.utsa.rentals;

public class Rental { private Movie movie; private int daysRented;

public Rental(Movie aMovie, int aDaysRented) { movie = aMovie; daysRented = aDaysRented; }

public int getDaysRented() { return daysRented; }

public Movie getMovie() { return movie; }

Customer.java

package edu.utsa.rentals; import java.util.*; public class Customer { private String name; private List rentals;

public Customer(String aName) { name = aName; rentals = new ArrayList(); }

public void addRental(Rental aRental) { rentals.add(aRental); }

public String getName() { return name; }

public String statement() { double totalAmount = 0; int frequentRenterPoints = 0; Iterator iter = rentals.iterator(); String result = "RentalRecord for " + getName() + "\n"; while (iter.hasNext()) { double thisAmount = 0; Rental each = (Rental) iter.next(); 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; } frequentRenterPoints++; if ( (each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1) frequentRenterPoints++; result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(thisAmount) + "\n"; totalAmount += thisAmount; } result += "Amount owed is " + String.valueOf(totalAmount) + "\n"; result += "You earned " + String.valueOf(frequentRenterPoints) + " frequent Renter Points"; return result; }

childrens.getPriceCode()); childrens.setPriceCode(Movie.CHILDRENS); assertEquals("Price of childrens", Movie.CHILDRENS, childrens.getPriceCode()); }

public static Test suite() { TestSuite suite = new TestSuite(MovieTester.class); return suite; }

public static void main(String args[]) { junit.textui.TestRunner.run(suite()); } }

RentalTester.java

package test.edu.utsa.rentals;

import edu.utsa.rentals.; import junit.framework.;

public class RentalTester extends TestCase { private Movie childrens; private Movie regular; private Movie newRelease;

public RentalTester(String name) { super(name); }

protected void setUp() { childrens = new Movie("Children's Movie", Movie.CHILDRENS); regular = new Movie("Regular Movie", Movie.REGULAR); newRelease = new Movie("New Release", Movie.NEW_RELEASE); }

protected void tearDown() { childrens = null; regular = null; newRelease = null; }

public void testGetMovie() { Rental rent = new Rental(childrens, 0); assertEquals("Rent children's movie", childrens, rent.getMovie()); assertEquals("0 days rented", 0, rent.getDaysRented()); rent = new Rental(regular, -1); assertEquals("Negative days rented", -1, rent.getDaysRented()); }

public static Test suite() { TestSuite suite = new TestSuite(RentalTester.class); return suite;

public static void main(String args[]) { junit.textui.TestRunner.run(suite()); } }

CustomerTester.java

package test.edu.utsa.rentals;

import edu.utsa.rentals.; import junit.framework.;

public class CustomerTester extends TestCase { private Rental childrens; private Rental regular; private Rental newRelease;

public CustomerTester(String name) { super(name); }

protected void setUp() { childrens = new Rental(new Movie("Children's Movie", Movie.CHILDRENS), 1); regular = new Rental(new Movie("Regular Movie", Movie.REGULAR), 5); newRelease = new Rental(new Movie("New Release", Movie.NEW_RELEASE), 0); }

protected void tearDown() { childrens = null; regular = null; newRelease = null; }

public void testGetMovie() { Customer cust = new Customer("George"); assertEquals("Customer's name", "George", cust.getName()); String state1 = cust.statement(); assertNotSame("Statement when no rentals", null, cust.statement()); assertEquals("Called twice", state1, cust.statement()); cust.addRental(childrens); assertNotSame("One rental", state1, cust.statement()); }

public static Test suite() { TestSuite suite = new TestSuite(CustomerTester.class); return suite; }

public static void main(String args[]) { junit.textui.TestRunner.run(suite()); }