-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// multiCurrencySupport.js | ||
|
||
class MultiCurrencySupport { | ||
constructor() { | ||
this.currencies = { | ||
USD: 1, // Base currency | ||
EUR: 0.85, | ||
GBP: 0.75, | ||
BTC: 0.000025, // Example conversion rate for Bitcoin | ||
ETH: 0.0004, // Example conversion rate for Ethereum | ||
}; | ||
} | ||
|
||
// Convert an amount from one currency to another | ||
convert(amount, fromCurrency, toCurrency) { | ||
if (!this.currencies[fromCurrency] || !this.currencies[toCurrency]) { | ||
throw new Error("Unsupported currency."); | ||
} | ||
const baseAmount = amount / this.currencies[fromCurrency]; | ||
return baseAmount * this.currencies[toCurrency]; | ||
} | ||
|
||
// Get available currencies | ||
getAvailableCurrencies() { | ||
return Object.keys(this.currencies); | ||
} | ||
|
||
// Get conversion rate between two currencies | ||
getConversionRate(fromCurrency, toCurrency) { | ||
if (!this.currencies[fromCurrency] || !this.currencies[toCurrency]) { | ||
throw new Error("Unsupported currency."); | ||
} | ||
return this.currencies[toCurrency] / this.currencies[fromCurrency]; | ||
} | ||
} | ||
|
||
// Example usage | ||
const currencySupport = new MultiCurrencySupport(); | ||
const amountInUSD = 100; | ||
const amountInEUR = currencySupport.convert(amountInUSD, 'USD', 'EUR'); | ||
console.log(`$${amountInUSD} is equivalent to €${amountInEUR.toFixed(2)}`); | ||
|
||
const availableCurrencies = currencySupport.getAvailableCurrencies(); | ||
console.log("Available currencies:", availableCurrencies.join(', ')); | ||
|
||
const conversionRate = currencySupport.getConversionRate('USD', 'BTC'); | ||
console.log(`Conversion rate from USD to BTC: ${conversionRate}`); | ||
|
||
export default MultiCurrencySupport; |