Skip to content

Commit

Permalink
Close to OOP refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rihter007 committed Nov 20, 2015
1 parent 9dcde15 commit 1ddb9bc
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 84 deletions.
51 changes: 28 additions & 23 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

140 changes: 79 additions & 61 deletions src/main/java/com/scrumtrek/simplestore/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,63 @@

public class Customer
{
private static final double REGULAR_INITIAL_BONUS = 2;
private static final double REGULAR_MIN_BONUS_DAYS = 2;
private static final double REGULAR_DAY_BONUS = 1.5;
private static class RegularPriceCalculus
{
private static final double REGULAR_INITIAL_BONUS = 2;
private static final double REGULAR_MIN_BONUS_DAYS = 2;
private static final double REGULAR_DAY_BONUS = 1.5;

public static double calculate(int daysRented)
{
double resultAmount = REGULAR_INITIAL_BONUS;
if (daysRented > REGULAR_MIN_BONUS_DAYS)
resultAmount += (daysRented - REGULAR_MIN_BONUS_DAYS) * REGULAR_DAY_BONUS;

return resultAmount;
}
}

private static class NewReleasePriceCalculus
{
private static final double NEW_RELEASE_DAY_BONUS = 3;

public static double calculate(int daysRented)
{
return daysRented * NEW_RELEASE_DAY_BONUS;
}
}

private static final double NEW_RELEASE_DAY_BONUS = 3;
private static class ChildrenPriceCalculus
{
private static final double CHILDRENS_INITIAL_BONUS = 1.5;
private static final double CHILDRENS_MIN_BONUS_DAYS = 3;
private static final double CHILDRENS_DAY_BONUS = 1.5;

public static double calculate(int daysRented)
{
double resultAmount = CHILDRENS_INITIAL_BONUS;
if (daysRented > CHILDRENS_MIN_BONUS_DAYS)
resultAmount += (daysRented - CHILDRENS_MIN_BONUS_DAYS) * CHILDRENS_DAY_BONUS;
return resultAmount;
}
}

private static final double CHILDRENS_INITIAL_BONUS = 1.5;
private static final double CHILDRENS_MIN_BONUS_DAYS = 3;
private static final double CHILDRENS_DAY_BONUS = 1.5;
private class RentalInfo
{
public String movieTitle;
public double totalAmount;
public int frequentPoints;

public RentalInfo(Rental rental)
{
this.movieTitle = rental.getMovie().getTitle();
this.totalAmount = getBonusForTargetRental(rental);
this.frequentPoints = getFrequentPointsForRental(rental);
}
}

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

public Customer(String name)
{
Expand All @@ -28,79 +73,52 @@ public String getName()
return this.name;
}

public void addRental(Rental arg)
public void addRental(Rental rental)
{
this.rentals.add(arg);
// we can create statement at this point, but I am lazy :)
this.rentals.add(new RentalInfo(rental));
}

public String Statement()
{
double totalAmount = 0;
int frequentRenterPoints = 0;

String result = "Rental record for " + this.name + "\n";

for (Rental each : this.rentals)
{
double thisAmount = getBonusForTargetPriceCode(each.getMovie().getPriceCode(), each.getDaysRented());

// Add frequent renter points
frequentRenterPoints++;
String result = "Rental record for " + this.name + "\n";

// Add bonus for a two-day new-release rental
if ((each.getMovie().getPriceCode() == PriceCodes.NewRelease) && (each.getDaysRented() > 1))
{
frequentRenterPoints++;
}

// Show figures for this rental
result += "\t" + each.getMovie().getTitle() + "\t" + thisAmount + "\n";
totalAmount += thisAmount;
}
double totalAmount = 0.0;
int frequentRenterPoints = 0;
for (RentalInfo rentalInfo : this.rentals)
{
result += "\t" + rentalInfo.movieTitle + "\t" + rentalInfo.totalAmount + "\n";
totalAmount += rentalInfo.totalAmount;
frequentRenterPoints += rentalInfo.frequentPoints;
}

// Add footer lines
result += "Amount owed is " + totalAmount + "\n";
result += "You earned " + frequentRenterPoints + " frequent renter points.";
return result;
result += "Amount owed is " + totalAmount + "\n";
result += "You earned " + frequentRenterPoints + " frequent renter points.";
return result;
}

private double getBonusForTargetPriceCode(PriceCodes priceCode, int daysRented)
private static double getBonusForTargetRental(Rental rental)
{
switch (priceCode)
switch (rental.getMovie().getPriceCode())
{
case Regular:
return calculateBonusForRegularPriceCode(daysRented);
return RegularPriceCalculus.calculate(rental.getDaysRented());

case NewRelease:
return calculateBonusForNewReleasePriceCode(daysRented);
return NewReleasePriceCalculus.calculate(rental.getDaysRented());

case Childrens:
return calculateBonusForChildrensPriceCode(daysRented);
return ChildrenPriceCalculus.calculate(rental.getDaysRented());
}

throw new AssertionError();
}

private double calculateBonusForRegularPriceCode(int daysRented)
{
double resultAmount = REGULAR_INITIAL_BONUS;
if (daysRented > REGULAR_MIN_BONUS_DAYS)
resultAmount += (daysRented - REGULAR_MIN_BONUS_DAYS) * REGULAR_DAY_BONUS;

return resultAmount;
}

private double calculateBonusForNewReleasePriceCode(int daysRented)
{
return daysRented * NEW_RELEASE_DAY_BONUS;
}

private double calculateBonusForChildrensPriceCode(int daysRented)
{
double resultAmount = CHILDRENS_INITIAL_BONUS;
if (daysRented > CHILDRENS_MIN_BONUS_DAYS)
resultAmount += (daysRented - CHILDRENS_MIN_BONUS_DAYS) * CHILDRENS_DAY_BONUS;
return resultAmount;
}
private static int getFrequentPointsForRental(Rental rental)
{
if ((rental.getMovie().getPriceCode() == PriceCodes.NewRelease) && (rental.getDaysRented() > 1))
return 2;
return 1;
}
}

0 comments on commit 1ddb9bc

Please sign in to comment.