Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.1 kB
4
Indexable
Never
import java.util.Scanner;

public class ElectricityBillGenerator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        System.out.println("     TN Electricity Bill");
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        
        System.out.print("Consumer Name: ");
        String consumerName = scanner.nextLine();
        
        System.out.print("Consumer No.: ");
        int consumerNo = scanner.nextInt();
        
        System.out.print("Previous Units: ");
        int previousUnits = scanner.nextInt();
        
        System.out.print("Present Units: ");
        int presentUnits = scanner.nextInt();
        
        int unitsConsumed = presentUnits - previousUnits;
        
        double totalAmount = calculateBillAmount(unitsConsumed);
        
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        System.out.println("Consumer Name : " + consumerName);
        System.out.println("Consumer No.  : " + consumerNo);
        System.out.println("Previous Units : " + previousUnits);
        System.out.println("Present Units : " + presentUnits);
        System.out.println("Units Consumed : " + unitsConsumed);
        System.out.println("Total Amount : ₹" + totalAmount);
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        
        scanner.close();
    }
    
    public static double calculateBillAmount(int unitsConsumed) {
        double totalAmount = 0;
        
        if (unitsConsumed <= 500) {
            if (unitsConsumed > 400) {
                totalAmount += (unitsConsumed - 400) * 6;
                unitsConsumed = 400;
            }
            if (unitsConsumed > 200) {
                totalAmount += (unitsConsumed - 200) * 4.5;
                unitsConsumed = 200;
            }
            totalAmount += unitsConsumed * 2.25;
        } else {
            totalAmount += 500 * 2.25;
            unitsConsumed -= 500;

            if (unitsConsumed <= 400) {
                totalAmount += unitsConsumed * 4.5;
            } else {
                totalAmount += 400 * 4.5;
                unitsConsumed -= 400;
                if (unitsConsumed <= 100) {
                    totalAmount += unitsConsumed * 6;
                } else {
                    totalAmount += 100 * 6;
                    unitsConsumed -= 100;
                    if (unitsConsumed <= 100) {
                        totalAmount += unitsConsumed * 8;
                    } else {
                        totalAmount += 100 * 8;
                        unitsConsumed -= 100;
                        if (unitsConsumed <= 200) {
                            totalAmount += unitsConsumed * 9;
                        } else {
                            totalAmount += 200 * 9;
                            unitsConsumed -= 200;
                            totalAmount += unitsConsumed * 10;
                        }
                    }
                }
            }
        }
        return totalAmount;
    }
}