SalesFormatter 接口
HTMLSalesFormatter 类
PlainTextSalesFormatter 类
XMLSalesFormatter 类
GourmatCoffer 类 (仅粘贴需要自己实现的方法)
SalesFormatter 接口
HTMLSalesFormatter 类
PlainTextSalesFormatter 类
public interface SalesFormatter {
public String formatSales(Sales sales);
}
public class HTMLSalesFormatter implements SalesFormatter {
static private HTMLSalesFormatter singletonInstance= new
HTMLSalesFormatter();
private HTMLSalesFormatter() {
}
static public HTMLSalesFormatter getSingletonInstance() {
return HTMLSalesFormatter.singletonInstance;
}
@Override
public String formatSales(Sales sales) {
double sum = 0.0;
String ans = "<html>" + " " + "<body>" + " " + "<center>
<h2>Orders</h2></center>";
for (Order ord : sales) {
sum += ord.getTotalCost();
}
ans += "<hr><h4>Total = " + sum + "</h4>";
for (Order ord : sales) {
ans += " <p>";
for (OrderItem ordit : ord) {
Product pro = ordit.getProduct();
ans += " <b>code:</b> " + pro.getCode() + "<br>" +
" <b>quantity:</b> " + ordit.getQuantity() + "<br>" +
" <b>price:</b> " + pro.getPrice() + " </p>";
}
}
return ans + " </body> </html>";
}
}
public class PlainTextSalesFormatter implements SalesFormatter {