concurrency-problems

git clone git://git.codymlewis.com/concurrency-problems.git
Log | Files | Refs | README

Customer.java (2242B)


      1 /**
      2  * Customer - COMP2240
      3  * An ice cream parlour customer
      4  *
      5  * @author Cody Lewis (c3283349)
      6  */
      7 
      8 public class Customer extends Thread implements Comparable<Customer> {
      9     private String id;
     10     private int arrivalTime;
     11     private int eatingTime;
     12     private int finishEatingTime;
     13     private Parlour parlour;
     14     private String stats;
     15 
     16     /**
     17      * Input Constructor
     18      *
     19      * @param arrivalTime Time that the customer arrives at the parlour
     20      * @param id id of the customer
     21      * @param eatingTime Amount of time that the customer takes to eat
     22      * @param parlour The ice cream parlour that the customer will eat at
     23      */
     24     public Customer(int arrivalTime, String id, int eatingTime, Parlour parlour) throws Exception {
     25         this.id = id;
     26         this.arrivalTime = arrivalTime;
     27         this.eatingTime = eatingTime;
     28         this.parlour = parlour;
     29         this.stats = String.format("%s\t\t%d\t", id, arrivalTime);
     30         parlour.addEvent(this);
     31     }
     32 
     33     /**
     34      * Arrive at the parlour
     35      */
     36     public void arrive() throws Exception {
     37         this.stats += String.format("%d\t", parlour.takeSeat(this));
     38     }
     39 
     40     /**
     41      * Start eating at the parlour
     42      */
     43     public void startEating(int time) {
     44         this.stats += String.format("%d\t", time);
     45     }
     46 
     47     /**
     48      * Leave the parlour
     49      */
     50     public int leave() throws Exception {
     51         return parlour.leave(this);
     52     }
     53 
     54     /**
     55      * Get the summary of what the customer has done
     56      */
     57     public String summary() {
     58         return stats;
     59     }
     60 
     61     /**
     62      * Get the id of this
     63      *
     64      * @return this Customer's id
     65      */
     66     public String getID() { return id; }
     67 
     68     /**
     69      * Arrive at the parlour, eat, then leave
     70      */
     71     public void run() {
     72         try {
     73             Thread.sleep(arrivalTime * Timer.QUANTUM + Timer.EPSILON);
     74             arrive();
     75             Thread.sleep(eatingTime * Timer.QUANTUM + Timer.EPSILON);
     76             this.stats += leave();
     77         } catch (Exception e) {
     78             System.out.format("Error: %s", e.getMessage());
     79         }
     80     }
     81 
     82     /**
     83      * Compare the id of this customer to another
     84      */
     85     public int compareTo(Customer other) {
     86         return id.compareTo(other.getID());
     87     }
     88 }