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.
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.
Solution
import java.io.*;
/**
* class BookFair here.
*
* @author (Khurshid Md Anwar)
*
*/
public class BookFair
{
// instance variables
private double price; // Book price
private double discount; //
Book discount
private String Bname; // Book Name
/**
* Constructor for objects of
class BookFair
*/
public BookFair()
{
// initialise instance
variables
price = 0.0;
Bname = null;
}
void input()throws IOException // input method
{
InputStreamReader in=new
InputStreamReader(System.in);
BufferedReader br=new
BufferedReader(in);
System.out.println("Enter Book name and it's Price: ");
Bname = br.readLine();
price =
Double.parseDouble(br.readLine());
}
void calculate() // calculate method
{
if(price <= 1000)
{
discount = price *
0.02; // 2% discount price
}
else if(price > 1000
&& price <= 3000) // 10% discount price
{
discount = price *
0.1;
}
else
{
discount = price
*.15; // 15% discount price
}
}
void display() // display method
{
price = price - discount;
System.out.println("\n\n
Book Name :" + Bname);
System.out.println("\n Price after discount :" + price);
}
public static void
main(String args[]) throws IOException
{
// Object creation
BookFair ob = new
BookFair();
ob.input();
ob.calculate();
ob.display();
}
}
Output:
Enter Book name and it's Price:
Learn Java in 24 Hours
450.0
Book Name :Learn Java in 24 Hours
Price after discount :441.0
Enter Book name and it's Price:
Search Engine Optimization
1200.0
Book Name :Search Engine Optimization
Price after discount :1080.0
More programs: