Consider a super class Sales Item which models shops sales T
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();
}
}


