Home Java Build a Simple Water System Calculator in Java Using Swing

Build a Simple Water System Calculator in Java Using Swing

2 min read
0
0
0

If you’re just beginning to learn Java GUI programming creating an Water System Calculator is a fantastic project for a beginner. This tutorial will help you discover how to build a basic water-bill calculator by using Java Swing and simple code and the best methods.The Java project is great for college projects, practical learning, or for adding an element to your resume.

What Will You Build?

You’ll design the GUI application that allows users to:

  • Then enter the water consumption (litres).

  • Click Calculate.

  • Check out the total bill for water in relation to usage.

The water bill costs:

  • 1.5 per Liter that can be used up to 50 litres.

  • 2.0 per Liter for up to 100 litres.

  • 3.0 per 1 litre for more than 100 litres.

Technologies Used

  • Java 8+

  • Swing GUI

  • NetBeans IDE (or any Java IDE)

Step 1: Create the Calculation Logic

Create a new class called WaterCalculator.java

public class WaterCalculator {
    public static double calculateBill(double consumption) {
        double rate;

        if (consumption <= 50) {
            rate = 1.5;
        } else if (consumption <= 100) {
            rate = 2.0;
        } else {
            rate = 3.0;
        }

        return consumption * rate;
}
}

Step 2: Build the Swing User Interface

Now, create the GUI in Water.java. paste include the Calculate Button

try {
        double consumption = Double.parseDouble(txtConsumption.getText());
        double bill = WaterCalculator.calculateBill(consumption);
        lblResult.setText("Total Bill: Rs. " + String.format("%.2f", bill));
    } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(this, "Please enter a valid number.", "Error", JOptionPane.ERROR_MESSAGE);
    }

 

 

 

 

 

Load More Related Articles
Load More By admin
Load More In Java

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also

GitHub Copilot vs Microsoft Copilot Best AI Tool to Use in 2025

GitHub is a powerful tool used by teams and developers around the globe. This guide is for…