Refactoring Test

测试:

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("\n");
		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("\n");
			totalAmount += thisAmount;
		}
		result.append("Amount owed is ").append(String.valueOf(totalAmount))
				.append("\n").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("\n")
				.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("\n");
		}
	}

	private void appendHeadInfoTo(StringBuilder result) {
		result.append("Rental Record for ").append(getName()).append("\n");
	}

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

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值