Skip to content
Snippets Groups Projects
Commit 8dff9243 authored by WILLEM THEO's avatar WILLEM THEO
Browse files

exo1

parent f1229f5b
Branches
No related merge requests found
Pipeline #364723 passed with stages
in 4 minutes and 35 seconds
...@@ -4,6 +4,14 @@ import java.util.ArrayList; ...@@ -4,6 +4,14 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class Customer { public class Customer {
private final static float REGULAR_PRICE = 2;
private final static float CHILDRENS_PRICE = 1.5f;
private final static int MAX_REGULAR_RENTAL_DAYS = 2;
private final static int MAX_CHILDREN_RENTAL_DAYS = 3;
private final static float NORMAL_OUTRENTAL_MULTIPLICATOR = 2;
private final static float NEW_RELEASE_OUTRENTAL_MULTIPLICATOR = 3;
private String name; private String name;
private List<Rental> rentals = new ArrayList<Rental>(); private List<Rental> rentals = new ArrayList<Rental>();
...@@ -12,57 +20,63 @@ public class Customer { ...@@ -12,57 +20,63 @@ public class Customer {
this.name = name; this.name = name;
} }
public void addRental(Rental arg) { public void addRental(Rental rental) {
rentals.add(arg); rentals.add(rental);
} }
public String getName() { public String getName() {
return name; return name;
} }
public String statement() { public String constructRentalRecap() {
double amt = 0; double totalPrice = 0;
int pnts = 0; int loyaltyPoint = 0;
String result = "Rental Record for " + getName() + "\n"; String rentalRecap = "Rental Record for " + getName() + "\n";
for (Rental each : rentals) { for (Rental rental : rentals) {
double thisAmt = 0; double rentalPrice = 0;
//determine amounts for each line rentalPrice = calculRentalPrice(rental, rentalPrice);
switch (each.getMovie().getPriceCode()) { loyaltyPoint = calculLoyaltyPoint(loyaltyPoint, rental);
case Movie.REGULAR:
thisAmt += 2; rentalRecap += "\t" + rental.getMovie().getTitle() + "\t" + String.valueOf(rentalPrice) + "\n";
if (each.getDaysRented() > 2) { totalPrice += rentalPrice;
thisAmt += (each.getDaysRented() - 2) * 1.5;
}
break;
case Movie.NEW_RELEASE:
thisAmt += each.getDaysRented() * 3;
break;
case Movie.CHILDRENS:
thisAmt += 1.5;
if (each.getDaysRented() > 3) {
thisAmt += (each.getDaysRented() - 3) * 1.5;
}
break;
}
// add frequent renter points, with bonus for a two day new release rental
if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1) {
pnts += 2;
} else {
pnts++;
}
// show figures for this rental
result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(thisAmt) + "\n";
amt += thisAmt;
} }
// add footer lines // Add footer lines
result += "Amount owed is " + String.valueOf(amt) + "\n"; rentalRecap += "Amount owed is " + String.valueOf(totalPrice) + "\n";
result += "You earned " + String.valueOf(pnts) + " frequent renter points"; rentalRecap += "You earned " + String.valueOf(loyaltyPoint) + " frequent renter points";
return result; return rentalRecap;
}
private int calculLoyaltyPoint(int loyaltyPoint, Rental rental) {
if ((rental.getMovie().getPriceCode() == Movie.NEW_RELEASE) && rental.getDaysRented() > 1) {
loyaltyPoint += 2;
} else {
loyaltyPoint++;
}
return loyaltyPoint;
}
private double calculRentalPrice(Rental rental, double rentalPrice) {
switch (rental.getMovie().getPriceCode()) {
case Movie.REGULAR:
rentalPrice += REGULAR_PRICE;
if (rental.getDaysRented() > 2) {
rentalPrice += (rental.getDaysRented() - MAX_REGULAR_RENTAL_DAYS) * NORMAL_OUTRENTAL_MULTIPLICATOR;
}
break;
case Movie.NEW_RELEASE:
rentalPrice += rental.getDaysRented() * NEW_RELEASE_OUTRENTAL_MULTIPLICATOR;
break;
case Movie.CHILDRENS:
rentalPrice += CHILDRENS_PRICE;
if (rental.getDaysRented() > 3) {
rentalPrice += (rental.getDaysRented() - MAX_CHILDREN_RENTAL_DAYS) * NORMAL_OUTRENTAL_MULTIPLICATOR;
}
break;
}
return rentalPrice;
} }
} }
...@@ -40,7 +40,7 @@ public class CustomerTest { ...@@ -40,7 +40,7 @@ public class CustomerTest {
"\tGone with the Wind\t3.5\n" + "\tGone with the Wind\t3.5\n" +
"Amount owed is 3.5\n" + "Amount owed is 3.5\n" +
"You earned 1 frequent renter points"; "You earned 1 frequent renter points";
String statement = customer2.statement(); String statement = customer2.constructRentalRecap();
assertEquals(expected, statement); assertEquals(expected, statement);
} }
...@@ -57,7 +57,7 @@ public class CustomerTest { ...@@ -57,7 +57,7 @@ public class CustomerTest {
"\tStar Wars\t9.0\n" + "\tStar Wars\t9.0\n" +
"Amount owed is 9.0\n" + "Amount owed is 9.0\n" +
"You earned 2 frequent renter points"; "You earned 2 frequent renter points";
String statement = customer2.statement(); String statement = customer2.constructRentalRecap();
assertEquals(expected, statement); assertEquals(expected, statement);
} }
...@@ -74,7 +74,7 @@ public class CustomerTest { ...@@ -74,7 +74,7 @@ public class CustomerTest {
"\tMadagascar\t1.5\n" + "\tMadagascar\t1.5\n" +
"Amount owed is 1.5\n" + "Amount owed is 1.5\n" +
"You earned 1 frequent renter points"; "You earned 1 frequent renter points";
String statement = customer2.statement(); String statement = customer2.constructRentalRecap();
assertEquals(expected, statement); assertEquals(expected, statement);
} }
...@@ -97,7 +97,7 @@ public class CustomerTest { ...@@ -97,7 +97,7 @@ public class CustomerTest {
"\tGone with the Wind\t11.0\n" + "\tGone with the Wind\t11.0\n" +
"Amount owed is 23.0\n" + "Amount owed is 23.0\n" +
"You earned 4 frequent renter points"; "You earned 4 frequent renter points";
String statement = customer1.statement(); String statement = customer1.constructRentalRecap();
assertEquals(expected, statement); assertEquals(expected, statement);
} }
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment