Currency Converter

Challenge: Currency Converter Class with Accessors & Mutators

Objective: Create a CurrencyConverter class that converts an amount from a foreign currency to your local currency, using accessor and mutator methods.


1. Class Properties (Instance Variables)

  • currency: The name of the foreign currency (e.g., “USD”, “AUD”).
  • rate: The conversion rate of that currency to your local currency (e.g., 1 USD = 70 INR).

2. Class Methods

  • __init__(self, currency, rate)
    • The constructor that takes the initial currency name and conversion rate.
    • Initializes the object’s properties with these values.
  • Accessor Methods (Getters)
    • get_currency(self)
      • Returns the current currency name.
    • get_rate(self)
      • Returns the current conversion rate.
  • Mutator Methods (Setters)
    • set_currency(self, new_currency)
      • Changes the currency name to new_currency.
    • set_rate(self, new_rate)
      • Changes the conversion rate to new_rate.
  • Core Functionality Method
    • convert(self, amount)
      • Takes an amount in the foreign currency.
      • Formula: amount * rate
      • Returns the converted value in your local currency.

3. Task Instructions

  1. Write the CurrencyConverter class with the constructor, two getters, two setters, and the convert method.
  2. Create an object of the class (e.g., for “USD” with a rate of 70).
  3. Test the class by:
    • Using the convert method to convert an amount (e.g., 100 USD).
    • Using the setter methods to change the currency and rate (e.g., to “AUD” with a rate of 50).
    • Using the getter methods to check the new currency and rate.
    • Calling the convert method again with a new amount to verify the updated conversion works.

class CurrencyConverter:

    def __init__(self, name, rate):
        self.currency = name
        self.rate = rate

    def get_currency(self):
        return self.currency

    def get_rate(self):
        return self.rate

    def set_currency(self, name):
        self.currency = name

    def set_rate(self, rate):
        self.rate = rate

    def convert(self, amount):
        return self.currency + ' conversion is ' + str(self.rate * amount)


cc = CurrencyConverter('USD', 70)
print(cc.convert(100))

cc.set_currency('AUD')
cc.set_rate(50)

print(cc.convert(100))

Similar Posts

  •  List Comprehensions 

    List Comprehensions in Python (Basic) with Examples List comprehensions provide a concise way to create lists in Python. They are more readable and often faster than using loops. Basic Syntax: python [expression for item in iterable if condition] Example 1: Simple List Comprehension Create a list of squares from 0 to 9. Using Loop: python…

  • Python Statistics Module

    Python Statistics Module: Complete Methods Guide with Examples Here’s a detailed explanation of each method in the Python statistics module with 3 practical examples for each: 1. Measures of Central Tendency mean() – Arithmetic Average python import statistics as stats # Example 1: Basic mean calculation data1 = [1, 2, 3, 4, 5] result1 = stats.mean(data1) print(f”Mean of…

  • re module

    The re module is Python’s built-in module for regular expressions (regex). It provides functions and methods to work with strings using pattern matching, allowing you to search, extract, replace, and split text based on complex patterns. Key Functions in the re Module 1. Searching and Matching python import re text = “The quick brown fox jumps over the lazy dog” # re.search()…

  • date time modules class55

    In Python, the primary modules for handling dates and times are: 🕰️ Key Built-in Modules 1. datetime This is the most essential module. It provides classes for manipulating dates and times in both simple and complex ways. Class Description Example Usage date A date (year, month, day). date.today() time A time (hour, minute, second, microsecond,…

  • Functions as Parameters in Python

    Functions as Parameters in Python In Python, functions are first-class objects, which means they can be: Basic Concept When we pass a function as a parameter, we’re essentially allowing one function to use another function’s behavior. Simple Examples Example 1: Basic Function as Parameter python def greet(name): return f”Hello, {name}!” def farewell(name): return f”Goodbye, {name}!” def…

  • Password Strength Checker

    python Enhanced Password Strength Checker python import re def is_strong(password): “”” Check if a password is strong based on multiple criteria. Returns (is_valid, message) tuple. “”” # Define criteria and error messages criteria = [ { ‘check’: len(password) >= 8, ‘message’: “at least 8 characters” }, { ‘check’: bool(re.search(r'[A-Z]’, password)), ‘message’: “one uppercase letter (A-Z)”…

Leave a Reply

Your email address will not be published. Required fields are marked *