Rocketsm46
Rocketsm46

Reputation: 21

Simulating a gas station

So I'm supposed to create a program where I have a gas station with 5 gas pumps and and I don't know how to keep track of the statistics, the program correctly subtracts the amount of gas left from the type of gas tank but when the next customer pumps gas the statistics get erased? any help would be appreciated, here is my code. keep in my mind this is part of a 5 class program and this is only one part.

import java.util.Random;
import java.util.Scanner;
public class GasPump
{
    static Scanner Input = new Scanner (System.in);
    private static double totalRevenue;
    private  static double currentPurchase;
    //private int currentGasType;
    private static double currentGasType;
    private static double currentGasAmount;
    private Client currentClient;
    private int clock;
    private static double regularTank;
    private static double plusTank;
    private static double supremeTank;
    //private static double amountLeftInTank;



    public void updatePump()
    {
        if (currentClient != null)
        {
            clock--;
            if (clock == 0)
            {
                //totalRevenue += currentPurchase;
                totalRevenue = totalRevenue + currentPurchase;
                resetPump ( );

            }
        }
    }

    public void resetPump ( )    
    {
        clock = 0;
        currentClient = null;
        currentPurchase = 0;
        //currentGasType = "";
        currentGasType = 0;
        currentGasAmount = 0;

    }

    public boolean pumpOpen()
    {
        return (clock == 0) && (currentClient == null);
    }

    public static double getCurrentGasType() 
    {
        regularTank = 1000;
        plusTank = 1000;
        supremeTank = 1000;

        //Scanner Input = new Scanner(System.in);
        System.out.println("What type of gas would you like?");
        System.out.println("Regular , Plus or Premium");
        System.out.println("1)Regular: $2.99 per gallon; 2)Plus: $3.99 per gallon; 3)Premium: $4.99 per gallon");
        currentGasType = Input.nextDouble();

        Random gen = new Random ();
        //currentGasAmount = gen.nextDouble ()* 45;
        currentGasAmount = gen.nextDouble()*50;
        double roundOff = (double) Math.round(currentGasAmount * 100) / 100;
        //System.out.println("How many gallons would you like?");
        //currentGasAmount = Input.nextDouble();
        if (currentGasType == 1) 
        {
            currentGasType = 2.99;
            regularTank = regularTank - currentGasAmount;
        }
        else if (currentGasType == 2) 
        {
            currentGasType = 3.99;
            plusTank = plusTank - currentGasAmount;
        }
        else 
        {
            currentGasType = 4.99;
            supremeTank = supremeTank - currentGasAmount;
        }

        System.out.println("# of gallons purchased: " + currentGasAmount);
        System.out.println("Amount of regular gas left:  " + regularTank);
        System.out.println("Amount of plus gas left: " + plusTank);
        System.out.println("Amount of supreme gas left: " + supremeTank);
        return currentGasType;


    }

    /*public static double getCurrentGasAmount() {
        Random gen = new Random ();
        currentGasAmount = gen.nextDouble ()* 50;
        //System.out.println("How many gallons would you like?");
        //currentGasAmount = Input.nextDouble();
        System.out.println("# of gallons purchased: " + currentGasAmount);
        return currentGasAmount;
    }
     */
    public static double getCurrentPurchase() 
    {
        currentPurchase = currentGasType * currentGasAmount;
        return currentPurchase;

    }

    public static double getTotalRevenue() {
        totalRevenue += currentPurchase;

        System.out.println("Total revenue so far is " + totalRevenue);
        return totalRevenue;
    }
    /*public static double getAmountLeftInTank() 
    {
        regularTank = 1000;
        plusTank = 1000;
        supremeTank = 1000;
        if (currentGasAmount == 1) 
            if (currentGasType == 1)
        {
            //regularTank = regularTank - currentGasAmount; 
        }
            else if (currentGasType == 2)
        else if (currentGasAmount == 2) 
        {
            //plusTank = plusTank - currentGasAmount;
        }
        else 
        {
            supremeTank = supremeTank - currentGasAmount;
        }
        System.out.println("Amount of regular gas left:  " + regularTank);
        System.out.println("Amount of plus gas left: " + plusTank);
        System.out.println("Amount of supreme gas left: " + supremeTank);
        return amountLeftInTank;
    }
     */
    public void serveAClient (Client aClient)
    {
        clock = 10;
        currentClient = aClient;


        GasPump.getCurrentGasType();
        System.out.println("Your total is " + GasPump.getCurrentPurchase());
        GasPump.getTotalRevenue();

        //GasPump.getAmountLeftInTank();
        /*
         * design get methods
         * ask client what type of gas he wants 
         * add more code here
         */
        // add the total here
    }
}

Upvotes: 1

Views: 2186

Answers (1)

Chris K
Chris K

Reputation: 11927

Don't use static fields for the data stored in GasPump.

static fields are singletons, they only have a single value shared across all instances of GasPump. This means that if you have multiple instances of GasPump, then calling reset will reset all of the gas pumps.

By removing the keyword static from each of the fields, then there will be a separate copy of the field held for each of the GasPump's. And thus calling reset will only wipe the fields for that one instance of GasPump.

The following diagram may help you to visualise the difference:

enter image description here

In this example, count has been shared across instances c1 and c2 of CircleWithCount.

You can read more detail about using the static keyword on fields here: What does the 'static' keyword do in a class?

Upvotes: 7

Related Questions