Design a class with the following description.
Class Name: ShowRoom
Data Members:
- String name – To store the name of the consumer.
- long mobile – To store consumer’s mobile number.
- double cost – To store the cost of the item purchased.
- double d – To store the discount amount.
- double amount – To store the amount to be paid after discount.
Member Methods:
- ShowRoom() – Default constructor to initialize the data members.
- void input() – To input consumer name, mobile number and cost of item.
- void calculate() – To calculate the discount on the cost of purchased items based on the following criteria.
- Upto ₹1000 discount will be 3%.
- From ₹1001 to ₹5000 discount will be 5%.
- From ₹5001 to ₹10000 discount will be 8%.
- More than ₹10000 discount will be 10%
- 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 ShowRoom
{
String name;
long mobile;
double cost,amount,d;
ShowRoom()
{
name="";
mobile=0;
cost=0.0;
amount=0.0;
d=0.0;
}
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.print("Consumer name: ");
name=sc.nextLine();
System.out.print("Consumer's mobile number: ");
mobile=sc.nextLong();
System.out.print("Cost of the item purchased: ");
cost=sc.nextDouble();
System.out.println();
}
public void calculate()
{
if(cost>0&&cost<=1000)
{
d=cost*0.03;
}
else if(cost>1000&&cost<=5000)
{
d=cost*0.05;
}
else if(cost>5000&&cost<=10000)
{
d=cost*0.08;
}
else if(cost>10000)
{
d=cost*0.1;
}
else
{
System.out.println("Cost can not be zero or negative");
}
amount=cost-d;
}
public void display()
{
System.out.println("Customer's Name: "+name);
System.out.println("Customer's Mobile Number: "+mobile);
System.out.println("Cost of the product: "+cost);
System.out.println("Amount to be paid after discount: "+amount);
}
public static void main(String args[])
{
ShowRoom ob=new ShowRoom();
ob.input();
ob.calculate();
ob.display();
}
}Java