-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplianceDemo (copy).java
More file actions
150 lines (144 loc) · 4.32 KB
/
ApplianceDemo (copy).java
File metadata and controls
150 lines (144 loc) · 4.32 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.TitledBorder;
import javax.swing.*;
import java.io.*;
import java.nio.file.*;
import java.util.*;
public class ApplianceDemo extends JFrame {
private static Formatter output;
// Crea// year, loan amount, monthly payment, and total payment
private JTextField name = new JTextField();
private JTextField fans = new JTextField();
private JTextField tubelights = new JTextField();
private JTextField ACs = new JTextField();
private JTextField fridges = new JTextField();
private JTextField units = new JTextField();
// Create a Compute Payment button
private JButton ok = new JButton("Calculate Bill");
private JButton smart = new JButton("Analyse");
private JButton reset = new JButton("Reset");
public ApplianceDemo() {
// Panel p1 to hold labels and text fields
JPanel p1 = new JPanel(new GridLayout(6, 2));
p1.add(new JLabel("Enter your Name"));
p1.add(name);
p1.add(new JLabel("Enter number of fans"));
p1.add(fans);
p1.add(new JLabel("Number of tubelights"));
p1.add(tubelights);
p1.add(new JLabel("Enter number of ACs"));
p1.add(ACs);
p1.add(new JLabel("Enter number of Fridges"));
p1.add(fridges);
p1.add(new JLabel("Enter Units consumed"));
p1.add(units);
p1.setBorder(new
TitledBorder("Enter no of the mentioned appliances"));
// Panel p2 to hold the button
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
p2.add(ok);
p2.add(reset);
p2.add(smart);
// Add the panels to the frame
add(p1, BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);
// Register listener
ok.addActionListener(new ButtonListener());
smart.addActionListener(new ButtonListener());
reset.addActionListener(new ButtonListener());
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Get values from text fields
Object obj=e.getSource();
if(obj==reset)
{
name.setText("");
fans.setText("");
tubelights.setText("");
ACs.setText("");
fridges.setText("");
units.setText("");
}
else if(obj==ok)
{
String names=name.getText();
int fanNo=Integer.parseInt( fans.getText() );
int tubeNo=Integer.parseInt( tubelights.getText() );
int ACno=Integer.parseInt( ACs.getText() );
int fridgeNo=Integer.parseInt(fridges.getText() );
int unitConsumed=Integer.parseInt( units.getText() );
// Create a loan object
Appliance appliance= new Appliance(names,fanNo,tubeNo,ACno,fridgeNo,unitConsumed);
// Display monthly payment and total payment
appliance.calculate();
double bill=appliance.getBill();
String billDisplay="Your Bill is "+ String.valueOf(bill);
JOptionPane.showMessageDialog(null,billDisplay);
addRecords(appliance);
}
else if(obj==smart)
{
Analysis frame = new Analysis();
frame.pack();
frame.setTitle("Electricity Bill Mate");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
}
public static void openFile()
{
try{
output=new Formatter("New.txt");
}
catch(IOException e)
{
System.out.println("Error..Opening file ");
System.exit(-1);
}
}
public static void addRecords(Appliance appliance)
{
try
{
output.format("%s %d %d %d %d %d ",appliance.getName(),appliance.getFans(),appliance.getTubelights(),appliance.getAC(),appliance.getFridges(),appliance.getUnits());
}
catch (FormatterClosedException formatterClosedException)
{
System.err.println("Error writing to file. Terminating.");
}
catch (NoSuchElementException elementException)
{
System.err.println("Invalid input. Please try again.");
}
}
public static void closeFile()
{
try
{
if(output!=null)
output.close();
}
catch (FormatterClosedException formatterClosedException)
{
System.err.println("Error writing to file. Terminating.");
}
catch (NoSuchElementException elementException)
{
System.err.println("Invalid input. Please try again.");
}
}
public static void main(String[] args) {
ApplianceDemo frame = new ApplianceDemo();
frame.pack();
frame.setTitle("Electricity Bill Mate");
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
openFile();
closeFile();
}
}