/*Moves equipment from State s and Order o to the next State */ private void moveEquip(int s, int o) { Equipment movedEquip = new Equipment(); switch (s) { case 1: movedEquip = availEquipment.get(o); movedEquip.state = 2; rentEquipment.add(movedEquip); availEquipment.remove(o); /*removes the actual equipment from the Arraylist */ break; case 2: movedEquip = rentEquipment.get(o); movedEquip.state = 3; shopEquipment.add(movedEquip); rentEquipment.remove(movedEquip); break; case 3: movedEquip = shopEquipment.get(o); movedEquip.state = 1; availEquipment.add(movedEquip); shopEquipment.remove(movedEquip); break; } eraseOldShapes(); assignShapes(); placeEquipments(); }
/*Creates the equipment in all states to initiate the game */ private void fillStates(int sAvail, int sRent, int sShop) { int[] statesSizes = {sAvail, sRent, sShop}; int s = 1; for (int stateSize : statesSizes) { for (int t = 1; t <= TYPES; t++) { for (int i = 1; i <= stateSize; i++) { Equipment e = new Equipment(); e.state = s; e.type = t; int sMax = Math.max( s - 2, 0); /*formula to make sure identifiers resume counting and stay unique */ int sMin = Math.min( s - 1, 1); /*formula to make sure identifiers resume counting and stay unique */ e.ident = i + (sMin) * sAvail + (sMax) * sRent; e.c = equipColor(e.type); switch (s) { case 1: availEquipment.add(e); break; case 2: rentEquipment.add(e); break; case 3: shopEquipment.add(e); break; } } } s++; } computeInitialTimes(); assignShapes(); }
/* Assign shapes to the Equipment based on state, type and order */ private void assignShapes() { int order = 0; int x = 0; int y = 0; /*Assign shapes to Equipment in available status (organized by types) */ for (Equipment e : availEquipment) { order = checkMyOrder(e); x = getX(e.state, e.type, order); y = getY(e.state, e.type, order); e.shape = new GRect(x, y, EQUIPWIDTH, EQUIPHEIGHT); e.shape.setFilled(true); e.shape.setColor(e.c); e.label = new GLabel("" + e.ident, x + EQUIPVERTGAP / 2, y + EQUIPWIDTH); } /*Assign shapes to Equipment in onRent status */ for (Equipment e : rentEquipment) { order = rentEquipment.indexOf(e); x = getX(e.state, e.type, order); y = getY(e.state, e.type, order); e.shape = new GRect(x, y, EQUIPWIDTH, EQUIPHEIGHT); e.shape.setFilled(true); e.shape.setColor(e.c); e.label = new GLabel("" + e.ident, x + EQUIPVERTGAP / 2, y + EQUIPWIDTH); } for (Equipment e : shopEquipment) { order = shopEquipment.indexOf(e); x = getX(e.state, e.type, order); y = getY(e.state, e.type, order); e.shape = new GRect(x, y, EQUIPWIDTH, EQUIPHEIGHT); e.shape.setFilled(true); e.shape.setColor(e.c); e.label = new GLabel("" + e.ident, x + EQUIPVERTGAP / 2, y + EQUIPWIDTH); } }
/*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 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(); } }
/*Fills the Available state */ private void fillAvailable(int t1, int t2, int t3) { /*Fill the type1 in the available state */ for (int i = 1; i <= t1; i++) { Equipment e = new Equipment(); e.state = 1; e.type = 1; e.ident = i; e.c = equipColor(e.type); availEquipment.add(e); } /*Fill the type2 in the available state */ for (int i = 1; i <= t2; i++) { Equipment e = new Equipment(); e.state = 1; e.type = 2; e.ident = i; e.c = equipColor(e.type); availEquipment.add(e); } /*Fill the type3 in the available state */ for (int i = 1; i <= t3; i++) { Equipment e = new Equipment(); e.state = 1; e.type = 3; e.ident = i; e.c = equipColor(e.type); availEquipment.add(e); } computeInitialTimes(); assignShapes(); }