Question
Design a class name ShowRoom with the following description:
Instance variables/data members:
String name: to store the name of the customer.
long mobno: to store customer’s mobile number.
double cost: to store the cost of the item purchased.
double dis: to store the discount amount.
double amount: to store the amount to be paid after discount.
Methods:
ShowRoom(): default constructor to initialize the data members
void input(): to input customer name, mobile number, cost
void calculate(): to calculate the discount on the cost of purchased items, based on the following criteria
Cost Discount(In percentage)
Less than or equal to ₹ 10000 5%
More than ₹ 10000 and less than or equal to ₹ 20000 10%
More than ₹ 20000 and less than or equal to ₹ 35000 15%
More than ₹ 35000 20%
void display()- To display customer, mobile number, amount to be paid after discount.
Write a main() method to create an object of the class and call the above 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 ShowRoom
{
String name;
long mobno;
double cost,dis,amount;
/*default constructor*/
public ShowRoom()
{
name="";
mobno=0;
cost=0.0;
dis=0.0;
amount=0.0;
}
public void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
name=sc.nextLine();
System.out.print("Enter your Mobile Number: ");
mobno=sc.nextLong();
System.out.print("Enter cost of item purchased: ");
cost=sc.nextDouble();
}
public void calculate()
{
if(cost<=10000)
{
dis = cost * 5/100;
amount = cost - dis;
}
else if(cost>10000&&cost<=20000)
{
dis = cost * 10/100;
amount = cost - dis;
}
else if(cost>20000&&cost<=35000)
{
dis = cost * 15/100;
amount = cost - dis;
}
else if(cost>35000)
{
dis = cost * 20/100;
amount = cost - dis;
}
}
public void display()
{
System.out.println("Name of the customer: "+name);
System.out.println("Mobile number : "+mobno);
System.out.println("Amount to be paid after discount: "+amount);
}
public static void main(String[]args)
{
ShowRoom ob1 = new ShowRoom();
ob1.input();
ob1.calculate();
ob1.display();
}
}