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 String statement = customer.statement(); 66 return System.currentTimeMillis() - startTime; 67 } 68 69 public static void main(String argv[]) 70 { 71 Customer harry = new Customer("Harry"); 72 Movie movie = new Movie("Gone With The Wind", Movie.REGULAR); 73 Rental rental = new Rental(movie, 3); 74 harry.addRental(rental); 75 String statement = harry.statement(); 76 Customer tom = new Customer("Tom"); 77 // Suppose we have 1000000 rentals. How long will it take 78 // to compute the total charge? 79 int numberRentals = 1000000; 80 long timeToCompute = stressTest(tom, rental, numberRentals); 81 System.out.println("time elapsed for " + numberRentals + 82 " rentals = " + timeToCompute); 83 } 84 } 85 // Output: 86 // $ java -classpath . Customer 87 // 88 // 89 // 90 // 91 // 92 93