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