/** @return XML representation of cart contents */ public String toXml() { StringBuffer xml = new StringBuffer(); xml.append("<?xml version=\"1.0\"?>\n"); // Append the current system time to check for latest async calls xml.append( "<cart generated=\"" + System.currentTimeMillis() + "\" total=\"" + getCartTotal() + "\">\n"); // Generate xml tags and values by iterating through the contents for (Iterator<Item> I = contents.values().iterator(); I.hasNext(); ) { Item item = I.next(); // int itemQuantity = contents.get(item).intValue(); xml.append("<item code=\"" + item.getCode() + "\">\n"); xml.append("<name>"); xml.append(item.getName()); xml.append("</name>\n"); // xml.append("<quantity>"); // xml.append(itemQuantity); // xml.append("</quantity>\n"); xml.append("</item>\n"); } // xml.append("<feed>").append(cartXml).append("</feed>\n"); xml.append("</cart>\n"); return xml.toString(); }
private static void fillAndPrintMap(Map<String, Item> map) { Item item; item = new Item("IPQ", "IPAQ", 22); map.put(item.getCode(), item); item = new Item("PPC", "POCKET PC", 33); map.put(item.getCode(), item); item = new Item("SPC", "SUPER PC", 44); map.put(item.getCode(), item); item = new Item("IPQ", "IPAQ2", 55); map.put(item.getCode(), item); System.out.print(map); System.out.println(":" + ((Object) map).getClass().getName()); }
/** @return JSON representation of cart contents */ public String toJson() { StringBuffer json = new StringBuffer(); // Append the current system time to check for latest async calls json.append("{\"cart generated\":" + System.currentTimeMillis() + ","); json.append("\"total\":\"" + getCartTotal() + "\","); // Generate json array of json objects by iterating through the contents json.append("\"items\":["); for (Iterator<Item> I = contents.values().iterator(); I.hasNext(); ) { Item item = I.next(); // int itemQuantity = contents.get(item).intValue(); json.append("{"); json.append("\"item code\":\"" + item.getCode() + "\","); json.append("\"name\":\""); json.append(item.getName()).append("\"},"); // json.append("\"quantity\":"); // json.append(itemQuantity).append("},"); } // Remove , if it exists at the end of json for correcting the syntax if (json.toString().endsWith(",")) { json.setLength(json.length() - 1); } json.append("]}"); return json.toString(); }