État initial

Transcription

État initial
État initial
package com.example.refactoring;
import java.util.Enumeration;
import java.util.Vector;
/**
* Classe Customer, état initial
*/
public class Customer {
private String _name = "";
private Vector _rentals = new Vector<Rental>();
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;
}
// 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;
}
public String getName() {
return _name;
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
package com.example.refactoring;
/**
* Classe Rental, état initial
*/
public class Rental {
private int _daysRented = 0;
private Movie _movie = null;
public int getDaysRented() {
return _daysRented;
}
public Movie getMovie() {
return _movie;
}
}
package com.example.refactoring;
/**
* Classe Movie, état initial
*/
public class Movie {
public static final int CHILDRENS = 0;
public static final int REGULAR = 1;
public static final int NEW_RELEASE = 2;
private String _title = "";
private int _priceCode;
public String getTitle() {
return _title;
}
public int getPriceCode() {
return _priceCode;
}
public void setPriceCode(int priceCode) {
this._priceCode = priceCode;
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
1er Refactoring – Extraire une méthode (switch dans statement())
package com.example.refactoring;
import java.util.Enumeration;
import java.util.Vector;
/**
* Classe Customer
*/
public class Customer {
private String _name = "";
private Vector _rentals = new Vector<Rental>();
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
thisAmount = amountFor(each);
// 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;
}
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;
}
public String getName() {
return _name;
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
2e Refactoring – Renommer un paramètre (each dans amountFor())
package com.example.refactoring;
import java.util.Enumeration;
import java.util.Vector;
/**
* Classe Customer
*/
public class Customer {
private String _name = "";
private Vector _rentals = new Vector<Rental>();
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
thisAmount = amountFor(each);
// 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;
}
private double amountFor(Rental aRental) {
double thisAmount = 0;
switch (aRental.getMovie().getPriceCode()) {
case Movie.REGULAR:
thisAmount += 2;
if (aRental.getDaysRented() > 2) {
thisAmount += (aRental.getDaysRented() - 2) * 1.5;
}
break;
case Movie.NEW_RELEASE:
thisAmount += aRental.getDaysRented() * 3;
break;
case Movie.CHILDRENS:
thisAmount += 1.5;
if (aRental.getDaysRented() > 3) {
thisAmount += (aRental.getDaysRented() - 3) * 1.5;
}
break;
}
return thisAmount;
}
public String getName() {
return _name;
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
3e Refactoring – Renommer une variable (thisAmount dans amountFor())
package com.example.refactoring;
import java.util.Enumeration;
import java.util.Vector;
/**
* Classe Customer
*/
public class Customer {
private String _name = "";
private Vector _rentals = new Vector<Rental>();
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
thisAmount = amountFor(each);
// 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;
}
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;
}
public String getName() {
return _name;
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
4e Refactoring – Déplacer une méthode (amountFor() vers Rental)
package com.example.refactoring;
import java.util.Enumeration;
import java.util.Vector;
/**
* Classe Customer
*/
public class Customer {
private String _name = "";
private Vector _rentals = new Vector<Rental>();
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
thisAmount = each.amountFor(each);
// 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;
}
public String getName() {
return _name;
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
package com.example.refactoring;
/**
* Classe Rental
*/
public class Rental {
private int _daysRented = 0;
private Movie _movie = null;
public int getDaysRented() {
return _daysRented;
}
public Movie getMovie() {
return _movie;
}
public double amountFor(Rental aRental) {
double result = 0;
switch (getMovie().getPriceCode()) {
case Movie.REGULAR:
result += 2;
if (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 (getDaysRented() > 3) {
result += (aRental.getDaysRented() - 3) * 1.5;
}
break;
}
return result;
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
5e Refactoring – Enlever un paramètre (aRental dans amountFor())
package com.example.refactoring;
import java.util.Enumeration;
import java.util.Vector;
/**
* Classe Customer
*/
public class Customer {
private String _name = "";
private Vector _rentals = new Vector<Rental>();
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
thisAmount = each.amountFor();
// 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;
}
public String getName() {
return _name;
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
package com.example.refactoring;
/**
* Classe Rental
*/
public class Rental {
private int _daysRented = 0;
private Movie _movie = null;
public int getDaysRented() {
return _daysRented;
}
public Movie getMovie() {
return _movie;
}
public double amountFor() {
double result = 0;
switch (getMovie().getPriceCode()) {
case Movie.REGULAR:
result += 2;
if (getDaysRented() > 2) {
result += (this.getDaysRented() - 2) * 1.5;
}
break;
case Movie.NEW_RELEASE:
result += this.getDaysRented() * 3;
break;
case Movie.CHILDRENS:
result += 1.5;
if (getDaysRented() > 3) {
result += (this.getDaysRented() - 3) * 1.5;
}
break;
}
return result;
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
6e Refactoring – Renommer une méthode (amountFor pour getCharge)
package com.example.refactoring;
/**
* Classe Rental
*/
public class Rental {
private int _daysRented = 0;
private Movie _movie = null;
public int getDaysRented() {
return _daysRented;
}
public Movie getMovie() {
return _movie;
}
public double getCharge() {
double result = 0;
switch (getMovie().getPriceCode()) {
case Movie.REGULAR:
result += 2;
if (getDaysRented() > 2) {
result += (this.getDaysRented() - 2) * 1.5;
}
break;
case Movie.NEW_RELEASE:
result += this.getDaysRented() * 3;
break;
case Movie.CHILDRENS:
result += 1.5;
if (getDaysRented() > 3) {
result += (this.getDaysRented() - 3) * 1.5;
}
break;
}
return result;
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
7e Refactoring – Remplacer une variable par une requête (thisAmount par getCharge() dans
statement)
package com.example.refactoring;
import java.util.Enumeration;
import java.util.Vector;
/**
* Classe Customer
*/
public class Customer {
private String _name = "";
private Vector _rentals = new Vector<Rental>();
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();
}
//add footer lines
result += "Amount owed is " + String.valueOf(totalAmount) + "\n";
result += "You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points";
return result;
}
public String getName() {
return _name;
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
8e Refactoring – Extraire une méthode (getFrequentRenterPoints() dans statement)
package com.example.refactoring;
import java.util.Enumeration;
import java.util.Vector;
/**
* Classe Customer
*/
public class Customer {
private String _name = "";
private Vector _rentals = new Vector<Rental>();
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 = getFrequentRenterPoints(each);
//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;
}
private int getFrequentRenterPoints(Rental each) {
int frequentRenterPoints = 1;
// add bonus for a two day new release rental
if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1) {
frequentRenterPoints++;
}
return frequentRenterPoints;
}
public String getName() {
return _name;
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
9e Refactoring – Déplacer une méthode (getFrequentRenterPoints() vers Rental)
package com.example.refactoring;
import java.util.Enumeration;
import java.util.Vector;
/**
* Classe Customer
*/
public class Customer {
private String _name = "";
private Vector _rentals = new Vector<Rental>();
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(each);
//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;
}
public String getName() {
return _name;
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
package com.example.refactoring;
/**
* Classe Rental
*/
public class Rental {
private int _daysRented = 0;
private Movie _movie = null;
public int getDaysRented() {
return _daysRented;
}
public Movie getMovie() {
return _movie;
}
public double getCharge() {
double result = 0;
switch (getMovie().getPriceCode()) {
case Movie.REGULAR:
result += 2;
if (getDaysRented() > 2) {
result += (this.getDaysRented() - 2) * 1.5;
}
break;
case Movie.NEW_RELEASE:
result += this.getDaysRented() * 3;
break;
case Movie.CHILDRENS:
result += 1.5;
if (getDaysRented() > 3) {
result += (this.getDaysRented() - 3) * 1.5;
}
break;
}
return result;
}
public int getFrequentRenterPoints(Rental each) {
int frequentRenterPoints = 1;
// add bonus for a two day new release rental
if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1) {
frequentRenterPoints++;
}
return frequentRenterPoints;
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
10e Refactoring – Enlever un paramètre (each dans getFrequentRenterPoints())
package com.example.refactoring;
import java.util.Enumeration;
import java.util.Vector;
/**
* Classe Customer
*/
public class Customer {
private String _name = "";
private Vector _rentals = new Vector<Rental>();
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;
}
public String getName() {
return _name;
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
package com.example.refactoring;
/**
* Classe Rental
*/
public class Rental {
private int _daysRented = 0;
private Movie _movie = null;
public int getDaysRented() {
return _daysRented;
}
public Movie getMovie() {
return _movie;
}
public double getCharge() {
double result = 0;
switch (getMovie().getPriceCode()) {
case Movie.REGULAR:
result += 2;
if (getDaysRented() > 2) {
result += (this.getDaysRented() - 2) * 1.5;
}
break;
case Movie.NEW_RELEASE:
result += this.getDaysRented() * 3;
break;
case Movie.CHILDRENS:
result += 1.5;
if (getDaysRented() > 3) {
result += (this.getDaysRented() - 3) * 1.5;
}
break;
}
return result;
}
public int getFrequentRenterPoints() {
int frequentRenterPoints = 1;
// add bonus for a two day new release rental
if ((getMovie().getPriceCode() == Movie.NEW_RELEASE) && getDaysRented() > 1) {
frequentRenterPoints++;
}
return frequentRenterPoints;
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
11e Refactoring – Remplacer une variable par une requête (totalAmount par getTotalCharge())
package com.example.refactoring;
import java.util.Enumeration;
import java.util.Vector;
/**
* Classe Customer
*/
public class Customer {
private String _name = "";
private Vector _rentals = new Vector<Rental>();
public String statement() {
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";
}
//add footer lines
result += "Amount owed is " + String.valueOf(getTotalCharge()) + "\n";
result += "You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points";
return result;
}
private double getTotalCharge() {
Enumeration rentals = _rentals.elements();
double result = 0;
while (rentals.hasMoreElements()) {
Rental each = (Rental) rentals.nextElement();
result += each.getCharge();
}
return result;
}
public String getName() {
return _name;
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
12e Refactoring – Remplacer une variable par une requête (frequentRenterPoints par
getTotalFrequentRenterPoints())
package com.example.refactoring;
import java.util.Enumeration;
import java.util.Vector;
/**
* Classe Customer
*/
public class Customer {
private String _name = "";
private Vector _rentals = new Vector<Rental>();
public String statement() {
Enumeration rentals = _rentals.elements();
String result = "Rental Record for " + getName() + "\n";
while (rentals.hasMoreElements()) {
Rental each = (Rental) rentals.nextElement();
//show figures for this rental
result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(each.getCharge()) + "\n";
}
//add footer lines
result += "Amount owed is " + String.valueOf(getTotalCharge()) + "\n";
result += "You earned " + String.valueOf(getTotalfrequentRenterPoints()) + " frequent renter points";
return result;
}
private int getTotalfrequentRenterPoints() {
Enumeration rentals = _rentals.elements();
int result = 0;
while (rentals.hasMoreElements()) {
Rental each = (Rental) rentals.nextElement();
result += each.getFrequentRenterPoints();
}
return result;
}
private double getTotalCharge() {
Enumeration rentals = _rentals.elements();
double result = 0;
while (rentals.hasMoreElements()) {
Rental each = (Rental) rentals.nextElement();
result += each.getCharge();
}
return result;
}
public String getName() {
return _name;
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
Ajout de la méthode htmlStatement()
package com.example.refactoring;
import java.util.Enumeration;
import java.util.Vector;
/**
* Classe Customer
*/
public class Customer {
private String _name = "";
private Vector _rentals = new Vector<Rental>();
public String statement() {
Enumeration rentals = _rentals.elements();
String result = "Rental Record for " + getName() + "\n";
while (rentals.hasMoreElements()) {
Rental each = (Rental) rentals.nextElement();
result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(each.getCharge()) + "\n";
}
result += "Amount owed is " + String.valueOf(getTotalCharge()) + "\n";
result += "You earned " + String.valueOf(getTotalfrequentRenterPoints()) + " frequent renter points";
return result;
}
public String htmlStatement() {
Enumeration rentals = _rentals.elements();
String result = "<h1>Rental Record for <em>" + getName() + "</em></h1><br />\n";
while (rentals.hasMoreElements()) {
Rental each = (Rental) rentals.nextElement();
result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(each.getCharge()) + "<br/>\n";
}
result += "<p>Amount owed is <em>" + String.valueOf(getTotalCharge()) + "</em></p>\n";
result += "<p>You earned <em>" + String.valueOf(getTotalfrequentRenterPoints()) + "</em> frequent renter points</p>";
return result;
}
private int getTotalfrequentRenterPoints() {
Enumeration rentals = _rentals.elements();
int result = 0;
while (rentals.hasMoreElements()) {
Rental each = (Rental) rentals.nextElement();
result += each.getFrequentRenterPoints();
}
return result;
}
private double getTotalCharge() {
Enumeration rentals = _rentals.elements();
double result = 0;
while (rentals.hasMoreElements()) {
Rental each = (Rental) rentals.nextElement();
result += each.getCharge();
}
return result;
}
public String getName() {
return _name;
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
État final, après avoir enlevé le switch (Java 5.0)
remplacé par ArrayList<Rental> parce qu’on n’a pas besoin d’une collection thread-safe
Boucles de parcours while et Enumeration remplacées par des boucles for each
Le calcul du prix de location et des points bonis est dans une nouvelle enum (MovieCategory). On aurait aussi pu utiliser le pattern
Strategy.
Ajout des methods absolument nécessaires pour tester
Commentaires enlevés dans le code
Commentaires Javadoc ajoutés
Vector<Rental>
package com.example.refactoring;
/**
* Rental detailing a rental of one movie by a specific customer
*/
public class Rental {
/**
* Number of days the movie was rented
*/
private int daysRented = 0;
/**
* The rented movie
*/
private Movie movie = null;
/**
* @param daysRented the rental's duration, in days
* @param movie the rented movie
*/
public Rental(int daysRented, Movie movie) {
this.daysRented = daysRented;
this.movie = movie;
}
public int getDaysRented() {
return daysRented;
}
public Movie getMovie() {
return movie;
}
public double getCharge() {
return this.getMovie().getCategory().getCharge(this.getDaysRented());
}
public int getFrequentRenterPoints() {
return this.getMovie().getCategory().getFrequentRenterPoints(this.getDaysRented());
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
package com.example.refactoring;
import java.util.ArrayList;
/**
* Video store customer
*/
public class Customer {
/**
* Customer's name
*/
private String name = "";
/**
* The customer's current rentals
*/
private ArrayList<Rental> rentals = new ArrayList<Rental>();
/**
* @param name the customer'S complete name
*/
public Customer(String name) {
this.name = name;
}
/**
* Generates a statement detailing the customer's current rentals, billing information and frequent renter points
* @return a String containing the preformatted plain text statement
*/
public String statement() {
String result = "Rental Record for " + getName() + "\n";
for (Rental each : rentals) {
result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(each.getCharge()) + "\n";
}
result += "Amount owed is " + String.valueOf(getTotalCharge()) + "\n";
result += "You earned " + String.valueOf(getTotalfrequentRenterPoints()) + " frequent renter points";
return result;
}
/**
* Generates a statement detailing the customer's current rentals, billing information and frequent renter points
* @return a String containing the preformatted HTML statement
*/
public String htmlStatement() {
String result = "<h1>Rental Record for <em>" + getName() + "</em></h1><br />\n";
for (Rental each : rentals) {
result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(each.getCharge()) + "<br/>\n";
}
result += "<p>Amount owed is <em>" + String.valueOf(getTotalCharge()) + "</em></p>\n";
result += "<p>You earned <em>" + String.valueOf(getTotalfrequentRenterPoints()) + "</em> frequent renter points</p>";
return result;
}
private int getTotalfrequentRenterPoints() {
int result = 0;
for (Rental each : rentals) {
result += each.getFrequentRenterPoints();
}
return result;
}
private double getTotalCharge() {
double result = 0;
for (Rental each : rentals) {
result += each.getCharge();
}
return result;
}
public String getName() {
return name;
}
/**
* Adds a rental to the customer's current rentals list
* @param newRental the new rental
*/
public void addRental(Rental newRental) {
this.rentals.add(newRental);
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
package com.example.refactoring;
/**
* Movie for rent
*/
public class Movie {
/**
* The movie's complete title
*/
private String title = "";
/**
* The category this movie currently belongs to
*/
private MovieCategory category = MovieCategory.NEW_RELEASE;
/**
* @param title the movie's title
* @param category the movie's current category
*/
public Movie(String title, MovieCategory category) {
this.title = title;
this.category = category;
}
public String getTitle() {
return title;
}
public MovieCategory getCategory() {
return category;
}
public void setCategory(MovieCategory category) {
this.category = category;
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2
package com.example.refactoring;
/**
* Movie categories
*/
public enum MovieCategory {
/**
* A children's movie
*/
CHILDRENS(3, 1.5, 1.5, 1, 0),
/**
* A regular movie
*/
REGULAR(2, 2, 1.5, 1, 0),
/**
* A recently-released movie
*/
NEW_RELEASE(1, 3, 3, 1, 1);
/**
* The base number of days for the rental of a movie in this category
*/
private final int rentalBaseDuration;
/**
* The cost to rent a movie from this category for the standard number of days
*/
private final double rentalBaseCost;
/**
* Rental cost per day exceeding the base rental duration
*/
private final double rentalExtraDayCost;
/**
* Number of frequent renter points given for a rental not exceeding the standard number of days
*/
private final int rentalBasePoints;
/**
* Number of frequent renter points given if the rental exceeds the standard period
*/
private final int prolongedRentalExtraPoints;
/**
* @param rentalBaseDuration number of days the customer is allowed to keep the movie
* @param rentalBaseCost rental cost if the movie is returned on time
* @param rentalExtraDayCost cost per extra day
* @param rentalBasePoints number of frequent renter points given for renting a movie and returning it on time
* @param rentalExtraDayPoints number of extra frequent renter points given if the rental is prolonged
*/
private MovieCategory(int rentalBaseDuration, double rentalBaseCost, double rentalExtraDayCost, int rentalBasePoints, int
rentalExtraDayPoints) {
this.rentalBaseDuration = rentalBaseDuration;
this.rentalBaseCost = rentalBaseCost;
this.rentalExtraDayCost = rentalExtraDayCost;
this.rentalBasePoints = rentalBasePoints;
this.prolongedRentalExtraPoints = rentalExtraDayPoints;
}
public double getCharge(int duration) {
double result = this.rentalBaseCost;
if (duration > this.rentalBaseDuration) {
result += (duration - this.rentalBaseDuration) * this.rentalExtraDayCost;
}
return result;
}
public int getFrequentRenterPoints(int duration) {
int result = this.rentalBasePoints;
if ((this.prolongedRentalExtraPoints > 0) && duration > 1) {
result += this.prolongedRentalExtraPoints;
}
return result;
}
}
D’après FOWLER, Martin, Refactoring, 1999, Addison-Wesley. ISBN 0-201-48567-2

Documents pareils