📄 exercise 5.htmlsalesformatter.java
字号:
import java.util.*;
/**
* This class implements the interface SalesFormatter. This class is implemented
* as a singleton so a new object will not be created every time the HTML format
* is used.
*
* @author Neil
* @version 1.1.0
* @see Order
* @see OrderItem
* @see Product
*/
public class HTMLSalesFormatter implements SalesFormatter {
private static HTMLSalesFormatter singletonInstance = null;
/**
* Static method that obtains the single instance of class
* HTMLSalesFormatter.
*
* @return the single instance of class HTMLSalesFormatter.
*/
public static HTMLSalesFormatter getSingletonInstance() {
if (singletonInstance == null)
singletonInstance = new HTMLSalesFormatter();
return singletonInstance;
}
/**
* Constructor that is declared private so it is inaccessible to other
* classes. A private constructor makes it impossible for any other class to
* create an instance of class HTMLSalesFormatter.
*/
private HTMLSalesFormatter() {
}
/**
* Produces a string that contains the specified sales information in an
* HTML format.
*
* @return a string that contains the specified sales information in an HTML
* format.
*/
public String formatSales(Sales sales) {
int k = 0;
String result = "<html>\n <body>\n <center><h2>Orders</h2></center>\n";
for (Iterator<Order> i = sales.iterator(); i.hasNext();) {
Order order = i.next();
k++;
result += " <hr>\n <h4>Total = " + order.getTotalCost()
+ "</h4>\n";
for (Iterator<OrderItem> j = order.iterator(); j.hasNext();) {
OrderItem orderItem = j.next();
result += " <p>\n <b>code:</b> "
+ orderItem.getProduct().getCode()
+ "<br>\n <b>quantity:</b> "
+ orderItem.getQuantity()
+ "<br>\n <b>price:</b> "
+ orderItem.getProduct().getPrice() + "\n </p>\n";
}
}
result += " </body>\n</html>";
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -