Permalink
Browse files

Fix feature envy, moving both `Determine*()` to `Rental`

  • Loading branch information...
rdeneau committed Aug 26, 2018
1 parent 8b222b8 commit fc1896e2dc2c244203c2e6f0e3bf8e4201f873db
@@ -12,5 +12,40 @@ public Rental(Movie movie, int daysRented)
Movie = movie;
DaysRented = daysRented;
}

public decimal DetermineAmount()
{
var rentalAmount = 0m;
switch (Movie.PriceCode)
{
case Movie.REGULAR:
rentalAmount += 2;
if (DaysRented > 2)
{
rentalAmount += (DaysRented - 2) * 1.5m;
}
break;
case Movie.NEW_RELEASE:
rentalAmount += DaysRented * 3;
break;
case Movie.CHILDREN:
rentalAmount += 1.5m;
if (DaysRented > 3)
{
rentalAmount += (DaysRented - 3) * 1.5m;
}
break;
}

return rentalAmount;
}

public int DetermineFrequentRenterPoints()
{
var bonusIsEarned = Movie.PriceCode == Movie.NEW_RELEASE && DaysRented > 1;
if (bonusIsEarned)
return 2;
return 1;
}
}
}
@@ -50,52 +50,15 @@ private string RentalLines()

private string RentalLine(Rental rental)
{
var rentalAmount = DetermineAmount(rental);
var rentalPoints = DetermineFrequentRenterPoints(rental);
var rentalAmount = rental.DetermineAmount();
var rentalPoints = rental.DetermineFrequentRenterPoints();

FrequentRenterPoints += rentalPoints;
TotalAmount += rentalAmount;

return FormatRentalLine(rental, rentalAmount);
}

private static decimal DetermineAmount(Rental rental)
{
var rentalAmount = 0m;
switch (rental.Movie.PriceCode)
{
case Movie.REGULAR:
rentalAmount += 2;
if (rental.DaysRented > 2)
{
rentalAmount += (rental.DaysRented - 2) * 1.5m;
}

break;
case Movie.NEW_RELEASE:
rentalAmount += rental.DaysRented * 3;
break;
case Movie.CHILDREN:
rentalAmount += 1.5m;
if (rental.DaysRented > 3)
{
rentalAmount += (rental.DaysRented - 3) * 1.5m;
}

break;
}

return rentalAmount;
}

private static int DetermineFrequentRenterPoints(Rental rental)
{
var bonusIsEarned = rental.Movie.PriceCode == Movie.NEW_RELEASE && rental.DaysRented > 1;
if (bonusIsEarned)
return 2;
return 1;
}

private static string FormatRentalLine(Rental rental, decimal rentalAmount) =>
FormatLine($"\t{rental.Title}\t{rentalAmount:0.0}");

0 comments on commit fc1896e

Please sign in to comment.