public class Supermarket {
String name;
double mrp;
boolean discountApplied;
double discountPercent;
// Default Constructor
Supermarket() {
name = "Unknown";
mrp = 0.0;
discountApplied = false;
discountPercent = 0;
}
// Constructor Overloading (without discount)
Supermarket(String name, double mrp) {
this.name = name;
this.mrp = mrp;
this.discountApplied = false;
this.discountPercent = 0;
}
// Constructor Overloading (with discount)
Supermarket(String name, double mrp, boolean discountApplied, double discountPercent) {
this.name = name;
this.mrp = mrp;
this.discountApplied = discountApplied;
this.discountPercent = discountPercent;
}
// Method: Billing (no parameters)
void Billing() {
double finalPrice = mrp;
System.out.println("Item Name: " + name);
System.out.println("MRP Price: ₹" + mrp);
System.out.println("Discount Applied: " + discountApplied);
if (discountApplied) {
double discountAmount = (mrp * discountPercent) / 100;
finalPrice = mrp - discountAmount;
System.out.println("Discount Percentage: " + discountPercent + "%");
} else {
System.out.println("Discount Percentage: 0%");
}
System.out.println("Final Price: ₹" + finalPrice);
System.out.println("-----------------------------");
}
// Method Overloading (optional usage)
void Billing(boolean discountApplied, double discountPercent) {
this.discountApplied = discountApplied;
this.discountPercent = discountPercent;
Billing(); // reuse main method
}
// Main Method
public static void main(String[] args) {
// Object without discount
Supermarket item1 = new Supermarket("Rice", 100);
item1.Billing();
// Object with discount
Supermarket item2 = new Supermarket("Oil", 200, true, 10);
item2.Billing();
// Another example
Supermarket item3 = new Supermarket("Sugar", 50, false, 0);
item3.Billing();
}
}
Output

Top comments (0)