Alright so I was having this issue with my receipt function for my restaurant POS system. I take a table's orders, print the receipt.. easy right. Well, it requires some HTML formatting which I suck at but unfortunately am forced to use right now.
So, got that down. The function I wrote to look at a table's orders and print the receipt, say you're given:
Mozzarella Sticks 5.00
Mozzarella Sticks 5.00
M Pepperoni Pizza 6.00
L Pepperoni Pizza 7.00
the receipt is supposed to print something like,
Mozzarella Sticks 2 @ $5.00
M Pepperoni Pizza 1 @ $6.00
L Pepperoni Pizza 1 @ $6.00
Well this was a way bigger pain in the ass than i think it should have been, basically because of how I've arranged my data I think.
I made a MenuItem class, which started out with 2 basic variables, String representing the name, and a double for the price. I have a few other member variables and some methods now but that's how it started off. There is a map that contains the entire menu with the item name as the key and the price as the value for easy retrieval.
So, to represent each table's unique order, I have a Vector<MenuItem> storing (sequentially) the orders for the table.
The problem comes when trying to count how many identical items there are. I thought of a few solutions, but this is the best I could come up with:
Code:
public String printOrdersHTML()
{
String r = "";
Map<String, Double> map = new TreeMap<String, Double>();
Vector<String> v = new Vector<String>();
for (MenuItem m : orders)
{
map.put(m.getItemName(), m.getItemPrice());
v.add(m.getItemName());
}
for (String key : map.keySet())
{
r += "<tr><td>" + key+ "</td><td align =\"right\">"
+ Integer.toString(Collections.frequency(v, key))
+ String.format(" @ $%4.2f", map.get(key)) + "</td></tr>";
}
return r;
}
I feel like this is horrific and ugly and probably really inefficient but it's by far the cleanest thing I've come up with for a problem I feel should be really simple. Any thoughts?
Last edited by jmakin; 05-09-2015 at 05:59 AM.