-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.java
More file actions
199 lines (173 loc) · 6.58 KB
/
Copy pathMainWindow.java
File metadata and controls
199 lines (173 loc) · 6.58 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
package currencyConverter;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import java.util.ResourceBundle;
public class MainWindow extends JFrame {
private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("localization.translation"); //$NON-NLS-1$
private JPanel contentPane;
private JTextField fieldAmount;
private ArrayList<Currency> currencies = Currency.init();
/**
* Create the mainWindow frame
*/
public MainWindow() {
setTitle(BUNDLE.getString("MainWindow.this.title")); //$NON-NLS-1$
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 589, 300);
setLocationRelativeTo(null);
setResizable( false );
// Create menu bar
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
// "File" menu
JMenu mnFile = new JMenu(BUNDLE.getString("MainWindow.mnFile.text")); //$NON-NLS-1$
mnFile.setMnemonic(KeyEvent.VK_F);
menuBar.add(mnFile);
// "Quit" menu item
JMenuItem mntmQuit = new JMenuItem(BUNDLE.getString("MainWindow.mntmQuit.text")); //$NON-NLS-1$
mntmQuit.setMnemonic(KeyEvent.VK_Q);
mntmQuit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
mnFile.add(mntmQuit);
// "Help" menu
JMenu mnHelp = new JMenu(BUNDLE.getString("MainWindow.mnHelp.text")); //$NON-NLS-1$
mnHelp.setMnemonic(KeyEvent.VK_H);
menuBar.add(mnHelp);
// "About" menu item
JMenuItem mntmAbout = new JMenuItem(BUNDLE.getString("MainWindow.mntmAbout.text")); //$NON-NLS-1$
mntmAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AboutWindow.getInstance().setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
});
mntmAbout.setMnemonic(KeyEvent.VK_A);
mnHelp.add(mntmAbout);
// Window components
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// Label "Convert"
JLabel lblConvert = new JLabel(BUNDLE.getString("MainWindow.lblConvert.text")); //$NON-NLS-1$
lblConvert.setHorizontalAlignment(SwingConstants.RIGHT);
lblConvert.setBounds(0, 14, 92, 15);
contentPane.add(lblConvert);
// ComboBox of the first currency
final JComboBox<String> comboBoxCountry1 = new JComboBox<String>();
comboBoxCountry1.setBounds(147, 7, 240, 28);
populate(comboBoxCountry1, currencies);
contentPane.add(comboBoxCountry1);
// Label "To"
JLabel lblTo = new JLabel(BUNDLE.getString("MainWindow.lblTo.text")); //$NON-NLS-1$
lblTo.setHorizontalAlignment(SwingConstants.RIGHT);
lblTo.setBounds(66, 54, 26, 15);
contentPane.add(lblTo);
// ComboBox of the second currency
final JComboBox<String> comboBoxCountry2 = new JComboBox<String>();
comboBoxCountry2.setBounds(147, 47, 240, 28);
populate(comboBoxCountry2, currencies);
contentPane.add(comboBoxCountry2);
// Label "Amount"
final JLabel lblAmount = new JLabel(BUNDLE.getString("MainWindow.lblAmount.text")); //$NON-NLS-1$
lblAmount.setHorizontalAlignment(SwingConstants.RIGHT);
lblAmount.setBounds(23, 108, 69, 15);
contentPane.add(lblAmount);
// Textfield where the user
fieldAmount = new JTextField();
fieldAmount.setText("0.00");
fieldAmount.setBounds(147, 101, 103, 29);
contentPane.add(fieldAmount);
fieldAmount.setColumns(10);
fieldAmount.setDocument(new JTextFieldLimit(8));
// Label displaying result of conversion
final JLabel lblResult = new JLabel("");
lblResult.setHorizontalAlignment(SwingConstants.LEFT);
lblResult.setBounds(147, 188, 428, 38);
contentPane.add(lblResult);
// Button "Convert"
JButton btnConvert = new JButton(BUNDLE.getString("MainWindow.btnConvert.text")); //$NON-NLS-1$
btnConvert.setBounds(147, 142, 129, 38);
btnConvert.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String nameCurrency1 = comboBoxCountry1.getSelectedItem().toString();
String nameCurrency2 = comboBoxCountry2.getSelectedItem().toString();
String result;
String formattedPrice;
String formattedAmount;
Double price;
Double amount = 0.0;
DecimalFormat format = new DecimalFormat("#0.00");
try {
amount = Double.parseDouble( fieldAmount.getText() ) ;
} catch (NumberFormatException e) {
e.printStackTrace();
amount = 0.0;
}
price = convert(nameCurrency1, nameCurrency2, currencies, amount);
// Format numbers to avoid "E7" problem
formattedPrice = format.format(price);
formattedAmount = format.format(amount);
result = formattedAmount + " " + nameCurrency1 + " = " + formattedPrice + " " + nameCurrency2;
lblResult.setText(result);
}
});
contentPane.add(btnConvert);
}
// Fill comboBox with currencies name
public static void populate(JComboBox<String> comboBox, ArrayList<Currency> currencies) {
for (Integer i = 0; i < currencies.size(); i++) {
comboBox.addItem( currencies.get(i).getName() );
}
}
// Find the short name and the exchange value of the second currency
public static Double convert(String currency1, String currency2, ArrayList<Currency> currencies, Double amount) {
String shortNameCurrency2 = null;
Double exchangeValue;
Double price = 0.0;
// Find shortname for the second currency
for (Integer i = 0; i < currencies.size(); i++) {
if (currencies.get(i).getName() == currency2) {
shortNameCurrency2 = currencies.get(i).getShortName();
break;
}
}
// Find exchange value and call convert() to calcul the new price
if (shortNameCurrency2 != null) {
for (Integer i = 0; i < currencies.size(); i++) {
if (currencies.get(i).getName() == currency1) {
exchangeValue = currencies.get(i).getExchangeValues().get(shortNameCurrency2);
price = Currency.convert(amount, exchangeValue);
break;
}
}
}
return price;
}
}