说明:
通过对计算逻辑的提炼,可以完成一个HtmlStatement(),并复用原本Statement()内的所有计算。
代码:
package Movie_Ref;
import java.util.Enumeration;
import java.util.Vector;
public class Customer {
private String _name;//姓名
private Vector _rentals = new Vector(); //租借记录
public Customer(String name) {
_name = name;
}
public void addRental(Rental obj) {
_rentals.addElement(obj);
}
public String get_name() {
return _name;
}
private int getTotalFrequentRenterPoints() {
int result = 0; //--常客积点
Enumeration rentals = _rentals.elements();
while(rentals.hasMoreElements()){
Rental each = (Rental) rentals.nextElement(); //--取得一笔租借记录
//---累加常客积点
result += each.getFrequentRenterPoints();
}
return result;
}
private double getTotalCharge() {
double result = 0; //--总消费金额
Enumeration rentals = _rentals.elements();
while(rentals.hasMoreElements()){
Rental each = (Rental) rentals.nextElement(); //--取得一笔租借记录
result += each.getCharge();
}
return result;
}
public String statement() {
Enumeration rentals = _rentals.elements();
String result = "Rental Record for " + get_name() + "/n";
while(rentals.hasMoreElements()){
Rental each = (Rental) rentals.nextElement(); //--取得一笔租借记录
//---显示此笔租借数据
result += "/t" + each.get_movie().get_title() + "/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 = " /n";Rentals for " + get_name() + "
while(rentals.hasMoreElements()){
Rental each = (Rental) rentals.nextElement(); //--取得一笔租借记录
//---显示此笔租借数据
result += each.get_movie().get_title() + ": " +
String.valueOf(each.getCharge()) + "
/n";
}
//---结尾打印
result += " You owe " + String.valueOf(getTotalCharge()) + " /n";
result += "On this rental you earned " + String.valueOf(getTotalFrequentRenterPoints()) +
" frequent renter points ";
return result;
}
}