Skip to search.

Breaking News Visit Yahoo! News for the latest.

×Close this window

  1. Home >
  2. All Categories >
  3. Computers & Internet >
  4. Programming & Design >
  5. Resolved Question
ethridgematthew ethridge...
Member since:
April 02, 2008
Total points:
1,191 (Level 3)

Resolved Question

Show me another »

Implement Gas Station in Java?

I have to write a program to implement a gas station in java. The gas station has 5 pumps and each pump has a queue that the cars wait in if the pump is occupied. The cars arrive at random intervals and join an empty pump or the shortest queue. The program ask the user for the run time. I know how to write the program but how do i create the cars in the programs loop and keep track of them?
wolfgangmeyers by wolfgang...
Member since:
July 25, 2006
Total points:
959 (Level 2)

Best Answer - Chosen by Voters

This sounds like a question about data structures. And a fun project :)
I think the most straightforward way would to have a single thread "generating" the cars. For each pump, you can store a java.util.Queue<Car> to keep track of them. You can either have a seperate thread for each pump or have a scheduler thread of some sort (maybe the same one as generating the cars). This way you can "enqueue" the cars from the producer thread and "dequeue" them in the consumer thread(s).

Sample for creation:

Thread creator = new Thread(new Runnable(){

public void run(){
while(programRunning){
Car car = createRandomCar();
Pump pump = choosePump();
car.goto(pump); //car implementation enqueues itself here in the pump
Thread.currentThread().sleep(Math.rand… 20000));
}
}

});
creator.start();

//continue in program
100% 1 Vote

There are currently no comments for this question.

Other Answers (0)

No other answers.

Answers International

Yahoo! does not evaluate or guarantee the accuracy of any Yahoo! Answers content. Click here for the Full Disclaimer.

Help us improve Yahoo! Answers. Send Feedback