1 import java.util.*; 2 3 public class Customer { 4 private String _name; 5 private ArrayList _rentals = new ArrayList(); 6 7 public Customer(String name) { 8 _name = name; 9 } 10 11 public void addRental(Rental arg) { 12 _rentals.add(arg); 13 } 14 15 public String getName() { 16 return _name; 17 } 18 19 public String statement() 20 { 21 double totalAmount = 0; 22 int frequentRenterPoints = 0; 23 Iterator rentals = _rentals.iterator(); 24 String result = "Rental Record for " + getName() + "\n"; 25 while (rentals.hasNext ()) { 26 double thisAmount = 0; 27 Rental each = (Rental) rentals.next(); 28 //determine amounts for each line 29 switch (each.getMovie().getPriceCode()) { 30 case Movie.REGULAR: 31 thisAmount += 2; 32 if (each.getDaysRented() > 2) 33 thisAmount += (each.getDaysRented() - 2) * 1.5; 34 break; 35 case Movie.NEW_RELEASE: 36 thisAmount += each.getDaysRented() * 3; 37 break; 38 case Movie.CHILDRENS: 39 thisAmount += 1.5; 40 if (each.getDaysRented() > 3) 41 thisAmount += (each.getDaysRented() - 3) * 1.5; 42 break; 43 } 44 // add frequent renter points 45 frequentRenterPoints ++; // add bonus for a two day new release rental 46 if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) 47 && each.getDaysRented() > 1) 48 frequentRenterPoints ++; 49 //show figures for this rental 50 result += "\t" + each.getMovie().getTitle()+ "\t" + 51 thisAmount + 52 "\n"; 53 totalAmount += thisAmount; 54 } 55 //add footer lines 56 result += "Amount owed is " + String.valueOf(totalAmount) + "\n"; 57 58 result += "You earned " + String.valueOf(frequentRenterPoints) + 59 " frequent renter points"; 60 return result; 61 } 62 63 public static void main(String argv[]) 64 { 65 Customer harry = new Customer("Harry"); 66 Movie movie = new Movie("Gone With The Wind", Movie.REGULAR); 67 Rental rental = new Rental(movie, 3); 68 harry.addRental(rental); 69 System.out.println(harry.statement()); 70 } 71 } 72 // Output: 73 // $ java -classpath . Customer 74 // Rental Record for Harry 75 // Gone With The Wind 3.5 76 // Amount owed is 3.5 77 // You earned 1 frequent renter points 78 // $ 79 80