-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathCurrencyAndTodoApp.java
More file actions
136 lines (112 loc) · 4.73 KB
/
Copy pathCurrencyAndTodoApp.java
File metadata and controls
136 lines (112 loc) · 4.73 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.HashMap;
public class CurrencyAndTodoApp extends JFrame implements ActionListener {
// Currency Converter components
JComboBox<String> fromCurrency, toCurrency;
JTextField amountField, resultField;
JButton convertButton;
// To-Do List components
DefaultListModel<String> listModel;
JList<String> taskList;
JTextField taskField;
JButton addButton, removeButton, clearButton;
// Exchange rates (for simplicity, using fixed rates)
HashMap<String, Double> exchangeRates;
public CurrencyAndTodoApp() {
setTitle("Currency Converter & To-Do List");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(2, 1));
// Initialize exchange rates
exchangeRates = new HashMap<>();
exchangeRates.put("USD", 1.0);
exchangeRates.put("EUR", 0.85);
exchangeRates.put("GBP", 0.76);
exchangeRates.put("INR", 84.0);
exchangeRates.put("JPY", 110.0);
// Currency Converter panel
JPanel currencyPanel = new JPanel();
currencyPanel.setLayout(new GridLayout(4, 2, 10, 10));
currencyPanel.setBorder(BorderFactory.createTitledBorder("Currency Converter"));
currencyPanel.add(new JLabel("Amount:"));
amountField = new JTextField();
currencyPanel.add(amountField);
currencyPanel.add(new JLabel("From:"));
fromCurrency = new JComboBox<>(new String[]{"USD", "EUR", "GBP", "INR", "JPY"});
currencyPanel.add(fromCurrency);
currencyPanel.add(new JLabel("To:"));
toCurrency = new JComboBox<>(new String[]{"USD", "EUR", "GBP", "INR", "JPY"});
currencyPanel.add(toCurrency);
convertButton = new JButton("Convert");
convertButton.addActionListener(this);
currencyPanel.add(convertButton);
resultField = new JTextField();
resultField.setEditable(false);
currencyPanel.add(resultField);
add(currencyPanel);
// To-Do List panel
JPanel todoPanel = new JPanel();
todoPanel.setLayout(new BorderLayout());
todoPanel.setBorder(BorderFactory.createTitledBorder("To-Do List"));
listModel = new DefaultListModel<>();
taskList = new JList<>(listModel);
taskField = new JTextField(20);
addButton = new JButton("Add Task");
removeButton = new JButton("Remove Task");
clearButton = new JButton("Clear All");
addButton.addActionListener(this);
removeButton.addActionListener(this);
clearButton.addActionListener(this);
JPanel taskInputPanel = new JPanel();
taskInputPanel.add(taskField);
taskInputPanel.add(addButton);
JPanel taskActionPanel = new JPanel();
taskActionPanel.add(removeButton);
taskActionPanel.add(clearButton);
todoPanel.add(new JScrollPane(taskList), BorderLayout.CENTER);
todoPanel.add(taskInputPanel, BorderLayout.NORTH);
todoPanel.add(taskActionPanel, BorderLayout.SOUTH);
add(todoPanel);
}
// Handle button actions
public void actionPerformed(ActionEvent e) {
if (e.getSource() == convertButton) {
// Currency Conversion Logic
try {
double amount = Double.parseDouble(amountField.getText());
String from = fromCurrency.getSelectedItem().toString();
String to = toCurrency.getSelectedItem().toString();
double fromRate = exchangeRates.get(from);
double toRate = exchangeRates.get(to);
double result = (amount / fromRate) * toRate;
resultField.setText(String.format("%.2f %s", result, to));
} catch (NumberFormatException ex) {
resultField.setText("Invalid amount");
}
} else if (e.getSource() == addButton) {
// Add Task Logic
String task = taskField.getText();
if (!task.isEmpty()) {
listModel.addElement(task);
taskField.setText(""); // Clear the input field after adding
}
} else if (e.getSource() == removeButton) {
// Remove selected task
int selectedIndex = taskList.getSelectedIndex();
if (selectedIndex != -1) {
listModel.remove(selectedIndex);
}
} else if (e.getSource() == clearButton) {
// Clear all tasks
listModel.clear();
}
}
// Main method to run the application
public static void main(String[] args) {
CurrencyAndTodoApp app = new CurrencyAndTodoApp();
app.setVisible(true);
}
}