**
* This class is implemented as a singleton.
* It displays the sales in a plain-text format.
*
* @author Panfei
*
*/
public class PlainTextSalesFormatter implements SalesFormatter {
/*The single instance */
static private PlainTextSalesFormatter singletonInstance = null;
private final static String NEW_LINE = System.getProperty("line.separator");
/**
* Static method that obtains the single instance.
*
* @return singletonInstance
*/
static public PlainTextSalesFormatter getSingletonInstance(){
if(singletonInstance == null){
singletonInstance = new PlainTextSalesFormatter();
}
return singletonInstance;
}
private PlainTextSalesFormatter(){}
/**
* Produces a string that contains the specified sales information in a plain-text format.
*
*/
public String formatSales(Sales sales){
String out = NEW_LINE;
int i = 1;
for (Order order : sales) {
out += "---------------------------";
out += NEW_LINE;
out += "Order" + " " + i+ NEW_LINE;
for (OrderItem item: order) {
out += item.getQuantity()+" " + item.getProduct().getCode() +" " +item.getProduct().getPrice()
+NEW_LINE;
}
i++;
out += NEW_LINE + "total=" + order.getTotalCost()+ NEW_LINE;
}
return out;
}
}