icse-promo

Question

Define a class named BookFair with the following description: 

Instance variables/Data members:
String Bname – stores the name of the book.
double price – stores the price of the book.

Member Methods:
(i) BookFair() – Default constructor to initialize data members.
(ii) void Input() – To input and store the name and the price of the book.
(iii) void calculate() – To calculate the price after discount. Discount is calculated based on the following criteria.

				
					PRICE DISCOUNT
Less than or equal to Rs 1000:  2% of price
More than Rs 1000 and less than or equal to Rs 3000:  10% of price
More than Rs 3000:  15% of price
				
			

(iv) void display() – To display the name and price of the book after discount.

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

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;

public class BookFair
{
    String Bname;
    double price;

    public BookFair() 
    {
        Bname = "";
        price = 0.0;
    }

    public void Input() 
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the book name: ");
        Bname = sc.nextLine();
        System.out.print("Enter the price: ");
        price = sc.nextDouble();
    }

    public void calculate() 
    {
        double discountPer = 0;
        if (price <= 1000)
        {
	        discountPer = 2;
        }      
        else if (price > 1000 && price <= 3000) 
        {
	        discountPer = 10;
        } 
        else if (price > 3000) 
        {
            discountPer = 15;
        }
        price = price - (price * discountPer / 100);
    }
    public void display() 
    {
        System.out.println("Name: " + Bname);
        System.out.println("Price after discount: " + price);
    }
    public static void main(String[] args) 
    {
        BookFair ob1 = new BookFair();
        ob1.Input();
        ob1.calculate();
        ob1.display();
    }
}

				
			

Coding Store

Leave a Reply

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