📄 simbusstop2.java
字号:
/** * Title: SimBusStop2.java * Description: A simple Bus Stop simulation using Queue * Events are generated randomly */import miscLib.SimpleInput;import miscLib.GenLib;public class SimBusStop2 { public static void main(String[] args) { ListQueue t = new ListQueue();// ArrayQueue t = new ArrayQueue(50); int code, time, count; Integer item; int totalPeople = 0; int totalBus = 0; int totalVacantSeat = 0; int outstandingWait = 0; int people = 0; int wait = 0; int period; // the time period in minutes for simulation int eventInterval; // interval of event arrival int peopleLB; // people count - lower bound int peopleUB; // people count - upper bound int busLB; // bus capacity - lower bound int busUB; // bus capacity - upper bound period = SimpleInput.getInteger("Enter total duration in minutes: "); eventInterval = SimpleInput.getInteger("Enter event interval in minutes: "); peopleLB = SimpleInput.getInteger("Enter people count (lower bound): "); peopleUB = SimpleInput.getInteger("Enter people count (upper bound): "); busLB = SimpleInput.getInteger("Enter bus available seats (lower bound): "); busUB = SimpleInput.getInteger("Enter bus available seats (upper bound): "); // determine for an event every eventInterval count = 0; for (int lastTime = 0; lastTime <= period; ) { code = GenLib.genInt(-1, 1); // 0:people, 1:bus, -1:no event time = GenLib.genInt(lastTime + 1, lastTime + eventInterval); // event time if (time > period) break; // stop the loop if time expired lastTime = time; // update the time for next event if (code != -1) { // it's an event, let's generate the count of the triplet if (code == 0) { count = GenLib.genInt(peopleLB, peopleUB); // how many people arrived System.out.println(count + " people arrived at time " + time); } else { count = GenLib.genInt(busLB, busUB); // how many seats available System.out.println("Bus with " + count + " seats arrived at time " + time); } } if (code == 0) { // people arrived and line up in the queue totalPeople += count; while (count-- > 0) t.enqueue (new Integer(time)); } else if (code == 1) { // bus arrived and people got on bus from the queue totalBus++; while ( (count > 0) && !t.empty ()) { item = (Integer) t.dequeue (); wait += time - item.intValue(); // now, we know how long been waiting people++; count--; } totalVacantSeat += count; } } // check # of people got on a bus if (people != 0) { // some people got on the bus System.out.println (people + " people waited for " + wait + " minutes, average waiting time = " + (float) wait/people + " minutes."); } else { // no one got on the bus System.out.println ("0 people go on the bus."); } // check # o buses passed the bus stop if (totalBus != 0) { // some buses did pass the bus stop System.out.println ("Number of buses passed the bus stop = " + totalBus); System.out.println ("Average number of people got on a bus = " + (float) people/totalBus); System.out.println ("Average number of vacant seats for a bus = " + (float) totalVacantSeat / totalBus ); } else { // no bus passed the bus stop System.out.println("0 bus passed the bus stop."); } // check # of people still waiting at the bus stop System.out.print ("Number of people still waiting = " + (totalPeople - people) ); if (!t.empty()) { while (!t.empty ()) { item = (Integer) t.dequeue (); outstandingWait += period - item.intValue(); } System.out.println(" with average waiting time = " + (float) outstandingWait / (totalPeople - people) + " minutes"); } else { System.out.println(" with average waiting time = 0.0 minute"); } System.exit(0); } // main} // class SimBusStop2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -