exercise 5.plaintextsalesformatter.java
来自「ssd3的全部答案」· Java 代码 · 共 69 行
JAVA
69 行
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 plain-text
* format is used.
*
* @author Neil
* @version 1.1.0
* @see Order
* @see OrderItem
* @see Product
*/
public class PlainTextSalesFormatter implements SalesFormatter {
private static PlainTextSalesFormatter singletonInstance = null;
/**
* Static method that obtains the single instance of class
* PlainTextsalesFormatter.
*
* @return the single instance of class PlainTextsalesFormatter.
*/
public static PlainTextSalesFormatter getSingletonInstance() {
if (singletonInstance == null)
singletonInstance = new PlainTextSalesFormatter();
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 PlainTextSalesFormatter.
*/
private PlainTextSalesFormatter() {
}
/**
* Produces a string that contains the specified sales information in a
* plain-text format.
*
* @return a string that contains the specified sales information in a
* plain-text format.
*/
public String formatSales(Sales sales) {
int k = 0;
String result = "";
for (Iterator<Order> i = sales.iterator(); i.hasNext();) {
Order order = i.next();
k++;
result += "\n------------------------\nOrder " + k + "\n\n";
for (Iterator<OrderItem> j = order.iterator(); j.hasNext();) {
OrderItem orderItem = j.next();
result += orderItem.getQuantity() + " "
+ orderItem.getProduct().getCode() + " "
+ orderItem.getProduct().getPrice() + "\n";
}
result += "\nTotal = " + order.getTotalCost();
}
return result;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?