Book Shop

Design a class with the following description.

Class Name: BookShop

Data Members:

  • String cName – To store the name of the customer.
  • String bName – To store the name of the book.
  • double price – To store price of the book.
  • double bill – To store the amount to be paid after applying discount.

Member Methods:

  • BookShop() – Default constructor to initialize the data members.
  • void input() – To input customer name, book name and price of the book.
  • void calculate() – To calculate the bill based on the following criteria.
    • Upto ₹500 discount will be 5%.
    • From ₹501 to ₹1500 discount will be 8%.
    • From ₹1501 to ₹5000 discount will be 10%.
    • More than ₹5000 discount will be 15%.
  • void display() – To print all details.

Write a main method to create an object of the class and call methods.

Java
import java.util.*;
public class BookShop
{
    String bName,cName;
    double price,bill;
    BookShop()
    {
        bName="";
        cName="";
        price=0.0;
        bill=0.0;
    }
    public void input()
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Customer name: ");
        cName=sc.nextLine();
        System.out.print("Name of the book: ");
        bName=sc.nextLine();
        System.out.print("Price of the book: ");
        price=sc.nextDouble();
        System.out.println();
    }
    public void calculate()
    {
        if(price>0&&price<=500)
        {
            bill=price-(price*0.05);
        }
        else if(price>500&&price<=1500)
        {
            bill=price-(price*0.08);
        }
        else if(price>1500&&price<=5000)
        {
            bill=price-(price*0.10);
        }
        else if(price>5000)
        {
            bill=price-(price*0.15);
        }
        else
        {
            System.out.println("Price of book can not be zero or negative");
        }
    }
    public void display()
    {
        System.out.println("Customer Name: "+cName);
        System.out.println("Name of the book: "+bName);
        System.out.println("Price of the book: "+price);
        System.out.println("Price after discount: "+bill);
    }
    public static void main(String args[])
    {
        BookShop ob=new BookShop();
        ob.input();
        ob.calculate();
        ob.display();
    }
}
Java