重構之一起點

這是一個影片出租店用的程序,計算每一位顧客的消費金額打印詳單。操作者告訴程序:顧客租了那些影片,租期多長,程序會根據租賃時間和影片類型算出費用。影片分爲三類:普通片,兒童片和新片。除了計算費用,還要爲常客計算積分,積分會根據租片種類是否爲新片而有所不同。

package movie;

import java.util.Enumeration;
import java.util.Vector;

public class Customer {
	private String _name;
	private Vector _rentals = new Vector();

	public Customer(String name) {
		this._name = name;
	}

	public void addRental(Rental rental) {
		this._rentals.add(rental);
	}

	public String getName() {
		return this._name;
	}

	public String statement() {
		double totalAmount = 0;
		int frequentRenterPoints = 0;
		Enumeration rentals = this._rentals.elements();
		String result = "Rental Record for " + getName() + "\n";
		while (rentals.hasMoreElements()) {
			double thisAmount = 0;
			Rental each = (Rental) rentals.nextElement();
			// ....
//			switch (each.get_movie().get_priceCode()) {
//			case Movie.REGULAR:
//				thisAmount += 2;
//				if (each.get_daysRented() > 2)
//					thisAmount += (each.get_daysRented() - 2) * 1.5;
//
//				break;
//			case Movie.NEW_RELEASE:
//				thisAmount += 3;
//				break;
//			case Movie.CHILDRENS:
//				thisAmount += 1.5;
//				if (each.get_daysRented() > 3)
//					thisAmount += (each.get_daysRented() - 3) * 1.5;
//				break;
//			default:
//				break;
//			}
			thisAmount = amountFor(each);
			frequentRenterPoints ++;
			if((each.get_movie().get_priceCode() == Movie.NEW_RELEASE) && each.get_daysRented() > 1)
				frequentRenterPoints ++;
			result += "\t" + each.get_movie().get_title() + "\t" + String.valueOf(thisAmount) + "\n";
		}
		
		return result;
	}
	
	private double amountFor(Rental aRental){
		int thisAmount = 0;
		switch (aRental.get_movie().get_priceCode()) {
		case Movie.REGULAR:
			thisAmount += 2;
			if (aRental.get_daysRented() > 2)
				thisAmount += (aRental.get_daysRented() - 2) * 1.5;

			break;
		case Movie.NEW_RELEASE:
			thisAmount += 3;
			break;
		case Movie.CHILDRENS:
			thisAmount += 1.5;
			if (aRental.get_daysRented() > 3)
				thisAmount += (aRental.get_daysRented() - 3) * 1.5;
			break;
		default:
			break;
		}
		return thisAmount;
	}

}

package movie;

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 int _priceCode;
	
	public Movie(String title, int priceCode) {
		this._title = title;
		this._priceCode = priceCode;
	}

	public String get_title() {
		return _title;
	}

	public void set_title(String _title) {
		this._title = _title;
	}

	public int get_priceCode() {
		return _priceCode;
	}

	public void set_priceCode(int _priceCode) {
		this._priceCode = _priceCode;
	}

}


 

package movie;

public class Rental {
	private Movie _movie;
	private int _daysRented;
	
	public Rental(Movie movie, int daysRented) {
		this._movie = movie;
		this._daysRented = daysRented;
	}

	public Movie get_movie() {
		return _movie;
	}

	public void set_movie(Movie _movie) {
		this._movie = _movie;
	}

	public int get_daysRented() {
		return _daysRented;
	}

	public void set_daysRented(int _daysRented) {
		this._daysRented = _daysRented;
	}
	
	
}


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章