Answer to Question #261997 in Java | JSP | JSF for Zagar

Question #261997

You are required to create a class called BuyDiesel so that you can represent your purchase information related to Diesel.


The class should include five pieces of information in the form of instance variable


1. the station’s location (type String)

2. the type of Diesel (type String)

3. the quantity (type int) of the purchase in liters

4. the price per liter (double)

5. the percentage discount (double) which is in terms of how much percentage is off.


Your class should have a constructor that initializes the five instance variables. Provide a set and a get method for each instance variable.


In addition, provide a method named getPurchaseAmount that calculates the net purchase amount


(i.e., multiplies the quantity by the price per liter) minus the discount, then returns the net amount you had to pay as a double value.

Write an application class named Diesel that demonstrates the capabilities of class Diesel

Purchase.


1
Expert's answer
2021-11-06T18:56:48-0400
public class BuyDiesel {
    private String stationLocation;
    private String typeOfDiesel;
    private int purchaseQuantity;
    private double pricePerLiter;
    private double percentageDiscount;

    public BuyDiesel(String stationLocation, String typeOfDiesel, int purchaseQuantity,
                     double pricePerLiter, double percentageDiscount) {
        this.stationLocation = stationLocation;
        this.typeOfDiesel = typeOfDiesel;
        this.purchaseQuantity = purchaseQuantity;
        this.pricePerLiter = pricePerLiter;
        this.percentageDiscount = percentageDiscount;
    }

    public String getStationLocation() {
        return stationLocation;
    }

    public void setStationLocation(String stationLocation) {
        this.stationLocation = stationLocation;
    }

    public String getTypeOfDiesel() {
        return typeOfDiesel;
    }

    public void setTypeOfDiesel(String typeOfDiesel) {
        this.typeOfDiesel = typeOfDiesel;
    }

    public int getPurchaseQuantity() {
        return purchaseQuantity;
    }

    public void setPurchaseQuantity(int purchaseQuantity) {
        this.purchaseQuantity = purchaseQuantity;
    }

    public double getPricePerLiter() {
        return pricePerLiter;
    }

    public void setPricePerLiter(double pricePerLiter) {
        this.pricePerLiter = pricePerLiter;
    }

    public double getPercentageDiscount() {
        return percentageDiscount;
    }

    public void setPercentageDiscount(double percentageDiscount) {
        this.percentageDiscount = percentageDiscount;
    }

    public double getPurchaseAmount() {
        return purchaseQuantity * pricePerLiter * (1 - percentageDiscount / 100);
    }
}


public class Diesel {
    public static void main(String[] args) {
        BuyDiesel buyDiesel = new BuyDiesel("HomeTown", "Euro100", 20, 15.5, 20);

        System.out.println("The station's location: " + buyDiesel.getStationLocation());
        System.out.println("The type of diesel: " + buyDiesel.getTypeOfDiesel());
        System.out.println("The purchase quantity in liters: " + buyDiesel.getPurchaseQuantity());
        System.out.println("The price per liter: " + buyDiesel.getPricePerLiter());
        System.out.println("The percentage discount %: " + buyDiesel.getPercentageDiscount());
        System.out.println("The purchase amount: " + buyDiesel.getPurchaseAmount());
        System.out.println();

        buyDiesel.setStationLocation("Mars");
        buyDiesel.setTypeOfDiesel("Marsol");
        buyDiesel.setPurchaseQuantity(100);
        buyDiesel.setPricePerLiter(12.33);
        buyDiesel.setPercentageDiscount(1.5);

        System.out.println("The station's location: " + buyDiesel.getStationLocation());
        System.out.println("The type of diesel: " + buyDiesel.getTypeOfDiesel());
        System.out.println("The purchase quantity in liters: " + buyDiesel.getPurchaseQuantity());
        System.out.println("The price per liter: " + buyDiesel.getPricePerLiter());
        System.out.println("The percentage discount %: " + buyDiesel.getPercentageDiscount());
        System.out.println("The purchase amount: " + buyDiesel.getPurchaseAmount());

    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS