UML:  The Gas Pump  Class

 

Class: GasPump

-pricePerGallon: double

-gallons: double

-fuelType: int

-fuelName: String

-paymentType: int

-washPrice: float

+ GasPump ( )          //   Default Constructor

+ GasPump ( double price) Constructor  w/params

+setCost(double cost): void

+setGrade(int fuelType): void

+getGrade( ): String

+calcBill(double gallons): double

+paymentMethod(int type) : void

+payInsideMethod ( ): void

+payOutsideMethod( ): void

+getCost( ): double

+printReceipt ( ): void

+carWash (int  washType): float

 

 

Attributes (Data items) :

 

 pricePerGallon   // price per gallon of gasoline

 gallons:              //Number of gallons purchased

 fuelType            // 1 = Regular; 2 = Mid Grade, 3=Super Unleaded,     

                            //                     4 =Diesel, 5 = Natural Gas          

 paymentType     // 1=Pay Inside, 2 = Pay Outside,  3 = Pay before pumping

 washPrice          // Cost of a car wash at the station    

 

Methods (Operations):

public GasPump {   };   // Default constructor  without anything

public GasPump ( )   // Default Constructor (no parameters in parameter list)

{   pricePerGallon = 1.999;  }  // Set default price per gallon of fuel

 

public GasPump ( double price)   // Constructor with parameters

{  pricePerGallon = price; }    // Set price per gallon for class data attribute

 

    

The setCost  method sets (initializes) the cost per gallon of fuel

 

public setCost (double cost)    //  Setter method for class variables

{      pricePerGallon  = cost;   }

 

double  getCost( )

{  return pricePerGallon; }

 

public setGrade(int fuelType)

{  String fuelName:

switch (fuelType)

{

   case 1:    fuelName = “Regular Unleaded”

                   break;

  case 2:    fuelName = “Mid grade Unleaded”

                   break;

 case 3:    fuelName = “Super Unleaded”

                   break;

 case 4:    fuelName = “Diesel”

                   break;

 case 5:    fuelName = “Natural Gas”

                   break;

 default:    System.out.println(“Unknown fuel type” + fuelType);

                 fuelName = “Unknown”;

}   // End of switch

}  // End of method

 

public String getGrade ( )

{ return fuelName; }

 

calcBill method  computes the bill:     pricePerGallon * gallons

                 and returns the result as the amount the customer has to pay.

 

double calcBill (double gallons)

{  return (pricePerGallon * gallons);  }

 

void paymentMethod (int  paymentType)     // Payment method

{

   if (paymentType  = = 1)

       payInsideMethod();     // Pay inside after pumping

   else

      if (paymentType == 2)

        payOutsideMethod();   // Pay at the pump

      else

        if (paymentType == 3)

            payInsideMethod ( );     // Pay inside before pumping

       else

           System.out.println(“Error: Unknown payment type”);

}

 

void payInsideMethod ( )

{

     System.out.println(“Pay cashier inside the station”);

}

void payOutsideMethod ( )

{

     System.out.println(“Pay at the pump with credit card ”);

}

float  washPrice  carWash ( int washType)

{

   washPrice = 0.00;        // Initialize wash price to zero dollars & cents

   float wash1 = 7.00;    float wash2 = 8.00;  float wash3 = 10.00;

 

   if (washType == 1)

       washPrice = wash1;    // Standard wash no wax, no dry

  else

        if (washType == 2)

             washPrice = wash2;  // Deluxe wash with wax and dry

       else

           if (washType == 3)

             washPrice = wash3;  // Deluxe wash with wax and hand dry

           else

             System.out.println(“Error: Incorrect or unknown wash type”);

    return  washPrice;    // Return wash price to calling method

}   // End of washPrice method

    

 

 

void printReceipt ( )     // Print receipt method

{  System.out.println (fuelName + “ “ +gallons + “ Gallons purchased at $ “ +  pricePerGallon + “ per gallon “ +    “ Total cost is:  “ +  pricePerGallon * gallons + “ Total bill is $ :  pricePerGallon * gallons + washPrice );

 }

 

 

 

 

Examples of how to instantiate (create) new gas pump objects using the GasPump class:

 

GasPump BP = new GasPump ( );     // “new” means make a new object

GasPump Exxon = new GasPump (2.899 );     // 2.899 is the price per gallon

GasPump Shell = new GasPump (2.779 );

GasPump WaWa = new GasPump (2.759);

GasPump  Sheetz   = new GasPump (2.899);

GasPump  Valero  = new GasPump (2.719);