-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaCurrencyCoverter.java
More file actions
207 lines (155 loc) · 6.11 KB
/
JavaCurrencyCoverter.java
File metadata and controls
207 lines (155 loc) · 6.11 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
//Program extends JFrame to use it's properties
public class JavaCurrencyCoverter extends JFrame {
//declares instance variables
private JButton convButton;
private JLabel convLabel;
private JLabel initCurrLabel;
private JLabel convCurrLabel;
private JLabel headerLabel;
private JTextField initCurrField;
private JPanel convPanel;
private JComboBox<String> initCurrCombo;
private JComboBox<String> convCurrCombo;
private double initAmnt;
private double convAmnt;
private static final int CONVFRAME_WIDTH = 370;
private static final int CONVFRAME_HEIGHT = 190;
private static final double EUR_TO_USD = 1.42;
private static final double GBP_TO_USD = 1.64;
private static final double GBP_TO_EUR = 1.13;
private final int CURR_FIELD_WIDTH = 9;
//Constructor calls createComponents & setSize function
public ChevonieDanielProgramV() {
createComponents();
setSize(CONVFRAME_WIDTH, CONVFRAME_HEIGHT);
}
//Main function sets main attributes of frame
public static void main(String[] args) {
JFrame convFrame = new ChevonieDanielProgramV();
convFrame.setTitle("Chevonie's Currency Converter");
convFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
convFrame.setVisible(true);
}
//Determines properties of the gui
public void createComponents() {
headerLabel = new JLabel("CHEVONIE'S CURRENCY CONVERTER");
initCurrLabel = new JLabel("Initial Currency Amount: ");
convCurrLabel = new JLabel("Convert to: ");
initCurrField = new JTextField(CURR_FIELD_WIDTH);
initCurrCombo = new JComboBox<String>();
initCurrCombo.addItem("USD");
initCurrCombo.addItem("GBP");
initCurrCombo.addItem("EUR");
convCurrCombo = new JComboBox<String>();
convCurrCombo.addItem("USD");
convCurrCombo.addItem("GBP");
convCurrCombo.addItem("EUR");
convButton = new JButton("CONVERT");
convLabel = new JLabel();
convPanel = new JPanel();
convPanel.add(headerLabel);
convPanel.add(initCurrLabel);
convPanel.add(initCurrCombo);
convPanel.add(initCurrField);
convPanel.add(convCurrLabel);
convPanel.add(convCurrCombo);
convPanel.add(convButton);
convPanel.add(convLabel);
add(convPanel);
ActionListener convButtonlistener = new convertListener();
convButton.addActionListener(convButtonlistener);
}
//Implements ActionListener in another class for convenience
public class convertListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
//Casts combo selection as string
String initCurrStr = (String) initCurrCombo.getSelectedItem();
String convCurrStr = (String) convCurrCombo.getSelectedItem();
//Try block contains conditions that determine what will be converted
try {
if (initCurrStr == "EUR" && convCurrStr == "USD") {
initAmnt = Double.parseDouble(initCurrField.getText());
convAmnt = initAmnt * EUR_TO_USD;
double roundedConvAmnt = Math.round(convAmnt * 100.0) / 100.0;
if (initAmnt >= 0 && initAmnt < 99999999999.0) {
convLabel.setText("$" + roundedConvAmnt);
}
else {
convLabel.setText("Your number must be between 0 and 99999999999");
}
}
else if (initCurrStr == "GBP" && convCurrStr == "USD") {
initAmnt = Double.parseDouble(initCurrField.getText());
convAmnt = initAmnt * GBP_TO_USD;
double roundedConvAmnt = Math.round(convAmnt * 100.0) / 100.0;
if (initAmnt >= 0 && initAmnt < 99999999999.0) {
convLabel.setText("$" + roundedConvAmnt);
}
else {
convLabel.setText("Your number must be between 0 and 99999999999");
}
}
else if (initCurrStr == "GBP" && convCurrStr == "EUR") {
initAmnt = Double.parseDouble(initCurrField.getText());
convAmnt = initAmnt * GBP_TO_EUR;
double roundedConvAmnt = Math.round(convAmnt * 100.0) / 100.0;
if (initAmnt >= 0 && initAmnt < 99999999999.0) {
convLabel.setText("€" + roundedConvAmnt);
}
else {
convLabel.setText("Your number must be between 0 and 99999999999");
}
}
else if (initCurrStr == "USD" && convCurrStr == "EUR") {
initAmnt = Double.parseDouble(initCurrField.getText());
convAmnt = initAmnt / EUR_TO_USD;
double roundedConvAmnt = Math.round(convAmnt * 100.0) / 100.0;
if (initAmnt >= 0 && initAmnt < 99999999999.0) {
convLabel.setText("€" + roundedConvAmnt);
}
else {
convLabel.setText("Your number must be between 0 and 99999999999");
}
}
else if (initCurrStr == "USD" && convCurrStr == "GBP") {
initAmnt = Double.parseDouble(initCurrField.getText());
convAmnt = initAmnt / GBP_TO_USD;
double roundedConvAmnt = Math.round(convAmnt * 100.0) / 100.0;
if (initAmnt >= 0 && initAmnt < 99999999999.0) {
convLabel.setText("£" + roundedConvAmnt);
}
else {
convLabel.setText("Your number must be between 0 and 99999999999");
}
}
else if (initCurrStr == "EUR" && convCurrStr == "GBP") {
initAmnt = Double.parseDouble(initCurrField.getText());
convAmnt = initAmnt / GBP_TO_EUR;
double roundedConvAmnt = Math.round(convAmnt * 100.0) / 100.0;
if (initAmnt >= 0 && initAmnt < 99999999999.0) {
convLabel.setText("£" + roundedConvAmnt);
}
else {
convLabel.setText("Your number must be between 0 and 99999999999");
}
}
//Warns that two currencies were not selected
else {
convLabel.setText("Please choose two different currencies");
}
}
//Catches invalid input and prompts for valid input
catch (NumberFormatException exp1) {
convLabel.setText("Invalid Number! Please enter a valid number");
}
}
}
}