-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurrency.java
More file actions
75 lines (62 loc) · 2.25 KB
/
Currency.java
File metadata and controls
75 lines (62 loc) · 2.25 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
import java.util.Scanner;
// Base class: CurrencyConverter
class CurrencyConverter {
public double convert(double amount) {
return amount; // Default implementation (can be overridden)
}
}
// Subclass: USDConverter
class USDConverter extends CurrencyConverter {
@Override
public double convert(double amount) {
return amount * 290; // Conversion rate for USD to LKR
}
}
// Subclass: GBPConverter
class GBPConverter extends CurrencyConverter {
@Override
public double convert(double amount) {
return amount * 370; // Conversion rate for GBP to LKR
}
}
// Subclass: EURConverter
class EURConverter extends CurrencyConverter {
@Override
public double convert(double amount) {
return amount * 307; // Conversion rate for EUR to LKR
}
}
// Main class
public class Currency {
public static void main(String[] args) {
// Create objects for each currency converter
CurrencyConverter usdConverter = new USDConverter();
CurrencyConverter gbpConverter = new GBPConverter();
CurrencyConverter eurConverter = new EURConverter();
// User input
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the currency type (USD/GBP/EUR): ");
String currencyType = scanner.nextLine().toUpperCase();
System.out.println("Enter the amount to convert: ");
double amount = scanner.nextDouble();
// Variable to store converted amount
double convertedAmount = 0;
{
// Perform conversion without switch-case
if (currencyType.equals("USD")) {
convertedAmount = usdConverter.convert(amount);
} else if (currencyType.equals("GBP")) {
convertedAmount = gbpConverter.convert(amount);
} else if (currencyType.equals("EUR")) {
convertedAmount = eurConverter.convert(amount);
} else {
System.out.println("Invalid currency type!");
scanner.close();
return;
}
// Display the result
System.out.println("Converted amount in LKR: " + convertedAmount);
// Close scanner
scanner.close();
}
}