Membership-Based Restaurant Billing with Tiered Discounts and Bonus Thresholds
![]() |
Membership-Based Restaurant Billing System Java Code |
A fine-dining restaurant offers membership-based discounts and maintains customer billing records for every visit. Each customer is categorized into one of the following membership tiers:
- Regular
- Gold
- Platinum
For each visit, customers order multiple items. The final payable bill depends on two rules:
Tier-Based Discount:
- Regular → 5% off
- Gold → 10% off
- Platinum → 20% off
Bonus Discount Threshold:
If the original total (before discount) exceeds ₹1000, they get an extra ₹50 off in addition to their tier-based discount.
Customers are billed per visit, and all billing data is stored by customer name.
The system must:
- Accept a batch of customer orders with item prices
- Compute the final amount due per customer using the above rules
- Store and display all customer bills in alphabetical order of names
- If any item has a negative price, immediately throw and handle a custom exception
InvalidPriceException
with the message:
Invalid price found for customer <customerName>: <price>
Input Format:
An integer n indicating number of customers
For each customer:
One line: <customerName> <membershipType> <k> (where k = number of items)
Next k lines: <itemName> <itemPrice>
Output Format:
If all inputs are valid, print each customer and their total bill like:
“<customerName>: ₹<finalBill>“
If any item price is negative, print only:
“Invalid price found for customer <customerName>: <price>”
Sample Input 1
2
Alice Gold 3
Pasta 400
Wine 700
Dessert 100
Bob Regular 2
Burger 300
Fries 150
Sample Output 1
Alice: ₹1030.00
Bob: ₹427.50
Sample Input 2
1
Eve Platinum 2
Steak 1200
Juice -50
Sample Output 2
Invalid price found for customer Eve: -50
Membership-Based Restaurant Billing with Tiered Discounts and Bonus Thresholds (Java Code) 👇
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
/**
* Custom exception for handling negative item prices.
*/
class InvalidPriceException extends Exception {
public InvalidPriceException(String message) {
super(message);
}
}
/**
* Main class to handle restaurant billing.
* The class name is 'Main' to match the filename 'Main.java'.
*/
public class Main {
public static void main(String[] args) {
// Scanner for reading user input
Scanner scanner = new Scanner(System.in);
// TreeMap automatically stores entries sorted by key (customer name)
Map<String, Double> customerBills = new TreeMap<>();
try {
// Read the total number of customers
int numberOfCustomers = Integer.parseInt(scanner.nextLine());
// Loop through each customer to process their order
for (int i = 0; i < numberOfCustomers; i++) {
String[] customerInfo = scanner.nextLine().split(" ");
String customerName = customerInfo[0];
String membershipType = customerInfo[1];
int numberOfItems = Integer.parseInt(customerInfo[2]);
double totalOriginalBill = 0.0;
// Loop through each item for the current customer
for (int j = 0; j < numberOfItems; j++) {
String[] itemInfo = scanner.nextLine().split(" ");
// String itemName = itemInfo[0]; // Item name is not used in calculation
double itemPrice = Double.parseDouble(itemInfo[1]);
// Validate the item price
if (itemPrice < 0) {
// If price is negative, throw the custom exception and stop
throw new InvalidPriceException(
"Invalid price found for customer " + customerName + ": " + (int)itemPrice
);
}
totalOriginalBill += itemPrice;
}
// Calculate tier-based discount
double discountRate = 0.0;
switch (membershipType) {
case "Regular":
discountRate = 0.05; // 5%
break;
case "Gold":
discountRate = 0.10; // 10%
break;
case "Platinum":
discountRate = 0.20; // 20%
break;
}
double billAfterTierDiscount = totalOriginalBill * (1 - discountRate);
double finalBill = billAfterTierDiscount;
// Apply bonus discount if the original total exceeds the threshold
if (totalOriginalBill > 1000) {
finalBill -= 50;
}
// Store the final bill for the customer
customerBills.put(customerName, finalBill);
}
// If all inputs were valid, print the sorted bills
for (Map.Entry<String, Double> entry : customerBills.entrySet()) {
System.out.printf("%s: ₹%.2f\n", entry.getKey(), entry.getValue());
}
} catch (InvalidPriceException e) {
// Catch the custom exception and print its message
System.out.println(e.getMessage());
} catch (NumberFormatException e) {
// Handle cases where a number might be formatted incorrectly
System.out.println("Invalid number format in input.");
} finally {
// Close the scanner to prevent resource leaks
scanner.close();
}
}
}