Stock Sales Inheritance Java Program | ISC Computer Science 2021 Paper 1

A super class Stock has been defined to store the details of the stock of a retail store. Define a subclass Sales to store the details of the item sold and update the stock.

The details of the members of both 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
Methods/Member functions:
Stock(…): parameterized constructor to assign values to the data members
void display(): to display the stock details

Class name: Sales
Data members/instance variables:
sqty: to store the quantity sold
Methods/Member functions:
Sales(…): parameterized constructor to assign values to the data members of both the classes
void update(): to update stock by reducing the sold quantity from the stock quantity
void display(): to display the stock details before and after updating

Assume that the super class Stock has been defined. Using the concept of inheritance, specify the class Sales giving the details of the constructor(…), void update() and void display().

The super class, main() function and algorithm need not be written.

import java.util.Scanner;
class Stock{
    String item;
    int qty;
    public Stock(String i, int q){
        item = i;
        qty = q;
    }
    public void display(){
        System.out.println("Item: " + item);
        System.out.println("Stock: " + qty);
    }
}
class Sales extends Stock{
    int sqty;
    public Sales(String i, int q, int s){
        super(i, q);
        sqty = s;
    }
    public void update(){
        qty -= sqty;
    }
    public void display(){
        super.display();
        update();
        System.out.println("Quantity sold: " + sqty);
        System.out.println("Updated stock: " + qty);
    }
    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("Quantity sold: ");
        int s = Integer.parseInt(in.nextLine());
        Sales obj = new Sales(i, q, s);
        obj.display();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *