Stock Purchase Inheritance ISC 2014 Theory

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 the 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 of an item.
amt: to store the net value of the item in stock.
Member functions:
Stock(…): parameterized constructor to assign 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(…): parameterized 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 of the item if there is a difference in the purchase rate. Also update the current stock value as:
(quantity * unit price)
void display(): to display the stock details before and after updating.

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

The main function and algorithm need not be written.

class Stock{
    protected String item;
    protected int qty;
    protected double rate;
    protected double amt;
    public Stock(String i, int q, double r){
        item = i;
        qty = q;
        rate = r;
        amt = qty * rate;
    }
    public void display(){
        System.out.println("Item: " + item);
        System.out.println("Quantity: " + qty);
        System.out.println("Rate: Rs. " + rate);
        System.out.println("Amount: Rs. " + amt);
    }
}
class Purchase extends Stock{
    private int pqty;
    private double prate;
    public Purchase(String i, int q, double r, int pq, double pr){
        super(i, q, r);
        pqty = pq;
        prate = pr;
    }
    public void update(){
        qty = pqty;
        if(prate != rate)
            rate = prate;
        amt = qty * rate;
    }
    public void display(){
        System.out.println("Before update:");
        super.display();
        System.out.println("After update:");
        update();
        System.out.println("Quantity: " + qty);
        System.out.println("Rate: Rs. " + rate);
        System.out.println("Amount: Rs. " + amt);
    }
}

Leave a Reply

%d bloggers like this: