1- # TODO REVIEW AND ENSURE THE PURPOSE OF THIS MODULE IS TO CONVERT
2- # Python program to convert the currency
3- # of one country to that of another country
4-
5- # Import the modules needed
6- import requests
7-
8- class Currency_convertor :
9- # empty dict to store the conversion rates
10- rates = {}
11-
12- def __init__ (self , url ):
13- data = requests .get (url ).json ()
14- # Extracting only the rates from the json data
1+ """
2+ CurrencyConverter: Converts an amount from one currency to another using live exchange rates.
3+ """
4+
5+ from api_handler import get_exchange_data
6+
7+ class CurrencyConverter :
8+ def __init__ (self ):
9+ data = get_exchange_data ()
1510 self .rates = data ["rates" ]
1611
17- # function to do a simple cross multiplication between
18- # the amount and the conversion rates
19- def convert (self , from_currency , to_currency , amount ):
20- initial_amount = amount
21- if from_currency != 'EUR' :
22- amount = amount / self .rates [from_currency ]
12+ def convert (self , from_currency : str , to_currency : str , amount : float ) -> float :
13+ """
14+ Convert amount between two currencies.
15+
16+ Args:
17+ from_currency: Source currency code (e.g., 'USD')
18+ to_currency: Target currency code (e.g., 'AUD')
19+ amount: Amount to convert
20+
21+ Returns:
22+ Converted amount as float
23+
24+ Raises:
25+ ValueError: If a currency code is invalid
26+ """
27+ from_currency = from_currency .upper ()
28+ to_currency = to_currency .upper ()
29+
30+ if from_currency not in self .rates or to_currency not in self .rates :
31+ raise ValueError ("Invalid currency code." )
2332
24- # limiting the precision to 2 decimal places
25- amount = round (amount * self .rates [to_currency ], 2 )
26- print ('{} {} = {} {}' .format (initial_amount , from_currency , amount , to_currency ))
33+ # Convert amount to base (EUR), then to target
34+ amount_in_base = amount if from_currency == "EUR" else amount / self .rates [from_currency ]
35+ converted_amount = round (amount_in_base * self .rates [to_currency ], 2 )
36+ return converted_amount
2737
28- # Driver code
38+ # --- DEBUG / MANUAL TEST ---
2939if __name__ == "__main__" :
40+ print ("Running manual test for CurrencyConverter...\n " )
3041
31- YOUR_ACCESS_KEY = 'YOUR_ACCESS_KEY_HERE' # Define your access key
32- url = f'http://data.fixer.io/api/latest?access_key={ YOUR_ACCESS_KEY } ' # Use f-string for cleaner concatenation
33- c = Currency_convertor (url )
34-
35- from_country = input ("From Country (currency code): " )
36- to_country = input ("To Country (currency code): " )
37- amount = float (input ("Amount: " )) # Use float for decimal support
42+ converter = CurrencyConverter ()
43+ from_cur = "USD"
44+ to_cur = "AUD"
45+ amt = 100.0
3846
39- c .convert (from_country , to_country , amount )
47+ print (f"Converting { amt } { from_cur } to { to_cur } ..." )
48+ try :
49+ result = converter .convert (from_cur , to_cur , amt )
50+ print (f"{ amt } { from_cur } = { result } { to_cur } " )
51+ except ValueError as e :
52+ print ("Error during conversion:" , e )
0 commit comments