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 // How many times do we traverse the rentals ArrayList? 20 // Does it matter? 21 public String statement() 22 { 23 Iterator rentals = _rentals.iterator(); 24 String result = "Rental Record for " + getName() + "\n"; 25 while (rentals.hasNext()) { 26 Rental rental = (Rental) rentals.next(); 27 //show figures for this rental 28 result += "\t" + rental.getMovie().getTitle()+ "\t" + 29 rental.getCharge() + 30 "\n"; 31 } 32 //add footer lines 33 result += "Amount owed is " + getTotalCharge() + "\n"; 34 35 result += "You earned " + getTotalFrequentRenterPoints() + 36 " frequent renter points"; 37 return result; 38 } 39 40 private double getTotalCharge() 41 { 42 double result = 0; 43 Iterator rentals = _rentals.iterator(); 44 while(rentals.hasNext()) { 45 result += ((Rental) rentals.next()).getCharge(); 46 } 47 return result; 48 } 49 50 private int getTotalFrequentRenterPoints() 51 { 52 int result = 0; 53 Iterator rentals = _rentals.iterator(); 54 while(rentals.hasNext()) 55 result += ((Rental)rentals.next()).getFrequentRenterPoints(); 56 return result; 57 } 58 59 private static long stressTest(Customer customer, Rental rental, int numberRentals) 60 { 61 for(int i = 0; i < numberRentals; i++) 62 customer.addRental(rental); 63 long startTime = System.currentTimeMillis(); 64 double totalCharge = customer.getTotalCharge(); 65 return System.currentTimeMillis() - startTime; 66 } 67 68 public static void main(String argv[]) 69 { 70 Customer harry = new Customer("Harry"); 71 Movie movie = new Movie("Gone With The Wind", Movie.REGULAR); 72 Rental rental = new Rental(movie, 3); 73 harry.addRental(rental); 74 System.out.println(harry.statement()); 75 Customer tom = new Customer("Tom"); 76 // Suppose we have 1000000 rentals. How long will it take 77 // to compute the total charge? 78 int numberRentals = 1000000; 79 long timeToCompute = stressTest(tom, rental, 1000000); 80 System.out.println("time elapsed for " + numberRentals + 81 " rentals = " + timeToCompute); 82 } 83 } 84 // Output: 85 // $ java -classpath . Customer 86 //Rental Record for Harry 87 // Gone With The Wind 3.5 88 //Amount owed is 3.5 89 //You earned 1 frequent renter points 90 // time elapsed for 1000000 rentals = 985