首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >管理电影租金

管理电影租金
EN

Code Review用户
提问于 2017-05-07 05:24:31
回答 1查看 1K关注 0票数 3

重构的条件是:

定义两个新的规则,有关租金是适用的,并有能力添加新的类别的电影。例:儿童电影2天以上的租金从第3天起应为100英镑。

我有3个类(ChildrenMovieRental)。我需要关于代码嗅觉和重构的建议。

Customer.java

代码语言:javascript
运行
复制
import java.util.ArrayList;
import java.util.Iterator;

public class Customer {

    private String name;
    private ArrayList rentals = new ArrayList();

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

    public void addRental(Rental arg) {
        rentals.add(arg);
    }

    public String getName() {
        return name;
    }

     public String statement() { 
    
        double totalAmount = 0;
        int frequentRenterPoints = 0;
        
        String returnVal = "Rental record for : " + this.getName() + ".....\n";
        
        Iterator iter = rentals.iterator();
        
        while(iter.hasNext()) {
            double thisAmount = 0;
            Rental each = (Rental) iter.next();
            //determine amounts for each line
            switch (each.getMovie().getPriceCode()) {
                case Movie.REGULAR: thisAmount += 100;
                if (each.getDaysRented() > 2)
                    thisAmount += (each.getDaysRented() - 2) * 75; 
                break;
                
                case Movie.NEW_RELEASE:
                thisAmount += each.getDaysRented() * 150; 
                break;
                
                case Movie.CHILDREN:
                thisAmount += 75;
                if (each.getDaysRented() > 3)
                thisAmount += (each.getDaysRented() - 3) * 75; 
                break;
            }
            
            
            // add frequent renter points
            frequentRenterPoints ++;
            // add bonus for a two day new release rental
            if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1) 
                frequentRenterPoints ++;
            
            //show figures for this rental
            returnVal += "\t" + each.getMovie().getTitle() + ": " + thisAmount + "\n";
            totalAmount += thisAmount;

        }
        
        //print footer
        returnVal += "Amount owed is: " + totalAmount + "\n";
        returnVal += "You have earned " + frequentRenterPoints + " frequent renter points";

        return returnVal;
    }
}

Movie.java

代码语言:javascript
运行
复制
public class Movie {
    
    public static final int CHILDREN = 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 int getPriceCode() {
        return priceCode;
    }

    public void setPriceCode(int arg) {
        priceCode = arg;
    }

    public String getTitle() {
        return title;
    }
}

Rental.java

代码语言:javascript
运行
复制
class Rental {
    
    private Movie movie;
    private int daysRented;

    public Rental(Movie movie, int daysRented) {
        this.movie = movie;
        this.daysRented = daysRented;
    }

    public int getDaysRented() {
        return daysRented;
    }

    public Movie getMovie() {
        return movie;
    }
}
EN

回答 1

Code Review用户

发布于 2017-05-08 09:56:31

private ArrayList rentals = new ArrayList();

该列表包含Rental对象,因此应该使其成为通用对象。不再需要演员了!

private List<Rental> rentals = new ArrayList<>();

ps:为了便于维护,我将类型从ArrayList (实现)更改为List (接口),参见基于接口的编程

票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/162722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档