P5: The Gas Station

COMP 14-091 Summer Session I 2000


Assigned: Tuesday, June 13

Due: Tuesday, June 20, at the beginning of class!

I changed GasStation.java! The three constants at the top of the class should say public static final int...

Description

In this exercise you will implement two classes to represent a gas station. Then you will write a program to use these classes to run a simulation of a gas station.
The two classes you need to write are: Then you will write a program that uses these two classes. It should pick a random number of gallons, a random grade of gasoline and a maximum price that customer is willing to pay for each customer. Then it should use a GasStation object that you've instantiated to simulate purchasing the gas (choose your own numbers for the price of the gasoline and the number of gallons in the gas pumps to start with). If the gas station's price is too high, then the customer doesn't buy the gas. If there isn't enough gas of the grade they want, they also leave without buying gas. You should continue to run this simulation until the gas station runs out of all three grades of gas. Then it should report these statistics for the gas station: What if the station runs out of their type of gas and the price is too high? Well, then just count them as a person who thought the price was too high. If the price is too high, they wouldn't have even bothered to see if there was any gas.

What does "run out of gas" mean?

In this case, we mean when a customer comes along that wants more gas than is available in the pump's tank, you can say that that pump has run out of gas.
For example, say there are 10 gallons of gas left in the regular pump. A customer drives up and asks for 12 gallons. You don't have enough, so you can consider that pump out of gas. (even though someone could drive up and only want 5 gallons and you'd have that...) Now, it's up to you as to whether you give the person who only wants 5 gallons of gas the gas or not. The "run out of gas" condition is just for the purposes of ending the simulation. In fact, it is easier to program if you give this person the gas.

Don't forget to check the program grading criteria to see all the normal stuff your program should do.

Grading for this Assignment

Assuming you perfectly comment your program and have excellent programming style, here is what you need to have working to get each of these letter grades:

Steps to Follow

Same as usual. This time you will have 3 classes in your project. Here are their names, what kind of class you should add, and a framework I made to start from:
  1. GasSimulator.java - this should be a ClassMain - framework for GasSimulater.java
  2. GasPump.java - this should be a Class - framework for GasPump.java
  3. GasStation.java - this should be a Class - framwork for GasStation.java
Don't forget all the comments and the honor code pledge that should go at the top of each class in your program!

What is in the GasPump class

Your job in this class is to fill in the methods to make them do what is expected. Also, you need to provide comments for the methods.

What is in the GasStation class

What is in the GasSimulator class

I have made three useful methods for you to use in your main method. They pick the three random numbers that you need to run the simulation:

Testing your code

The following are some pieces of code that you can use to test the two classes before you write your main method. The first thing you should do is fill in GasPump.java. After you do that, you should replace the main method in GasSimulator.java with this one and run it. It will test parts of your class to make sure they work. DISCLAIMER: Just because your class passes this test doesn't mean that everything in it is right. I'm just trying to help you some.

public static void main(String[] args)
{
    GasPump pump;

    // instantiate a GasPump object with 10 gallons in it
    pump = new GasPump(10);
    System.out.println("Pump has " + pump.checkTank() + " gallons in it.");
    System.out.println("Should be 10");

    // pump out 8.25 gallons of gas, and make sure there is 1.75 gallons left
    pump.pumpGas(8.25);
    System.out.println("\nPump now has " + pump.checkTank() + " gallons in it.");
    System.out.println("Should be 1.75");
}

After doing the above, you should write your GasStation.java code and use this piece of code to test the two classes you have written now. (Once again, the above disclaimer applies here too):

public static void main(String[] args)
{
    GasStation crown;
    double price, totalPaid;

    // instantiate a GasStation object with 100 gallons per tank and
    //  prices of 1.25 each
    crown = new GasStation(100.0, 1.25);
    System.out.println("Station has earned $" + crown.moneyEarned());
    System.out.println("Should be $0");

    // change gas prices so they aren't all the same
    crown.setGasPrice(GasStation.REGULAR, 1.15);
    crown.setGasPrice(GasStation.SUPREME, 1.35);

    // buy gas at each price to see if that works:
    price = crown.buyGas(GasStation.REGULAR, 10.0);
    System.out.println("\n10 gallons of regular gas cost $" + price);
    System.out.println("Should be 11.50");
    totalPaid = price;

    price = crown.buyGas(GasStation.PLUS, 10.0);
    System.out.println("\n10 gallons of plus gas cost $" + price);
    System.out.println("Should be 12.50");
    totalPaid += price;

    price = crown.buyGas(GasStation.SUPREME, 10.0);
    System.out.println("\n10 gallons of supreme gas cost $" + price);
    System.out.println("Should be 13.50");
    totalPaid += price;

    System.out.println("\nThe gas station has earned $" + crown.moneyEarned());
    System.out.println("Should be $" + totalPaid);
    price = crown.buyGas(GasStation.SUPREME, 15.0);
    System.out.println("\nTried to buy 15 gallons, but there should have been only 10 left");
    System.out.println("This number should be negative: " + price);
}

This is exactly the sort of thing you should do when writing this stuff for yourself from scratch. These little main methods are called drivers. (see the book's discussion on page 186). They are little pieces of code to test the functionality of your classes.

What to turn in

(don't forget to keep a copy for yourself!):

Notes

Note: this is only an example. Your work does not need to match the example word for word.