Define a class named BookFair with the following description: [15]
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.
(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.
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 % 3000 15% of price
import java.util.Scanner;
class BookFair {
private String Bname; // stores the name of the book
private double price; // stores the price of the book
// Default constructor to initialize data members
public BookFair() {
Bname = "";
price = 0.0;
}
// To input and store the name and the price of the book
public void Input() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the name of the book: ");
Bname = scanner.nextLine();
System.out.print("Enter the price of the book: Rs. ");
price = scanner.nextDouble();
scanner.close();
}
// To calculate the price after discount based on the given criteria
public void calculate() {
if (price <= 1000) {
price -= (0.02 * price); // 2% discount
} else if (price > 1000 && price <= 3000) {
price -= (0.10 * price); // 10% discount
} else {
price -= (0.15 * price); // 15% discount
}
}
// To display the name and price of the book after discount
public void display() {
System.out.println("Book Name: " + Bname);
System.out.println("Price after discount: Rs. " + price);
}
public static void main(String[] args) {
BookFair book = new BookFair(); // Create an object of the class
book.Input(); // Input the name and price of the book
book.calculate(); // Calculate the price after discount
book.display(); // Display the book details after discount
}
}