Write a Python program for class, Flower, that has three instance variables of type str, int, and float that respectively represent the name of the flower, its number of petals, and its price. Your class must include a constructor method that initializes each variable to an appropriate value, and your class should include methods for setting the value of each type, and retrieving the value of each type.
Write a Python program for class, Flower, that has three instance variables of type str, int, and float
that respectively represent the name of the flower, its number of petals, and its price. Your class
must include a constructor method that initializes each variable to an appropriate value, and your
class should include methods for setting the value of each type, and retrieving the value of each
type.
class Flower:
"""
Represents a flower with a name, number of petals, and price.
"""
def __init__(self, name: str, petals: int, price: float):
"""
Initializes the Flower instance with the given attributes.
Args:
name (str): The name of the flower.
petals (int): The number of petals.
price (float): The price of the flower.
"""
self._name = name
self._petals = petals
self._price = price
# --- Methods for retrieving values (getters) ---
def get_name(self) -> str:
"""
Retrieves the name of the flower.
Returns:
str: The flower's name.
"""
return self._name
def get_petals(self) -> int:
"""
Retrieves the number of petals.
Returns:
int: The count of petals.
"""
return self._petals
def get_price(self) -> float:
"""
Retrieves the price of the flower.
Returns:
float: The flower's price.
"""
return self._price
# --- Methods for setting values (setters) ---
def set_name(self, new_name: str):
"""
Sets a new name for the flower.
Args:
new_name (str): The new name.
"""
if not isinstance(new_name, str):
raise ValueError("Name must be a string.")
self._name = new_name
def set_petals(self, new_petals: int):
"""
Sets a new number of petals.
Args:
new_petals (int): The new number of petals (must be a positive integer).
"""
if not isinstance(new_petals, int) or new_petals <= 0:
raise ValueError("Number of petals must be a positive integer.")
self._petals = new_petals
def set_price(self, new_price: float):
"""
Sets a new price for the flower.
Args:
new_price (float): The new price (must be a positive number).
"""
if not isinstance(new_price, (int, float)) or new_price <= 0:
raise ValueError("Price must be a positive number.")
self._price = float(new_price)
def __str__(self):
"""
Provides a string representation of the Flower object for easy printing.
"""
return f"Flower(Name: {self._name}, Petals: {self._petals}, Price: ${self._price:.2f})"
# --- Example Usage ---
# 1. Create a new Flower instance
rose = Flower("Rose", 35, 1.99)
# 2. Print initial values using getter methods
print(f"Initial Details: Name: {rose.get_name()}, Petals: {rose.get_petals()}, Price: ${rose.get_price():.2f}")
print(rose) # Uses the __str__ method
# 3. Modify values using setter methods
rose.set_petals(40)
rose.set_price(2.50)
rose.set_name("Hybrid Rose")
# 4. Print the updated details
print(f"Updated Details: Name: {rose.get_name()}, Petals: {rose.get_petals()}, Price: ${rose.get_price():.2f}")
print(rose)
Comments
Post a Comment