Design a class ShowRoom with the following description: Instance variables / data members: String name - to store the name of the customer long mobNo - to store the mobile number of the customer double cost - to store the cost of the items purchased double dis - to store the discount amount double amount - to store the amount to be paid after discount Member methods: ShowRoom() - default constructor to initialize data members void input() - to input customer name, mobile number, cost void calculate()- to calculate discount on the cost of purchased items, based on the following criteria: Cost Discount (%) Less than or equal to Rs. 10000 5% More than 10000 and up to 20000 10% More than 20000 and up to 35000 15% More than 35000 20% void display() - to display customer name, mobile number, amount to be paid after discount Write a main() method to create an object of the class and call the above member methods.
Topic: Class with default constructor and discount slabs
Answer
import java.util.Scanner;
class ShowRoom { String name; long mobNo; double cost, dis, amount;
ShowRoom() { name = ""; mobNo = 0L; cost = 0.0; dis = 0.0; amount = 0.0; }
void input() { Scanner sc = new Scanner(System.in); System.out.print("Enter customer name : "); name = sc.nextLine(); System.out.print("Enter mobile number : "); mobNo = sc.nextLong(); System.out.print("Enter cost of items : "); cost = sc.nextDouble(); }
void calculate() { double p; if(cost <= 10000) p = 5; else if(cost <= 20000) p = 10; else if(cost <= 35000) p = 15; else p = 20;
dis = cost * p / 100; amount = cost - dis; }
void display() { System.out.println("Name : " + name); System.out.println("Mobile number : " + mobNo); System.out.println("Amount to be paid : " + amount); }
public static void main(String args[]) { ShowRoom ob = new ShowRoom(); ob.input(); ob.calculate(); ob.display(); } }
A percentage-slab class, and it differs in one important way from the cumulative slabs you meet in courier and electricity questions: here the whole cost is discounted at ONE rate, decided by which band it falls into. There is no adding of lower bands.
Order the tests from the smallest band upwards. Because else-if stops at the first true condition, reaching the second branch already guarantees the cost is above 10000, so cost <= 20000 alone is enough.
Note the data type of mobNo: a ten-digit mobile number exceeds the range of int (about 2.1 billion), so long is required. The question specifies it, and choosing int would be a real error, not just a style point. Write the constructor initialisation as 0L.
The constructor is explicitly demanded, so write it with no return type and set every member to the stated default.
Check with a cost of 25000: it falls in the 15% band, so dis = 3750 and amount = Rs. 21250.
name String - name of the customer mobNo long - mobile number (long, since it exceeds int range) cost double - cost of the items purchased p double - discount percentage decided by the slab dis double - discount amount amount double - amount payable after discount sc Scanner - object used to read input ob ShowRoom - object of the class used in main()