-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelecomBillingGUI.java
More file actions
126 lines (103 loc) · 4.3 KB
/
TelecomBillingGUI.java
File metadata and controls
126 lines (103 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*📱 Telecom Billing System – Java Swing GUI
This project is a Java Swing–based GUI application that calculates the monthly telecom bill for a user based on package type (A–E) and total call duration in seconds.
⭐ Features
User-friendly GUI built with JFrame, JPanel, and GridLayout
Input fields for:
Package type (A–E)
Seconds called
Built-in input validation for incorrect or missing values
Automatic bill calculation based on:
Monthly rental
Per–second call rates
Discounts & surcharges:
10% discount if call charges > LKR 5000
5% surcharge if call charges < LKR 1000
Displays final bill in a non-editable text box
Error messages shown in real-time
🧮 Billing Logic
Each package (A–E) has:
Different monthly rental
Different per-second charge
Final bill = monthly rental + (seconds × per-second rate)
Then discount/surcharge rules are applied.
🧰 Technologies Used
Java Swing
Event Handling (ActionListener)
GUI Layout Management (GridLayout) */
package project;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TelecomBillingGUI {
public static void main(String[] args) {
// Create the frame
JFrame frame = new JFrame("Telecom Billing System");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create panels
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 2, 10, 10));
frame.add(panel);
// Package type input
JLabel packageLabel = new JLabel("Package Type (A-E):");
JTextField packageField = new JTextField();
panel.add(packageLabel);
panel.add(packageField);
// Seconds called input
JLabel secondsLabel = new JLabel("Seconds Called:");
JTextField secondsField = new JTextField();
panel.add(secondsLabel);
panel.add(secondsField);
// Button to calculate
JButton calculateButton = new JButton("Calculate Bill");
panel.add(new JLabel()); // Spacer
panel.add(calculateButton);
// Result label
JLabel resultLabel = new JLabel("Final Bill: ");
panel.add(resultLabel);
JTextField resultField = new JTextField();
resultField.setEditable(false);
panel.add(resultField);
// Error message
JLabel errorLabel = new JLabel("");
errorLabel.setForeground(Color.RED);
panel.add(errorLabel);
// Billing logic
calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String packageType = packageField.getText().toUpperCase();
String secondsText = secondsField.getText();
// Validate input
if (!secondsText.matches("\\d+") || packageType.length() != 1 || !"ABCDE".contains(packageType)) {
errorLabel.setText("Invalid input! Check package type or seconds.");
resultField.setText("");
return;
}
errorLabel.setText(""); // Clear previous error message
int secondsCalled = Integer.parseInt(secondsText);
// Define package details
String[] packages = {"A", "B", "C", "D", "E"};
double[] monthlyRentals = {1000, 1500, 2000, 2500, 3000};
double[] perSecondCharges = {2, 1.5, 1, 0.75, 0.5};
int packageIndex = "ABCDE".indexOf(packageType);
double monthlyRental = monthlyRentals[packageIndex];
double perSecondCharge = perSecondCharges[packageIndex];
// Calculate call charges
double callCharges = secondsCalled * perSecondCharge;
double totalCharges = monthlyRental + callCharges;
// Apply discount or surcharge
if (callCharges > 5000) {
totalCharges *= 0.9; // 10% discount
} else if (callCharges < 1000) {
totalCharges *= 1.05; // 5% surcharge
}
// Display the result
resultField.setText(String.format("LKR %.2f", totalCharges));
}
});
// Set the frame visible
frame.setVisible(true);
}
}