Stock Purchase

ISC Computer Science 2014

Question 11.

A super class Stock has been defined to store the details of the stock of a retail store. Define a subclass Purchase to store the details of the items purchased with the new rate and updates the stock. Some of members of the classes are given below:

Class name: Stock

Data members/instance variables:

item : to store the name of the item
qty  : to store the quantity of an item in stock
rate : to store the unit price if an item
amt  : to store the net value of the item in stock

Member functions/methods:

Stock(...)    : parameterized constructor to assign the values to the data members
void display(): to display the stock details
Class name: Purchase

Data members/instance variables:

pqty : to store the purchased quantity
prate: to store the unit price of the purchased item

Member functions/methods:

Purchase(...) : parametrized constructor to assign values to the data 
                members of both classes
void update() : to update stock by adding the previous quantity by the 
                purchased quantity and replace the rate. 
                Also update the current stock value as:
                (quantity * unit price)
void display(): to display the stock details before and after updation

Specify the class Stock, giving details of the constructor() and void display(). Using the concept of inheritence, specify the class Purchase, giving details of the constructor(), void update() and void display() function.

class Stock{ // Stock
     protected String item;
     protected int qty;
     protected float rate;
     protected float amt;
     public Stock(String item, int qty, float rate, float amt){
         this.item = item;
         this.qty = qty;
         this.rate = rate;
     public void display(){
         System.out.println( "Item: " + item );
         System.out.println( "Quantity: " + qty );
         System.out.println( "Rate: " + rate );
         System.out.println( "Amount: " + amt );
class Purchase extends Stock{ //Stock Purchase
    private int pqty, prate;
    public Purchase(String item, int qty, float rate, float amt, int pqty, int prate){
        super( item, qty, rate, amt);
        this.pqty = pqty;
        this.prate = prate;
    public void update(){
        qty = qty + pqty;
        rate = prate;
        amt = qty * prate;
    public void display(){
        System.out.println( "Purchased Quantity: " + pqty );
        System.out.println( "Purchase Rate: " + prate );

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.