001package edu.pdx.cs410J.net;
002
003/**
004 * This class models a <code>McDonalds</code>.  There are a bunch of
005 * <code>McCustomer</code>s who all want a BigMac(tm).  There are a
006 * bunch of liberal arts majors, er, <code>McEmployee</code>s who cook
007 * the BigMacs(tm).  Each <code>McCustomer</code> and
008 * <code>McEmployee</code> runs in his or her own thread.
009 */
010public class McDonalds {
011  private static java.io.PrintStream err = System.err;
012  private int nBigMacs;
013
014  /**
015   * Creates a new <code>McDonalds</code> with a given number of
016   * BigMacs to cook.
017   */
018  public McDonalds(int nBigMacs) {
019    this.nBigMacs = nBigMacs;
020  }
021
022  /**
023   * Returns <code>true</code> if there are more BigMacs to cook.
024   */
025  public synchronized boolean moreBigMacs() {
026    if (this.nBigMacs <= 0) {
027      return false;
028
029    } else {
030      this.nBigMacs--;
031      return true;
032    }
033  }
034
035  /**
036   * Read the number of <code>McCustomer</code>s and the number of
037   * <code>McEmployee</code>s from the command line.  Spin off threads
038   * for each one and what minimum wage at work.
039   */
040  public static void main(String[] args) {
041    int nCustomers = 0;
042    int nEmployees = 0;
043
044    try {
045      nCustomers = Integer.parseInt(args[0]);
046      nEmployees = Integer.parseInt(args[1]);
047
048    } catch (NumberFormatException ex) {
049      err.println("** NumberFormatException");
050      System.exit(1);
051    }
052
053    // Each customer wants a BigMac(tm)
054    McDonalds mcDonalds = new McDonalds(nCustomers);
055
056    // The customers enter...
057    for (int i = 0; i < nCustomers; i++) {
058      McCustomer customer = new McCustomer(i, mcDonalds);
059      (new Thread(customer)).start();
060    }
061
062    // The employees start cooking...
063    for (int i = 0; i < nEmployees; i++) {
064      McEmployee employee = new McEmployee(i, mcDonalds);
065      (new Thread(employee)).start();
066    }
067
068    // Our work here is done.
069  }
070}