Java

Build a Simple Water System Calculator in Java Using Swing

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);
    }

 

 

 

 

 

admin

Recent Posts

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…

4 days ago

Chat with Claude AI Free – Your Super-Smart AI Buddy

It's like having a super-smart buddy that is always there to help you write stories,…

1 week ago

Best Festivals UK 2025 [Free Guide Included]

The UK is known for its rich history, diverse culture, and most of all  its…

2 weeks ago

Bank Holidays 2025 UK – Plan Your Perfect Long Weekends

Do you have a plan for your next holiday? Being aware of the Bank Holidays within the…

2 weeks ago

Master Cursor AI Full Guide for Students & Creators

The world is rapidly changing of software development AI-assisted tools for coding have become the main focus. As…

2 weeks ago

Google Gemini AI Free AI Tool for Students & Creators

Google Gemini AI is among the top talked about developments. What exactly is it? What…

2 weeks ago