KnowledgeBoat Logo

Computer Applications

A bookseller maintains record of books belonging to the various publishers. He uses a class with the specifications given below:

Class name — Stock

Data Members:

  1. String title — Contains title of the book
  2. String author — Contains author name
  3. String pub — Contains publisher's name
  4. int noc — Number of copies

Member Methods:

  1. void getdata() — To accept title, author, publisher's name and the number of copies.
  2. void purchase(int t, String a, String p, int n) — To check the existence of the book in the stock by comparing total, author's and publisher's name. Also check whether noc >n or not. If yes, maintain the balance as noc-n, otherwise display book is not available or stock is under flowing.

Write a program to perform the task given above.

Java

Java Classes

19 Likes

Answer

import java.util.Scanner;

public class Stock
{
    private String title;
    private String author;
    private String pub;
    private int noc;
    
    public void getdata() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter book title: ");
        title = in.nextLine();
        System.out.print("Enter book author: ");
        author = in.nextLine();
        System.out.print("Enter book publisher: ");
        pub = in.nextLine();
        System.out.print("Enter no. of copies: ");
        noc = in.nextInt();
    }
    
    public void purchase(String t, String a, String p, int n) {
        if (title.equalsIgnoreCase(t) &&
            author.equalsIgnoreCase(a) &&
            pub.equalsIgnoreCase(p)) {
                if (noc > n) {
                    noc -= n;
                    System.out.println("Updated noc = " + noc);
                }
                else {
                    System.out.println("Stock is under flowing");
                }
        }
        else {
            System.out.println("Book is not available");
        }
    }
    
    public static void main(String args[]) {
        Stock obj = new Stock();
        obj.getdata();
        obj.purchase("wings of fire", "APJ Abdul Kalam",
            "universities press", 10);
        obj.purchase("Ignited Minds", "APJ Abdul Kalam",
            "Penguin", 5);
        obj.purchase("wings of fire", "APJ Abdul Kalam",
            "universities press", 20);
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of A bookseller maintains record of books belonging to the various publishers. He uses a class with the specifications given below: Class name — Stock Data Members: (a) String title — Contains title of the book (b) String author — Contains author name (c) String pub — Contains publisher's name (d) int noc — Number of copies Member Methods: (e) void getdata() — To accept title, author, publisher's name and the number of copies. (f) void purchase(int t, String a, String p, int n) — To check the existence of the book in the stock by comparing total, author's and publisher's name. Also check whether noc >n or not. If yes, maintain the balance as noc-n, otherwise display book is not available or stock is under flowing. Write a program to perform the task given above.

Answered By

6 Likes


Related Questions