Petrol Station Simulator in Ruby

Yes! My simulator speed issue from the previous post is now solved! In this post I just want to show the simulator’s output and remind you how important it is to have just-for-fun side projects. One way to do that is by experimenting with Petrolex Station Simulator, all within the comfort of your primary coding language.

What is the Petrolex?

Imagine a virtual playground where you can create and explore a pretend petrol station. The Petrolex Station Simulator is just that—a project built using Ruby, designed to mimic the operations of a petrol station.

How Does It Work?

Picture this: cars arriving, waiting in line, refueling, and then moving on. The simulator handles all these actions in separate threads, synchronized by a timer. Your job is to tweak the setup to reduce wait times and optimize efficiency.

The code

You can find the project here: https://github.com/vysogot/petrolex

Sample output

Petrolex Station Simulator has started.

Simulation speed: x1000
Closing tick: 400

Cars to arrive: 5
Station fuel reserve: 300
Dispenser fueling speed: 0.5 litre/second

Tick | Message
--------------
000000: Station opens. Awaiting cars.
000059: Car#000002 has arrived and is 1 in queue
000059: Car#000002 waited 0 seconds to fuel
000060: Car#000002 starts fueling 42 litres
000107: Car#000004 has arrived and is 1 in queue
000144: Car#000002 got 42 liters in 84.0 seconds
000146: Car#000004 waited 39 seconds to fuel
000146: Car#000004 starts fueling 56 litres
000186: Car#000005 has arrived and is 1 in queue
000258: Car#000004 got 56 liters in 112.0 seconds
000259: Car#000005 waited 73 seconds to fuel
000259: Car#000005 starts fueling 42 litres
000267: Car#000001 has arrived and is 1 in queue
000289: Car#000003 has arrived and is 2 in queue
000344: Car#000005 got 42 liters in 84.0 seconds
000345: Car#000001 waited 78 seconds to fuel
000345: Car#000001 starts fueling 35 litres
000400: Station closes. Finishing last client! Goodbye!
000415: Car#000001 got 35 liters in 70.0 seconds

Results:
Cars served: 4
Cars left in queue: 1
Cars left the station unserved: 0

Avg wait time: 48.0 seconds
Avg fueling time: 87.5 seconds
Fuel left in station: 125 litres

Petrolex Station Simulator has ended.

What You Can Learn

Playing with the simulator isn’t just about fun—it’s a valuable learning experience. You’ll gain insights into multi-threading, synchronization, and performance optimization, all while honing your coding abilities.

Future Possibilities

The Petrolex Station Simulator is just the tip of the iceberg. In the future, you could expand on this concept by:

  • Adding more features like multiple stations and fuel pumps
  • Incorporating transactional simulations for a realistic touch
  • Implementing visualization tools to enhance understanding
  • Transforming it into an online game for collaborative coding fun

Conclusion

In the fast-paced world of coding, it’s easy to get caught up in the grind. But don’t forget to make time for fun and experimentation. Whether it’s tweaking a virtual petrol station or exploring new coding challenges, there’s always something exciting to discover. So, embrace the joy of coding and start playing today!

Leave a Reply

Your email address will not be published. Required fields are marked *