Refactoring Test
10266 ワード
テスト:
テスト結果(Green):
コードは次のとおりです.
Before:
Movie.java
Rental.java
Customer.java
After:
Price.java
RegularPrice.java
NewRealsePrice.java
Children.java
Movie.java
Rental.java
Customer.java
import org.junit.Before;
import org.junit.Test;
public class CustomerTest {
private Customer customer;
@Before
public void setUp() throws Exception {
customer = new Customer("flyingh");
Movie movie = new Movie("haha1_movie", Movie.NEW_RELEASE);
Movie movie2 = new Movie("haha2_movie", Movie.REGULAR);
Movie movie3 = new Movie("haha3_movie", Movie.CHILDREN);
Rental rental = new Rental(movie, 3);
Rental rental2 = new Rental(movie2, 2);
Rental rental3 = new Rental(movie3, 1);
customer.addRental(rental);
customer.addRental(rental2);
customer.addRental(rental3);
}
@Test
public void testStatement() {
System.out.println(customer.statement());
}
}
テスト結果(Green):
Rental Record for flyingh
haha1_movie 9.0
haha2_movie 2.0
haha3_movie 1.5
Amount owed is 12.5
You earned 4 frequent renter points
コードは次のとおりです.
Before:
Movie.java
public class Movie {
public static final int REGULAR = 0;
public static final int NEW_RELEASE = 1;
public static final int CHILDREN = 2;
private String title;
private int priceCode;
public Movie(String title, int priceCode) {
super();
this.title = title;
this.priceCode = priceCode;
}
public String getTitle() {
return title;
}
public int getPriceCode() {
return priceCode;
}
public void setPriceCode(int priceCode) {
this.priceCode = priceCode;
}
}
Rental.java
public class Rental {
private Movie movie;
private int daysRented;
public Rental(Movie movie, int daysRented) {
super();
this.movie = movie;
this.daysRented = daysRented;
}
public Movie getMovie() {
return movie;
}
public int getDaysRented() {
return daysRented;
}
}
Customer.java
import java.util.ArrayList;
import java.util.List;
public class Customer {
private String name;
private List<Rental> list = new ArrayList<Rental>();
public Customer(String name) {
super();
this.name = name;
}
public String statement() {
double totalAmount = 0;
int frequentRenterPoints = 0;
StringBuilder result = new StringBuilder();
result.append("Rental Record for ").append(getName()).append("
");
for (Rental rental : list) {
double thisAmount = 0;
switch (rental.getMovie().getPriceCode()) {
case Movie.REGULAR:
thisAmount += 2;
if (rental.getDaysRented() > 2) {
thisAmount += (rental.getDaysRented() - 2) * 1.5;
}
break;
case Movie.NEW_RELEASE:
thisAmount += rental.getDaysRented() * 3;
break;
case Movie.CHILDREN:
thisAmount += 1.5;
if (rental.getDaysRented() > 3) {
thisAmount += (rental.getDaysRented() - 3) * 1.5;
}
break;
}
++frequentRenterPoints;
if (rental.getMovie().getPriceCode() == Movie.NEW_RELEASE
&& rental.getDaysRented() > 1) {
++frequentRenterPoints;
}
result.append("\t").append(rental.getMovie().getTitle())
.append("\t").append(String.valueOf(thisAmount))
.append("
");
totalAmount += thisAmount;
}
result.append("Amount owed is ").append(String.valueOf(totalAmount))
.append("
").append("You earned ")
.append(String.valueOf(frequentRenterPoints))
.append(" frequent renter points");
return result.toString();
}
public void addRental(Rental rental) {
list.add(rental);
}
public String getName() {
return name;
}
}
After:
Price.java
public abstract class Price {
public abstract int getPriceCode();
public abstract double getCharge(int daysRented);
}
RegularPrice.java
public class RegularPrice extends Price {
@Override
public int getPriceCode() {
// TODO Auto-generated method stub
return Movie.REGULAR;
}
@Override
public double getCharge(int daysRented) {
// TODO Auto-generated method stub
double result = 2;
if (daysRented > 2) {
result += (daysRented - 2) * 1.5;
}
return result;
}
}
NewRealsePrice.java
public class NewRealsePrice extends Price {
@Override
public int getPriceCode() {
// TODO Auto-generated method stub
return Movie.NEW_RELEASE;
}
@Override
public double getCharge(int daysRented) {
// TODO Auto-generated method stub
return daysRented * 3;
}
}
Children.java
public class ChildrenPrice extends Price {
@Override
public int getPriceCode() {
// TODO Auto-generated method stub
return Movie.CHILDREN;
}
@Override
public double getCharge(int daysRented) {
// TODO Auto-generated method stub
double result = 1.5;
if (daysRented > 3) {
result += (daysRented - 3) * 1.5;
}
return result;
}
}
Movie.java
public class Movie {
public static final int REGULAR = 0;
public static final int NEW_RELEASE = 1;
public static final int CHILDREN = 2;
private String title;
private Price price;
public Movie(String title, int priceCode) {
super();
this.title = title;
setPriceByPriceCode(priceCode);
}
public String getTitle() {
return title;
}
public int getPriceCode() {
return price.getPriceCode();
}
public void setPriceByPriceCode(int priceCode) {
switch (priceCode) {
case Movie.REGULAR:
price = new RegularPrice();
break;
case Movie.NEW_RELEASE:
price = new NewRealsePrice();
break;
case Movie.CHILDREN:
price = new ChildrenPrice();
break;
default:
throw new IllegalArgumentException();
}
}
double getCharge(int daysRented) {
return price.getCharge(daysRented);
}
int getFrequentRenterPoints(int daysRented) {
// TODO Auto-generated method stub
return getPriceCode() == Movie.NEW_RELEASE && daysRented > 1 ? 2 : 1;
}
}
Rental.java
public class Rental {
private Movie movie;
private int daysRented;
public Rental(Movie movie, int daysRented) {
super();
this.movie = movie;
this.daysRented=daysRented;
}
public Movie getMovie() {
return movie;
}
public int getDaysRented() {
return daysRented;
}
double getCharge() {
return movie.getCharge(daysRented);
}
int getFrequentRenterPoints() {
return movie.getFrequentRenterPoints(daysRented);
}
}
Customer.java
import java.util.ArrayList;
import java.util.List;
public class Customer {
private String name;
private List<Rental> list = new ArrayList<Rental>();
public Customer(String name) {
super();
this.name = name;
}
public String statement() {
StringBuilder result = new StringBuilder();
appendInfoTo(result);
return result.toString();
}
private void appendInfoTo(StringBuilder result) {
appendHeadInfoTo(result);
appendMiddleInfoTo(result);
appendTailInfoTo(result);
}
private void appendTailInfoTo(StringBuilder result) {
result.append("Amount owed is ")
.append(String.valueOf(getTotalCharge())).append("
")
.append("You earned ")
.append(String.valueOf(getTotalFrequentRenterPoints()))
.append(" frequent renter points");
}
private void appendMiddleInfoTo(StringBuilder result) {
for (Rental rental : list) {
result.append("\t").append(rental.getMovie().getTitle())
.append("\t").append(String.valueOf(getCharge(rental)))
.append("
");
}
}
private void appendHeadInfoTo(StringBuilder result) {
result.append("Rental Record for ").append(getName()).append("
");
}
private int getTotalFrequentRenterPoints() {
int frequentRenterPoints = 0;
for (Rental rental : list) {
frequentRenterPoints += getFrequentRenterPoints(rental);
}
return frequentRenterPoints;
}
private double getTotalCharge() {
double totalAmount = 0;
for (Rental rental : list) {
totalAmount += getCharge(rental);
}
return totalAmount;
}
private int getFrequentRenterPoints(Rental rental) {
return rental.getFrequentRenterPoints();
}
private double getCharge(Rental rental) {
return rental.getCharge();
}
public void addRental(Rental rental) {
list.add(rental);
}
public String getName() {
return name;
}
}