Question: I have three java class and I need to determined a piece of the bed smelled code, also refactorin... I have three java class and

I have three java class and I need to determined a piece of the bed smelled code, also refactorin...

I have three java class and I need to determined a piece of the bed smelled code, also refactoring that resolves the smells.

I know the bad smell in customer class will be the \" switch \" statement, but how do I refactoring the code ?

Customer.java class ----------------------------------------------------------------------------------------

public class Customer { private String name; private ArrayList rentals = new ArrayList(); public Customer (String name) { this.name = name; } public void addRent(Rental unit) { rentals.add(unit); } public String getCustomerName() { return name; } public String generateStatement() { double totalAmount = 0; int frequentRenterPoints = 0; String result = \"Rental Record for \" + getCustomerName() + \" \"; for(Rental rentalUnit : rentals) { double thisAmount = 0; // determine amounts for each line switch (rentalUnit.getMovie().getPriceCode()) { case Movie.REGULAR: thisAmount += 2; if (rentalUnit.getDaysRented() > 2) thisAmount += (rentalUnit.getDaysRented() - 2) * 1.5; break; case Movie.NEW_RELEASE: thisAmount += rentalUnit.getDaysRented() * 3; break; case Movie.CHILDRENS: thisAmount += 1.5; if (rentalUnit.getDaysRented() > 3) thisAmount += (rentalUnit.getDaysRented() - 3) * 1.5; break; } // add frequent renter points frequentRenterPoints++; // add bonus for a two day new release rental if ((rentalUnit.getMovie().getPriceCode() == Movie.NEW_RELEASE) && (rentalUnit.getDaysRented() > 1)) frequentRenterPoints++; // show figures for this rental result += \"\\t\" + rentalUnit.getMovie().getMovieTitle() + \"\\t\" + String.valueOf(thisAmount) + \" \"; totalAmount += thisAmount; } // add footer lines result += \"Amount owed is \" + String.valueOf(totalAmount) + \" \"; result += \"You earned \" + String.valueOf(frequentRenterPoints) + \" frequent renter points\"; return result; } }

Movie class -------------------------------------------------------------------------------------------------

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 code) { this.title = title; this.priceCode = code; } public int getPriceCode() { return priceCode; } public void setPriceCode(int arg) { priceCode = arg; } public String getMovieTitle() { return title; }

}

Rental class -----------------------------------------------------------------------------------------

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!