起点:编写3个类的代码
1、第一个类-影片(Movie):
package com.lee.test.aFirstExample;
public class Movie {
/**
* @param title
* @param priceCode
*/
public Movie(String title, int priceCode) {
super();
this.title = title;
this.priceCode = priceCode;
}
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 String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getPriceCode() {
return priceCode;
}
public void setPriceCode(int priceCode) {
this.priceCode = priceCode;
}
}
2、第二个类-租赁(Rental):
package com.lee.test.aFirstExample;
public class Rental {
/**
* @param movie
* @param dayRented
*/
public Rental(Movie movie, int dayRented) {
super();
this.movie = movie;
this.dayRented = dayRented;
}
private Movie movie;
public Movie getMovie() {
return movie;
}
private int dayRented;
public int getDayRented() {
return dayRented;
}
}
3、第三个类-消费者(Customer):
package com.lee.test.aFirstExample;
import java.util.Enumeration;
import java.util.Vector;
public class Customer {
/**
* @param name
*/
public Customer(String name) {
super();
this.name = name;
}
private String name;
public String getName() {
return name;
}
private Vector rentals = new Vector();
public void addRental(Rental arg)
{
rentals.addElement(arg);
}
public String statement(){
double totalAmount = 0;
int frequentRenterPoints = 0;
Enumeration rentalss = rentals.elements();
String result = "Rental Record for" +" "+ getName()+"\n";
while(rentalss.hasMoreElements())
{
double thisAmount = 0;
Rental each = (Rental) rentalss.nextElement();
switch(each.getMovie().getPriceCode())
{
case Movie.regular:
thisAmount += 2;
if(each.getDayRented()>2)
thisAmount += (each.getDayRented()-2)*1.5;
break;
case Movie.new_release:
thisAmount += each.getDayRented()*3;
break;
case Movie.childrens:
thisAmount += 1.5;
if(each.getDayRented()>3)
thisAmount += (each.getDayRented()-3)*1.5;
break;
}
//积分 每借一张加1个积分
frequentRenterPoints++;
//积分累加条件 新版本的片子,借的时间大于1天
if((each.getMovie().getPriceCode()==Movie.new_release)&&each.getDayRented()>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;
}
}
4、自己定义客户端(Client)。
package com.lee.test.aFirstExample;
public class Client {
public static void main(String[] args) {
Movie mov = new Movie("metal",2);
Rental ren = new Rental(mov,8);
Customer cus = new Customer("Lee");
cus.addRental(ren);
System.out.println(cus.statement());
}
}
5、输出结果
Rental Record for Lee
metal 9.0
Amount owed is 9.0
You earned 1 frequent renter points
6、重构第一步
提炼方法(Extract Method)
提炼Switch/Case分支到一个方法,修改后Customer代码:
package com.lee.test.aFirstExample;
import java.util.Enumeration;
import java.util.Vector;
public class Customer {
/**
* @param name
*/
public Customer(String name) {
super();
this.name = name;
}
private String name;
public String getName() {
return name;
}
private Vector rentals = new Vector();
public void addRental(Rental arg)
{
rentals.addElement(arg);
}
public String statement(){
double totalAmount = 0;
int frequentRenterPoints = 0;
Enumeration rentalss = rentals.elements();
String result = "Rental Record for" +" "+ getName()+"\n";
while(rentalss.hasMoreElements())
{
double thisAmount = 0;
Rental each = (Rental) rentalss.nextElement();
thisAmount = amountFor(each); //提炼方法
/*switch(each.getMovie().getPriceCode())
{
case Movie.regular:
thisAmount += 2;
if(each.getDayRented()>2)
thisAmount += (each.getDayRented()-2)*1.5;
break;
case Movie.new_release:
thisAmount += each.getDayRented()*3;