.

Friday, February 3, 2023

Java project that implements the basic operations of a gas station

 Create a GasStation class that represents the gas station. This class should have fields to store the gas prices, stock levels, and account balance.

public class GasStation {

  private double regularGasPrice;

  private double premiumGasPrice;

  private double dieselGasPrice;

  private int regularGasStock;

  private int premiumGasStock;

  private int dieselGasStock;

  private double accountBalance;


  // Getters and setters for the fields

  ...

}

Create a Sale class that represents a single sale of gas. This class should have fields to store the type of gas, the number of gallons sold, and the price per gallon.

public class Sale {

  private GasType gasType;

  private double gallonsSold;

  private double pricePerGallon;


  // Getters and setters for the fields

  ...

}

Create an enumeration GasType that represents the different types of gas sold at the gas station.

public enum GasType {
  REGULAR,
  PREMIUM,
  DIESEL
}

Implement methods in the GasStation class to handle the sale of gas and update the stock levels and account balance accordingly.
public class GasStation {
  ...

  public void sellGas(Sale sale) {
    GasType gasType = sale.getGasType();
    double gallonsSold = sale.getGallonsSold();
    double pricePerGallon = sale.getPricePerGallon();

    switch (gasType) {
      case REGULAR:
        regularGasStock -= gallonsSold;
        accountBalance += gallonsSold * pricePerGallon;
        break;
      case PREMIUM:
        premiumGasStock -= gallonsSold;
        accountBalance += gallonsSold * pricePerGallon;
        break;
      case DIESEL:
        dieselGasStock -= gallonsSold;
        accountBalance += gallonsSold * pricePerGallon;
        break;
    }
  }
}

Implement methods to retrieve the stock levels and account balance for each type of gas and for the gas station as a whole.

public class GasStation {
  ...

  public int getRegularGasStock() {
    return regularGasStock;
  }

  public int getPremiumGasStock() {
    return premiumGasStock;
  }

  public int getDieselGasStock() {
    return dieselGasStock;
  }

  public double getAccountBalance() {
    return accountBalance;
  }
}

This is just a basic outline of a Java project for the operations of a gas station. You can add additional functionality as needed, such as methods to update the gas prices or to print reports of the sales and stock levels.




Google Ads