/* play the game */ private void play() { t0 = System.currentTimeMillis(); /*Times the beginning of the game */ currentTime = System.currentTimeMillis() - t0; generateRandomOrderTimes(); System.out.println("next t1 is in " + orderTimes.get(0)); System.out.println("next t2 is in " + orderTimes.get(1)); System.out.println("next t3 is in " + orderTimes.get(2)); while (System.currentTimeMillis() - t0 < ENDOFGAMETIME) { pause(100); /*Update the days label*/ daysElapsed = (int) ((System.currentTimeMillis() - t0) / (LENGTHOFADAY * 1000)); daysElapsedLabel.setLabel("DAYS " + daysElapsed); scanEquipments(); } }
/*setup the game */ private void setup() { t0 = System.currentTimeMillis(); /*Times the beginning of the game */ // int size; Scanner in = new Scanner(System.in); // System.out.println("Enter the size of available"); // size = in.nextInt(); initiateScores(); placeWalls(); placeStates(); placeLabels(); randomizeOrderRent(); /*randomizes the order of the equipment for the On Rent state */ System.out.println( "How many High Runners at price $" + EQUIPMENTCOSTS[0] + " do you want to buy?"); int type1Equip = in.nextInt(); System.out.println( "How many Medium Runners at price $" + EQUIPMENTCOSTS[1] + " do you want to buy?"); int type2Equip = in.nextInt(); System.out.println( "How many Low Runners at price $" + EQUIPMENTCOSTS[2] + " do you want to buy?"); int type3Equip = in.nextInt(); capitalInvested = EQUIPMENTCOSTS[0] * type1Equip + EQUIPMENTCOSTS[1] * type2Equip + EQUIPMENTCOSTS[2] * type3Equip; capitalLabel.setLabel("Capital Invested: $" + capitalInvested); fillAvailable(type1Equip, type2Equip, type3Equip); // fills with the proper number of equipment // fillStates(size,INITRENT,INITSHOP); placeEquipments(); }
/*compute the initial Time outs for all equipments */ private void computeInitialTimes() { System.out.println("t0 is" + t0); double timeDiff = 0; /*Times for available equipment will depend on the type and position in queue as it's FIFO */ generateRandomOrderTimes(); /*for equipment on rent, it's simply at the end of the rental period */ for (Equipment e : rentEquipment) { e.timeIn = System.currentTimeMillis() - t0; timeDiff = nextRandomTime(RENTALFREQUENCY[e.type - 1]); e.timeOut = System.currentTimeMillis() + timeDiff - t0; System.out.println("I am in state " + e.state + " and order " + rentEquipment.indexOf(e)); System.out.println( "My time in is " + e.timeIn + " And my time out is " + e.timeOut + " and the difference between the 2 is " + (e.timeOut - e.timeIn)); /*System.out.println("and nextRandomTime returns" + nextRandomTime(e.timeIn,RENTALFREQUENCY[e.type-1]) +" for type "+e.type+" for RentalFrequency "+RENTALFREQUENCY[e.type-1]); System.out.println("For equipment in state "+e.state+" and order "+rentEquipment.indexOf(e)+" : inter-arrival is "+(e.timeOut-e.timeIn)); */ } /*for equipment in the shop, it's simply at the end of the rental period */ for (Equipment e : shopEquipment) { e.timeIn = System.currentTimeMillis() - t0; timeDiff = nextRandomTime(SHOPFREQUENCY[e.type - 1]); e.timeOut = System.currentTimeMillis() + timeDiff - t0; System.out.println("I am in state " + e.state + " and order " + shopEquipment.indexOf(e)); System.out.println( "My time in is " + e.timeIn + " And my time out is " + e.timeOut + " and the difference between the 2 is " + (e.timeOut - e.timeIn)); /*System.out.println("For equipment in state "+e.state+" and order "+shopEquipment.indexOf(e)+" : inter-arrival is "+(e.timeOut-e.timeIn)); */ } }
/*Scans the particular equipment and decides whether or not it shoud be moved */ private int scan(Equipment equip) { double currentTime = System.currentTimeMillis() - t0; int orderMoved = -1; int or = -1; switch (equip.state) { case 1: or = availEquipment.indexOf(equip); break; case 2: or = rentEquipment.indexOf(equip); break; case 3: or = shopEquipment.indexOf(equip); break; } // System.out.println("Now looking at state "+equip.state+" and type "+equip.type+" and order" // +or); // System.out.println("Current time is "+currentTime+" while equip.timeOut time is // "+equip.timeOut+" and the difference is "+(equip.timeOut-currentTime)); switch (equip.state) { case 1: break; case 2: if (equip.timeOut < System.currentTimeMillis() - t0) { orderMoved = rentEquipment.indexOf(equip); return orderMoved; } break; case 3: if (equip.timeOut < System.currentTimeMillis() - t0) { orderMoved = shopEquipment.indexOf(equip); // System.out.println("I have just moved equipment from state 3 "+equip.state+" of type // "+equip.type+" and of order "+shopEquipment.indexOf(equip)); return orderMoved; } break; } return orderMoved; }
/* Generates random order times for the orders of each type in the Available state */ private void generateRandomOrderTimes() { orderTime1 = System.currentTimeMillis() + nextRandomTime(ARRIVAL1) - t0; orderTime2 = System.currentTimeMillis() + nextRandomTime(ARRIVAL2) - t0; orderTime3 = System.currentTimeMillis() + nextRandomTime(ARRIVAL2) - t0; double[] arrivalFrequencies = {ARRIVAL1, ARRIVAL2, ARRIVAL3}; orderTimes.add(System.currentTimeMillis() + nextRandomTime(ARRIVAL1) - t0); orderTimes.add(System.currentTimeMillis() + nextRandomTime(ARRIVAL2) - t0); orderTimes.add(System.currentTimeMillis() + nextRandomTime(ARRIVAL3) - t0); /*for (double counter : arrivalFrequencies) { System.out.println("t0 is = "+t0+" and current time is "+currentTime); currentTime=System.currentTimeMillis()-t0; orderTimes.add(System.currentTimeMillis()+nextRandomTime(counter)-t0); }*/ }
/*Scans all equipments and identifies the ones that need to be moved */ private void scanEquipments() { double timeDiff = 0; int orderToMove = -1; double[] arrivalFrequencies = {ARRIVAL1, ARRIVAL2, ARRIVAL3}; /*In the available state, the scan should not happen on the equipment as it's external customer demand and we should record a "lost sale" when there is no equipment*/ for (int counter = 0; counter < TYPES; counter++) { // System.out.println("I am computing for arrival frequency "+arrivalFrequencies[counter]+ " // and the corresponding time is "+orderTimes.get(counter)); if (orderTimes.get(counter) < System.currentTimeMillis() - t0) { System.out.println("Time is " + System.currentTimeMillis()); orderTimes.set( counter, System.currentTimeMillis() + nextRandomTime(arrivalFrequencies[counter]) - t0); System.out.println( "I have just computed a new exit time for state 1 and type " + counter + " = " + orderTimes.get(counter)); orderToMove = checkOrder( 1, counter + 1); // checks the order of the type being moved in the available arraylist if (orderToMove >= 0) { Equipment e = availEquipment.get(orderToMove); timeDiff = nextRandomTime( RENTALFREQUENCY[ e.type - 1]); // Equipment is moving to rent - so its next timeOut should be that // of rent e.timeOut = System.currentTimeMillis() + timeDiff - t0; moveEquip(1, orderToMove); sales = sales + RATES[counter]; salesLabel.setLabel("SALES : $" + sales); } else { System.out.println("You just lost a sale"); int ls = lostSales.get(counter); lostSales.set(counter, ls + 1); switch (counter) { case 0: lostSalesLabel1.setLabel("Lost Sales HR = " + ls + 1); break; case 1: lostSalesLabel2.setLabel("Lost Sales MR = " + ls + 1); break; case 2: lostSalesLabel3.setLabel("Lost Sales LR = " + ls + 1); break; } } } } for (Equipment e : rentEquipment) { orderToMove = scan(e); if (orderToMove >= 0) { /*Entering this loop means there is an equipment to move */ timeDiff = nextRandomTime( SHOPFREQUENCY[ e.type - 1]); // Equipment is moving to shop so its next timeOut is that of shop // System.out.println("Rental frequency is "+RENTALFREQUENCY[e.type-1]+" and timeDiff is // "+timeDiff); e.timeOut = System.currentTimeMillis() + timeDiff - t0; System.out.println("the new timeOut is " + e.timeOut); break; } } if (orderToMove >= 0) { moveEquip(2, orderToMove); // System.out.println("I have just moved equipment from state 2 and of order "+orderToMove); // placeEquipments(); } for (Equipment e : shopEquipment) { orderToMove = scan(e); if (orderToMove >= 0) { /*Entering this loop means there is an equipment to move */ e.timeOut = 0; // Doesn't matter what the time-out is as it's not governed by the equipment break; } } if (orderToMove >= 0) { moveEquip(3, orderToMove); // placeEquipments(); } }