Reputation: 85
I am working on a Restaurant Menu Assignment. For this assignment, I need to let the customer make an order. As for teacher's request, I need to use <forEach:>
to loop through my menu items, set limited quantity option, use "${item.itemNo}"
to identify which item is ordered and calculate the order.
<table>
<tr>
<th><b>Number</b></th>
<th><b>Description</b></th>
<th><b>Price</b></th>
<th><b>Quantity</b></th>
</tr>
<c:forEach var="item" items="${sessionScope.menuList}">
<%-- <c:set var="quantity" scope = "session" value = ""/> --%>
<tr>
<td><c:out value="${item.itemNo}" /></td>
<td><c:out value="${item.itemDesc}" /></td>
<td><fmt:formatNumber type="currency">
<c:out value="${item.itemPrice}" />
</fmt:formatNumber></td>
<td><select name="${item.itemNo}">
<option value="0"></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select></td>
</tr>
</c:forEach>
<tr>
<td></td>
<td></td>
<td></td>
<td><input type="submit" name="submitOrder" value="Order"></td>
</tr>
</table>
My menu order page looks like this
Part of my MenuItem class
public class MenuItem implements Comparable<MenuItem>, Serializable{
private static final long serialVersionUID = 1L;
private int itemNo;
private String itemDesc;
private double itemPrice;
private int quantity;
public MenuItem() {
super();
}
public MenuItem(int itemNo, String itemDesc, double itemPrice ) {
this.setItemNo(itemNo);
this.setItemDesc(itemDesc);
this.setItemPrice(itemPrice);
this.setQuantity(0);
}
My MenuManager that generates all menu items, hard coded:
public class MenuManager {
private static MenuManager theMenu = null;
private Map<Integer, MenuItem> menu = null;
private List<MenuItem> sortedMenu = null;
synchronized public static MenuManager getInstance() {
if ( theMenu == null ) {
theMenu = new MenuManager();
}
return theMenu;
}
private MenuManager() {
menu = new ConcurrentHashMap<Integer, MenuItem>();
menu.put( 22, new MenuItem( 22, "Egg Drop Soup", 3.00));
menu.put( 14, new MenuItem( 14, "WonTon Noodle Soup", 5.00));
menu.put( 6, new MenuItem( 6, "Egg Roll", 1.20));
menu.put( 17, new MenuItem( 17, "Spring Roll", 1.70));
menu.put(207, new MenuItem(207, "Roast Duck", 16.00));
menu.put( 77, new MenuItem( 77, "Shrimp with Snow Pea", 10.50));
menu.put( 98, new MenuItem( 98, "Beef with Brocolli", 8.40));
menu.put(108, new MenuItem(108, "Beef Ginger", 9.00));
menu.put(210, new MenuItem(210, "Moo Goo Stir Fry", 7.50));
menu.put(123, new MenuItem(123, "Lemon Chicken", 9.25));
menu.put(114, new MenuItem(114, "Sweet&Sour Ribs", 6.50));
menu.put(132, new MenuItem(132, "Sweet&Sour Chicken Balls", 8.60));
menu.put( 50, new MenuItem( 50, "Boiled Rice", 1.50));
menu.put( 63, new MenuItem( 63, "Vegetable Fried Rice", 3.00));
sortedMenu = new LinkedList<MenuItem>();
sortedMenu.addAll( menu.values() );
Collections.sort( sortedMenu );
}
public List<MenuItem> getMenu() {
return sortedMenu;
}
public MenuItem getMenuItem( Integer key ) {
return menu.get(key);
}
public static void main( String[] args) {
MenuManager mm = MenuManager.getInstance();
for ( MenuItem mi : mm.getMenu() ) {
System.out.println( mi);
}
}
}
After customer placed an order, next page should look like this:
But in my serlvet, whenever I call request.getParameter("${item.itemNo}")
, it is always null. How do I store which item the user ordered and the quantity of it ?
Upvotes: 0
Views: 684
Reputation: 10707
Instead of request.getParameter("${item.itemNo}")
you should be checking this with some real number from MenuManager. You are naming your selected field after itemNo, which is number.
To check all responses you should do it following way:
for(MenuItem menuItem : MenuManager.getInstance().getMenu()){
String value = request.getParameter("" + menuItem.getItemNo());
//do some processing with value
}
Upvotes: 1