// Project 5. Question 3. Solution // Cafeteria cleanup simulation (with arrays) // Date: 25 March 2001 // Author: Rimon Barr public class Simulation { // input stream private static TokenReader in=new TokenReader(System.in); public static void main(String[] args) { // read belt length from user System.out.print("Please enter belt length >1: [4]"); int length=in.readInt(); if(length<=0) length=4; // run simulation Belt b=new Belt(length); int run=1; int total=0; while(!b.isJammed()) { System.out.println("Current run: "+run++); b.shift(); System.out.println("Old contents: "+b); total+=b.process(); System.out.println("New contents: "+b); System.out.println("Total items taken, so far: "+total); } System.out.println("The workers are overloaded; belt jammed!"); System.out.println("Final belt: "+b); } } class Belt { // workers and trays private Tray[] trays; private Worker[] workers; // create new belt of n spots public Belt(int n) { trays=new Tray[n]; workers=new Worker[n]; for(int i=0; i0; i--) trays[i]=trays[i-1]; trays[0]=new Tray(); } // determine whether belt is jammed / last tray not empty public boolean isJammed() { return !trays[trays.length-1].isEmpty(); } // string representation of belt public String toString() { String s=""; for(int i=0; iMAX) items=MAX; if(itemsitems) i=items; items-=i; return i; } }