FileDocCategorySizeDatePackage
BurritoInventoryServlet.javaAPI DocExample3211Tue Jan 25 10:45:14 GMT 2000None

BurritoInventoryServlet

public class BurritoInventoryServlet extends HttpServlet

Fields Summary
private int
cheese
private int
rice
private int
beans
private int
chicken
Constructors Summary
Methods Summary
public voidaddBeans(int added)

 beans += added; 
public voidaddCheese(int added)


  // Add to the inventory as more servings are prepared.
       cheese += added; 
public voidaddChicken(int added)

 chicken += added; 
public voidaddRice(int added)

 rice += added; 
public voiddestroy()

    saveState();
  
public voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)

    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    out.println("<HTML><HEAD><TITLE>Current Ingredients</TITLE></HEAD>");
    out.println("<BODY>");
    out.println("<TABLE BORDER=1>");
    out.println("<TR><TH COLSPAN=2>Current ingredients:</TH></TR>");
    out.println("<TR><TD>Cheese:</TD><TD>" + cheese + "</TD></TR>");
    out.println("<TR><TD>Rice:</TD><TD>" + rice + "</TD></TR>");
    out.println("<TR><TD>Beans:</TD><TD>" + beans + "</TD></TR>");
    out.println("<TR><TD>Chicken:</TD><TD>" + chicken + "</TD></TR>");
    out.println("</TABLE>");
    out.println("</BODY></HTML>");
  
public voidinit(javax.servlet.ServletConfig config)

    super.init(config);
    loadState();
  
public voidloadState()

    // Try to load the counts
    FileInputStream file = null;
    try {
      file = new FileInputStream("BurritoInventoryServlet.state");
      DataInputStream in = new DataInputStream(file);
      cheese = in.readInt();
      rice = in.readInt();
      beans = in.readInt();
      chicken = in.readInt();
      file.close();
      return;
    }
    catch (IOException ignored) {
      // Problem during read
    }
    finally {
      try { if (file != null) file.close(); } 
      catch (IOException ignored) { }
    }
  
public synchronized booleanmakeBurrito()

    // Burritos require one serving of each item
    if (cheese > 0 && rice > 0 && beans > 0 && chicken > 0) {
      cheese--; rice--; beans--; chicken--;
      return true;  // can make the burrito
    }
    else {
      // Could order more ingredients
      return false;  // cannot make the burrito
    }
  
public voidsaveState()

    // Try to save the counts
    FileOutputStream file = null;
    try {
      file = new FileOutputStream("BurritoInventoryServlet.state");
      DataOutputStream out = new DataOutputStream(file);
      out.writeInt(cheese);
      out.writeInt(rice);
      out.writeInt(beans);
      out.writeInt(chicken);
      return;
    }
    catch (IOException ignored) {
      // Problem during write
    }
    finally {
      try { if (file != null) file.close(); } 
      catch (IOException ignored) { }
    }