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

  • Random Module?

    What is the Random Module? The random module in Python is used to generate pseudo-random numbers. It’s perfect for: Random Module Methods with Examples 1. random() – Random float between 0.0 and 1.0 Generates a random floating-point number between 0.0 (inclusive) and 1.0 (exclusive). python import random # Example 1: Basic random float print(random.random()) # Output: 0.5488135079477204 # Example…

  • Lambda Functions in Python

    Lambda Functions in Python Lambda functions are small, anonymous functions defined using the lambda keyword. They can take any number of arguments but can only have one expression. Basic Syntax python lambda arguments: expression Simple Examples 1. Basic Lambda Function python # Regular function def add(x, y): return x + y # Equivalent lambda function add_lambda =…

  • Basic Character Classes

    Basic Character Classes Pattern Description Example Matches [abc] Matches any single character in the brackets a, b, or c [^abc] Matches any single character NOT in the brackets d, 1, ! (not a, b, or c) [a-z] Matches any character in the range a to z a, b, c, …, z [A-Z] Matches any character in the range A to Z A, B, C, …, Z [0-9] Matches…

  • ASCII ,Uni Code Related Functions in Python

    ASCII Code and Related Functions in Python ASCII (American Standard Code for Information Interchange) is a character encoding standard that assigns numerical values to letters, digits, punctuation marks, and other characters. Here’s an explanation of ASCII and Python functions that work with it. ASCII Basics Python Functions for ASCII 1. ord() – Get ASCII value of a…

  • replace(), join(), split(), rsplit(), and splitlines() methods in Python

    1. replace() Method Purpose: Replaces occurrences of a substring with another substring.Syntax: python string.replace(old, new[, count]) Examples: Example 1: Basic Replacement python text = “Hello World” new_text = text.replace(“World”, “Python”) print(new_text) # Output: “Hello Python” Example 2: Limiting Replacements (count) python text = “apple apple apple” new_text = text.replace(“apple”, “orange”, 2) print(new_text) # Output: “orange orange apple”…

Leave a Reply

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