Complete the Mint and Coin classes so that the coins created by a mint have the correct year and worth. Each Mint instance has a year stamp. The update method sets the year stamp to the current_year class attribute of the Mint class. The create method takes a subclass of Coin and returns an instance of that class stamped with the mint's year (which may be different from Mint.current_year if it has not been updated.) A Coin's worth method returns the cents value of the coin plus one extra cent for each year of age beyond 50. A coin's age can be determined by subtracting the coin's year from the current_year class attribute of the Mint class.
时间: 2024-02-29 13:53:28 浏览: 14
Sure, here's an implementation of the Mint and Coin classes:
```
import datetime
class Mint:
current_year = datetime.datetime.now().year
def __init__(self, year):
self.year_stamp = year
def update(self):
self.year_stamp = self.current_year
def create(self, coin_class):
return coin_class(self.year_stamp)
class Coin:
def __init__(self, year):
self.year = year
def worth(self):
age = Mint.current_year - self.year
return self.cents + (max(0, age - 50) * 1)
class Quarter(Coin):
cents = 25
class Dime(Coin):
cents = 10
class Nickel(Coin):
cents = 5
class Penny(Coin):
cents = 1
```
Here's an example usage of the classes:
```
# Create a Mint with a year stamp of 1980
mint = Mint(1980)
# Create a Quarter stamped with the Mint's year
quarter = mint.create(Quarter)
# Update the Mint's year stamp to the current year
mint.update()
# Create a Penny stamped with the updated year
penny = mint.create(Penny)
# Determine the worth of the Quarter and Penny
print(quarter.worth()) # Prints 45 (25 cents + 20 extra cents for 70 years of age)
print(penny.worth()) # Prints 41 (1 cent + 40 extra cents for 41 years of age)
```