vendredi 5 mai 2017

Code smell and Refactoring in java

I need help to identify the code smells and refactor the given code. The condition for refactoring is :

Define 2 new rules with respect to rental charges that are applicable and with the ability to add new categories of movies.

Ex : Rental amount for children's movie for more than 2 days should be 100 from 3rd day onwards.

I have 3 classes (Children, Movie and Rental). I need suggestion on code smell and refactoring it.

enter code here

Customer.java

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

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

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

Aucun commentaire:

Enregistrer un commentaire