Consider a super class Sales Item which models shops sales T

Consider a super class Sales Item which models shop\'s sales. This class has: three private instance variables Productname (String), Purchase Price (double) and SalesPrice (double), An overloaded constructor to initialize the instance variables. A method getSalesPrice that returns the PurchasePrice. A method getSalesPrice that returns the SalesPrice. Accessors and mutator methods. A Print() method to return the name of the item followed by the PurchasePrice and SalePrice. Consider two subclasses ProfitShop and LossShop. Profit has an additional instance variable Profit (double) while LossShop has an additional variable Loss (double) both private. Write an appropriate constructor for each of the classed making use of the constructor of the superclass in defining those of the subclasses. Override getPurchasePrice and getSalesPrice method that returns the price of the Purchase and Sales and Profit (ProfitShop), or Loss (LossShop). Make us of getPurchasePrice and getSalesPrice of the superclass. Override also print method for each class making use of the print method of the superclass in defining those of the subclasses. Print method should return something that can be printed on the receipt.

Solution

Below is the whole code along with the test class. The parent Class is created namely \"SalesItem\" along with the child classes that are \"ProfitShop\" and \"LossShop\". Comments are provided in the code for better understanding.

**************************************************************************************

import java.util.*;
import java.lang.*;
import java.io.*;

class SalesItem
{
    // private variables
    private String ProductName;
    private double PurchasePrice;
    private double SalesPrice;
   
    SalesItem() //default constructor
    {
        this.ProductName=\"\";
        this.PurchasePrice=0.0;
        this.SalesPrice=0.0;
    }
   
   SalesItem(String ProductName,double PurchasePrice, double SalesPrice)// overloaded paramatized constructor
    {
    this.ProductName=ProductName;
    this.PurchasePrice=PurchasePrice;
    this.SalesPrice=SalesPrice;
    }

public String getProductName() //accessor or getter method for ProductName
   {
    return ProductName;
   }
public double getPurchasePrice() //accessor or getter method for PurchasePrice
   {
    return PurchasePrice;
   }
public double getSalesPrice() //accessor or getter method for SalesPrice
{
    return SalesPrice;
}

public void setProductName(String ProductName) // mutator or setter method ProductName
{
    this.ProductName=ProductName;
}

public void setPurchasePrice(double PurchasePrice) // mutator or setter method PurchasePrice
   {
    this.PurchasePrice=PurchasePrice;
   }

public void setSalesPrice(double SalesPrice) // mutator or setter method SalesPrice
   {
    this.SalesPrice=SalesPrice;
   }

public void print() // print the product along with the purchase and sale prices
   {
    System.out.println(\"The Product Name: \"+ProductName+\"\ The Purchase Price: \"+PurchasePrice+
    \"\ The Sales Price: \"+SalesPrice);
   }
   
}

class ProfitShop extends SalesItem
{
    private double Profit;
    ProfitShop () //default constructor
    {
        super();
        Profit=0.0;
    }
   
    ProfitShop (String ProductName,double PurchasePrice, double SalesPrice) // overloaded paramatized constructor
    {
        super(ProductName,PurchasePrice,SalesPrice);
        Profit=0.0;
    }
    public double getProfitPrice() // calculate profit price
    {
        double x,y;
        x=super.getSalesPrice();
        y=super.getPurchasePrice();
        return x-y;
    }
    public void print() // print receipt with profit price
    {
        super.print();
        Profit=getProfitPrice();
        System.out.println(\"The Profit Price: \"+Profit);
    }
}

class LossShop extends SalesItem
{
    private double Loss;
    LossShop () //default constructor
    {
        super();
        Loss=0.0;
    }
   
    LossShop (String ProductName,double PurchasePrice, double SalesPrice) // overloaded paramatized constructor
    {
        super(ProductName,PurchasePrice,SalesPrice);
        Loss=0.0;
    }
    public double getLossPrice() // calculate loss price
    {
        double x,y;
        x=super.getPurchasePrice();
        y=super.getSalesPrice();
        return x-y;
    }
    public void print() // print receipt with loss price
    {
        super.print();
        Loss=getLossPrice();
        System.out.println(\"The Loss Price: \"+Loss);
    }
}

class TestClass // test class
{
public static void main (String[] args) throws java.lang.Exception // main function
{
  ProfitShop p=new ProfitShop(\"Laptop\",120.52,140.62); //object for profit shop
  LossShop l=new LossShop(\"TV\",140.5,100.0); // object for loss shop
  p.print();
  l.print();
}
}

 Consider a super class Sales Item which models shop\'s sales. This class has: three private instance variables Productname (String), Purchase Price (double) an
 Consider a super class Sales Item which models shop\'s sales. This class has: three private instance variables Productname (String), Purchase Price (double) an
 Consider a super class Sales Item which models shop\'s sales. This class has: three private instance variables Productname (String), Purchase Price (double) an

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site