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

  • Challenge Summary: Inheritance – Polygon and Triangle Classes

    Challenge Summary: Inheritance – Polygon and Triangle Classes Objective: Create two classes where Triangle inherits from Polygon and calculates area using Heron’s formula. 1. Polygon Class (Base Class) Properties: Methods: __init__(self, num_sides, *sides) python class Polygon: def __init__(self, num_sides, *sides): self.number_of_sides = num_sides self.sides = list(sides) 2. Triangle Class (Derived Class) Inheritance: Methods: __init__(self, *sides) area(self) python import math…

  • Static Methods

    The primary use of a static method in Python classes is to define a function that logically belongs to the class but doesn’t need access to the instance’s data (like self) or the class’s state (like cls). They are essentially regular functions that are grouped within a class namespace. Key Characteristics and Use Cases General…

  • difference between positional and keyword arguments

    1. Positional Arguments How they work: The arguments you pass are matched to the function’s parameters based solely on their order (i.e., their position). The first argument is assigned to the first parameter, the second to the second, and so on. Example: python def describe_pet(animal_type, pet_name): “””Display information about a pet.””” print(f”\nI have a {animal_type}.”) print(f”My {animal_type}’s name…

  •  Duck Typing

    Python, Polymorphism allows us to use a single interface (like a function or a method) for objects of different types. Duck Typing is a specific style of polymorphism common in dynamically-typed languages like Python. What is Duck Typing? 🦆 The name comes from the saying: “If it walks like a duck and it quacks like…

  • append(), extend(), and insert() methods in Python lists

    append(), extend(), and insert() methods in Python lists, along with slicing where applicable. 1. append() Method Adds a single element to the end of the list. Examples: 2. extend() Method Adds multiple elements (iterable items) to the end of the list. Examples: 3. insert() Method Inserts an element at a specific position. Examples: Key Differences: Method Modifies List? Adds Single/Multiple Elements? Position append() ✅ Yes Single element (even if it’s a list) End…

Leave a Reply

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