-
Notifications
You must be signed in to change notification settings - Fork 13
/
Currency_Converter_Web_Application.html
320 lines (283 loc) · 9.68 KB
/
Currency_Converter_Web_Application.html
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Currency Converter</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/js/all.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
body {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: linear-gradient(45deg, #6b21a8, #3730a3);
padding: 20px;
}
.container {
background: white;
padding: 2rem;
border-radius: 1rem;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 400px;
}
h1 {
color: #1f2937;
margin-bottom: 1.5rem;
font-size: 1.5rem;
text-align: center;
}
.converter-box {
background: #f3f4f6;
padding: 1rem;
border-radius: 0.5rem;
margin-bottom: 1rem;
}
.amount-container {
display: flex;
align-items: center;
gap: 1rem;
margin-bottom: 1rem;
}
.amount-container label {
color: #4b5563;
font-size: 0.875rem;
flex-shrink: 0;
width: 80px;
}
input {
width: 100%;
padding: 0.5rem;
border: 1px solid #d1d5db;
border-radius: 0.25rem;
font-size: 1rem;
outline: none;
}
input:focus {
border-color: #6366f1;
box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
}
.currency-row {
display: flex;
align-items: center;
gap: 1rem;
margin-bottom: 1rem;
}
.currency-row label {
color: #4b5563;
font-size: 0.875rem;
flex-shrink: 0;
width: 80px;
}
select {
width: 100%;
padding: 0.5rem;
border: 1px solid #d1d5db;
border-radius: 0.25rem;
font-size: 1rem;
outline: none;
background-color: white;
}
select:focus {
border-color: #6366f1;
box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
}
.swap-btn {
display: flex;
align-items: center;
justify-content: center;
width: 2.5rem;
height: 2.5rem;
border: none;
background-color: #4f46e5;
color: white;
border-radius: 50%;
cursor: pointer;
margin: 0.5rem auto;
transition: background-color 0.2s;
}
.swap-btn:hover {
background-color: #4338ca;
}
.convert-btn {
width: 100%;
padding: 0.75rem;
background-color: #4f46e5;
color: white;
border: none;
border-radius: 0.5rem;
font-size: 1rem;
cursor: pointer;
transition: background-color 0.2s;
}
.convert-btn:hover {
background-color: #4338ca;
}
.result {
margin-top: 1rem;
padding: 1rem;
background-color: #f3f4f6;
border-radius: 0.5rem;
text-align: center;
}
.result-amount {
font-size: 1.5rem;
font-weight: bold;
color: #1f2937;
margin-bottom: 0.5rem;
}
.result-text {
color: #6b7280;
font-size: 0.875rem;
}
.loading {
display: none;
text-align: center;
margin: 1rem 0;
}
.loading.active {
display: block;
}
.error {
display: none;
color: #dc2626;
text-align: center;
margin-top: 1rem;
font-size: 0.875rem;
}
.error.active {
display: block;
}
</style>
</head>
<body>
<div class="container">
<h1>Currency Converter</h1>
<div class="converter-box">
<div class="amount-container">
<label for="amount">Amount</label>
<input type="number" id="amount" placeholder="Enter amount" min="0" step="0.01" value="1">
</div>
<div class="currency-row">
<label for="from">From</label>
<select id="from"></select>
</div>
<button class="swap-btn" id="swap">
<i class="fas fa-exchange-alt"></i>
</button>
<div class="currency-row">
<label for="to">To</label>
<select id="to"></select>
</div>
</div>
<button class="convert-btn" id="convert">Convert</button>
<div class="loading">Converting...</div>
<div class="error">An error occurred. Please try again.</div>
<div class="result">
<div class="result-amount">0.00</div>
<div class="result-text">1 USD = 0.00 EUR</div>
</div>
</div>
<script>
const currencies = {
USD: "United States Dollar",
EUR: "Euro",
GBP: "British Pound Sterling",
JPY: "Japanese Yen",
AUD: "Australian Dollar",
CAD: "Canadian Dollar",
CHF: "Swiss Franc",
CNY: "Chinese Yuan",
INR: "Indian Rupee",
NZD: "New Zealand Dollar",
// Add more currencies as needed
};
const fromSelect = document.getElementById('from');
const toSelect = document.getElementById('to');
const amountInput = document.getElementById('amount');
const convertBtn = document.getElementById('convert');
const swapBtn = document.getElementById('swap');
const resultAmount = document.querySelector('.result-amount');
const resultText = document.querySelector('.result-text');
const loadingElement = document.querySelector('.loading');
const errorElement = document.querySelector('.error');
// Populate currency dropdowns
Object.entries(currencies).forEach(([code, name]) => {
const option1 = new Option(`${code} - ${name}`, code);
const option2 = new Option(`${code} - ${name}`, code);
fromSelect.add(option1);
toSelect.add(option2);
});
// Set default values
fromSelect.value = 'USD';
toSelect.value = 'EUR';
// Swap currencies
swapBtn.addEventListener('click', () => {
const temp = fromSelect.value;
fromSelect.value = toSelect.value;
toSelect.value = temp;
convertCurrency();
});
// Convert currency
async function convertCurrency() {
const amount = parseFloat(amountInput.value);
const fromCurrency = fromSelect.value;
const toCurrency = toSelect.value;
if (isNaN(amount) || amount <= 0) {
errorElement.textContent = 'Please enter a valid amount';
errorElement.classList.add('active');
return;
}
try {
errorElement.classList.remove('active');
loadingElement.classList.add('active');
// For demonstration, we'll use a fixed exchange rate
// In a real application, you would fetch this from an API
const rate = await getMockExchangeRate(fromCurrency, toCurrency);
const convertedAmount = (amount * rate).toFixed(2);
resultAmount.textContent = `${convertedAmount} ${toCurrency}`;
resultText.textContent = `1 ${fromCurrency} = ${rate.toFixed(4)} ${toCurrency}`;
} catch (error) {
errorElement.textContent = 'Error fetching exchange rates. Please try again.';
errorElement.classList.add('active');
} finally {
loadingElement.classList.remove('active');
}
}
// Mock exchange rate function (replace with real API in production)
async function getMockExchangeRate(from, to) {
// Simulate API delay
await new Promise(resolve => setTimeout(resolve, 500));
const mockRates = {
'USD': 1,
'EUR': 0.85,
'GBP': 0.73,
'JPY': 110.0,
'AUD': 1.35,
'CAD': 1.25,
'CHF': 0.92,
'CNY': 6.45,
'INR': 74.5,
'NZD': 1.45,
};
const fromRate = mockRates[from];
const toRate = mockRates[to];
return toRate / fromRate;
}
// Event listeners
convertBtn.addEventListener('click', convertCurrency);
amountInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') convertCurrency();
});
// Initial conversion
convertCurrency();
</script>
</body>
</html>