














































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Hicks; Class: PROG LANG TECH & PDGMS; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;
Typology: Study notes
1 / 54
This page cannot be seen from the preview
Don't miss anything!















































2
a program to calculate and print a
statement of a customer s charges at a
video store
3
4
7
public String statement() { double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rentals.elements(); String result = Rental Record for + getName() + \n ;
while (rentals.hasMoreElements()) { double thisAmount = 0; Rental each = (Rental)rentals.nextElement();
// determine amounts for each line switch (each.getMovie().getPriceCode()) { case Movie.REGULAR: thisAmount += 2; if (each.getDaysRented() > 2) thisAmount += (each.getDaysRented() - 2) * 1.5; break; case Movie.NEW_RELEASE: thisAmount += each.getDaysRented() * 3; break; case Movie.CHILDRENS: thisAmount += 1.5; if (each.getDaysRented() > 3) thisAmount += (each.getDaysRented() - 3) * 1.5; break; }
8
// add frequent renter points frequentRenterPoints++; // add bonus for a two day new release rental if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1) frequentRenterPoints++;
// show figures for this rental result += \t + each.getMovie().getTitle() + \t + String.valueOf( thisAmount ) + \n ; totalAmount += thisAmount; }
// add footer lines result += Amount owed is + String.valueOf( totalAmount ) + \n ; result += You earned + String.valueOf( frequentRenterPoints ) + frequent renter points ; return result; } }
9
10
What is it?
13
Build self-checking tests.
14
Extract logical chunk of code as a new
method.
Apply Extract Method.
15
class Customer {
public String statement() { double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rentals.elements(); String result = Rental Record for + getName() + \n ;
while (rentals.hasMoreElements()) { double thisAmount = 0; Rental each = (Rental)rentals.nextElement();
// determine amounts for each line switch (each.getMovie().getPriceCode()) { case Movie.REGULAR: thisAmount += 2; if (each.getDaysRented() > 2) thisAmount += (each.getDaysRented() - 2) * 1.5; break; case Movie.NEW_RELEASE: thisAmount += each.getDaysRented() * 3; break; case Movie.CHILDRENS: thisAmount += 1.5; if (each.getDaysRented() > 3) thisAmount += (each.getDaysRented() - 3) * 1.5; break; }
16
class Customer {
public String statement() { double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rentals.elements(); String result = Rental Record for + getName() + \n ;
while (rentals.hasMoreElements()) { double thisAmount = 0; Rental each = (Rental)rentals.nextElement();
thisAmount = amountFor( each );
19
Enhance readability.
20
class Customer {
private double amountFor( Rental each ) { double thisAmount = 0; switch (each.getMovie().getPriceCode()) { case Movie.REGULAR: thisAmount += 2; if (each.getDaysRented() > 2) thisAmount += (each.getDaysRented() - 2) * 1.5; break; case Movie.NEW_RELEASE: thisAmount += each.getDaysRented() * 3; break; case Movie.CHILDRENS: thisAmount += 1.5; if (each.getDaysRented() > 3) thisAmount += (each.getDaysRented() - 3) * 1.5; break; } return thisAmount; }
}
21
class Customer {
private double amountFor( Rental aRental ) { double result = 0; switch (aRental.getMovie().getPriceCode()) { case Movie.REGULAR: result += 2; if (aRental.getDaysRented() > 2) result += (aRental.getDaysRented() - 2) * 1.5; break; case Movie.NEW_RELEASE: result += aRental.getDaysRented() * 3; break; case Movie.CHILDRENS: result += 1.5; if (aRental.getDaysRented() > 3) result += (aRental.getDaysRented() - 3) * 1.5; break; } return result; }
}
22
25
class Rental {
double getCharge() { double result = 0; switch (getMovie().getPriceCode()) { case Movie.REGULAR: result += 2; if (getDaysRented() > 2) result += (getDaysRented() - 2) * 1.5; break; case Movie.NEW_RELEASE: result += getDaysRented() * 3; break; case Movie.CHILDRENS: result += 1.5; if (getDaysRented() > 3) result += (getDaysRented() - 3) * 1.5; break; } return result; }
}
26
class Customer {
private double amountFor( Rental aRental ) { return aRental.getCharge(); }
}
27
28
Adjust references to old method to use
new method.
Remove old method.
31
32
Replace redundant temporary variable with
query.
Apply Replace Temp with Query.
33
class Customer {
public String statement() { double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rentals.elements(); String result = Rental Record for + getName() + \n ;
while (rentals.hasMoreElements()) { double thisAmount = 0; Rental each = (Rental)rentals.nextElement();
thisAmount = each.getCharge();
// add frequent renter points frequentRenterPoints++; // add bonus for a two day new release rental if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1) frequentRenterPoints++;
// show figures for this rental result += \t + each.getMovie().getTitle() + \t + String.valueOf( thisAmount ) + \n ; totalAmount += thisAmount; }
34
class Customer {
public String statement() { double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rentals.elements(); String result = Rental Record for + getName() + \n ;
while (rentals.hasMoreElements()) { Rental each = (Rental)rentals.nextElement();
// add frequent renter points frequentRenterPoints++; // add bonus for a two day new release rental if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1) frequentRenterPoints++;
// show figures for this rental result += \t + each.getMovie().getTitle() + \t + String.valueOf( each.getCharge() ) + \n ; totalAmount += each.getCharge(); }
37
class Customer {
public String statement() { double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rentals.elements(); String result = Rental Record for + getName() + \n ;
while (rentals.hasMoreElements()) { Rental each = (Rental)rentals.nextElement();
// add frequent renter points frequentRenterPoints += each.getFrequentRenterPoints();
// show figures for this rental result += \t + each.getMovie().getTitle() + \t + String.valueOf( each.getCharge() ) + \n ; totalAmount += each.getCharge(); }
38
class Rental {
int getFrequentRenterPoints() { if ((getMovie().getPriceCode() == Movie.NEW_RELEASE) && getDaysRented() > 1) return 2; else return 1; }
}
39
Apply Replace Temp with Query.
40
class Customer {
public String statement() { double totalAmount = 0; int frequentRenterPoints = 0; Enumeration rentals = _rentals.elements(); String result = Rental Record for + getName() + \n ;
while (rentals.hasMoreElements()) { Rental each = (Rental)rentals.nextElement();
// add frequent renter points frequentRenterPoints += each.getFrequentRenterPoints();
// show figures for this rental result += \t + each.getMovie().getTitle() + \t + String.valueOf( each.getCharge() ) + \n ; totalAmount += each.getCharge(); }
// add footer lines result += Amount owed is + String.valueOf( totalAmount ) + \n ; result += You earned + String.valueOf( frequentRenterPoints ) + frequent renter points ; return result; } }