《重构》 — Java示例:影片出租店程序(1、重构前)

说明:
这是一个影片出租店用的程序,计算每一位顾客的消费金额并打印报表(statement),操作者告诉程序:顾客租了哪些影片、租期多长,程序便根据租赁时间和影片类型算出费用。影片分为三类;普通片、儿童片和新片。除了计算费用,还要为常客计算点数;点数会随着"租片种类是否为新片而有不同。

代码:
1、Test.java
package movie;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Customer customer = new Customer("ZhangSan");
        
        customer.addRental(new Rental(new Movie("aaa",Movie.REGULAR),1));
        customer.addRental(new Rental(new Movie("bbb",Movie.NEW_RELEASE),1));    
        customer.addRental(new Rental(new Movie("ccc",Movie.CHILDRENS),1));    
        
        System.out.println(customer.statement());
    }

}

2、Customer.java
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) {
        _name = name;
    }

    public void addRental(Rental obj) {
        _rentals.addElement(obj);
    }

    public String get_name() {
        return _name;
    }
    
    public String statement() {
        double totalAmount = 0; //--总消费金额
        int frequentRenterPoints = 0; //--常客积点
        Enumeration rentals = _rentals.elements();
        String result = "Rental Record for " + get_name() + "/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 += each.get_daysRented() * 3;
                    break;
                case Movie.CHILDRENS: //--儿童片
                    thisAmount += 1.5;
                    if (each.get_daysRented() > 3)
                        thisAmount += (each.get_daysRented() - 3) * 1.5;
                    break;
            }
            
            //---累加常客积点
            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";
            totalAmount += thisAmount;
        }
        //---结尾打印
        result += "Amount owed is " + String.valueOf(totalAmount) + "/n";
        result += "You earned " + String.valueOf(frequentRenterPoints) +
                 " frequent renter points";
        return result;
    }
}

3、Movie.java
package movie;

/**
* 影片
*
*/
public class Movie {
    public static final int REGULAR = 0;
    public static final int NEW_RELEASE = 1;
    public static final int CHILDRENS = 2;
    
    private String _title; //名称
    private int _priceCode; //价格(代号)
    
    public Movie(String title, int priceCode) {
        _title = title;
        _priceCode = priceCode;
    }

    public int get_priceCode() {
        return _priceCode;
    }

    public void set_priceCode(int code) {
        _priceCode = code;
    }

    public String get_title() {
        return _title;
    }
    
}

4、Rental.java
package movie;

/**
* 租赁
*
*/
public class Rental {
    private Movie _movie; //影片
    private int _daysRented; //租期
    
    public Rental(Movie movie, int daysRented) {
        _movie = movie;
        _daysRented = daysRented;
    }

    public int get_daysRented() {
        return _daysRented;
    }

    public Movie get_movie() {
        return _movie;
    }
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值