📄 exercise 5.xmlsalesformatter.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 XML format
* is used.
*
* @author Neil
* @version 1.1.0
* @see Order
* @see OrderItem
* @see Product
*/
public class XMLSalesFormatter implements SalesFormatter {
private static XMLSalesFormatter singletonInstance = null;
/**
* Static method that obtains the single instance of class
* XMLSalesFormatter.
*
* @return the single instance of class XMLSalesFormatter.
*/
public static XMLSalesFormatter getSingletonInstance() {
if (singletonInstance == null)
singletonInstance = new XMLSalesFormatter();
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 XMLSalesFormatter.
*/
private XMLSalesFormatter() {
}
/**
* Produces a string that contains the specified sales information in an XML
* format.
*
* @return a string that contains the specified sales information in an HTML
* format.
*/
public String formatSales(Sales sales) {
int k = 0;
String result = "<Sales>\n";
for (Iterator<Order> i = sales.iterator(); i.hasNext();) {
Order order = i.next();
k++;
result += " <Order total=\"" + order.getTotalCost() + "\">\n";
for (Iterator<OrderItem> j = order.iterator(); j.hasNext();) {
OrderItem orderItem = j.next();
result += " <OrderItem quantity=\"" + orderItem.getQuantity()
+ "\" price=\"" + orderItem.getProduct().getPrice()
+ "\">" + orderItem.getProduct().getCode()
+ "</OrderItem>\n";
}
result += " </Order>\n";
}
result += "</Sales>";
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -