Stock Inheritance ISC Computer Science 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 update 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 previous 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 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.

import java.util.Scanner;
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 name: " + item);
        System.out.println("Quantity: " + qty);
        System.out.println("Unit price: " + rate);
        System.out.println("Stock value: " + amt);
    }
}
class Purchase extends Stock{
    int pqty;
    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(rate != prate)
            rate = prate;
        amt = qty * rate;
    }
    public void display(){
        System.out.println("BEFORE UPDATION:");
        super.display();
        update();
        System.out.println("AFTER UPDATION:");
        System.out.println("New quantity: " + qty);
        System.out.println("New rate: " + rate);
        System.out.println("New stock value: " + amt);
    }
}
class MyInheritance{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Item name: ");
        String i = in.nextLine();
        System.out.print("Quantity: ");
        int q = Integer.parseInt(in.nextLine());
        System.out.print("Rate: ");
        double r = Integer.parseInt(in.nextLine());
        System.out.print("New Quantity: ");
        int pq = Integer.parseInt(in.nextLine());
        System.out.print("New Rate: ");
        double pr = Integer.parseInt(in.nextLine());
        Purchase obj = new Purchase(i, q, r, pq, pr);
        obj.display();
    }
}

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory