Hi there!
I just wanted to share with you some of the code that I just come back to today and I finally finished the project that I left unfinished some time ago. Could you please give me some feedback? What could I make better?
from datetime import datetime, time
class Art:
def __init__(self, artist, title, medium, year, owner):
self.artist = artist
self.title = title
self.medium = medium
self.year = year
self.owner = owner
self.owner.artworks_owned.append(self)
def __repr__(self):
return f"{self.artist}, '{self.title}', {self.year}, {self.medium}, {self.owner.name}, {self.owner.location}"
def is_for_sale(self):
for market in Marketplace.marketplaces:
for listing in market.listings:
if listing.art is self:
if listing.expiration_date > datetime.today():
return True
return False
def where_for_sale(self):
markets = []
for market in Marketplace.marketplaces:
for listing in market.listings:
if listing.art is self:
markets.append(market)
return markets
class Marketplace:
marketplaces = []
def __init__(self, name):
self.listings = []
self.name = name
Marketplace.marketplaces.append(self)
def __repr__(self):
return self.name
def add_listing(self, new_listing):
self.listings.append(new_listing)
def remove_listing(self, listing_to_remove):
if listing_to_remove in self.listings:
self.listings.remove(listing_to_remove)
def show_listings(self):
if len(self.listings) > 0:
for listing in self.listings:
if listing.expiration_date > datetime.today():
print(listing)
else:
print("There are no listings here yet!")
class Client:
all_clients = []
def __init__(self, name, is_museum, location="Private Collection", wallet=0):
self.name = name
self.location = location
self.is_musem = is_museum
self.artworks_owned = []
self.wallet = wallet
self.wishlist = []
Client.all_clients.append(self)
def __repr__(self):
return self.name + ", " + self.location
def list_artwork_for_sale(self, artwork, price, expiration_date,
*marketplaces):
if artwork.owner == self:
new_listing = Listing(artwork, price, expiration_date, self)
for marketplace in marketplaces:
marketplace.add_listing(new_listing)
def buy_artwork(self, artwork):
if artwork.owner is not self:
if artwork.is_for_sale():
markets = artwork.where_for_sale()
print(f"Buying '{artwork.title}' in progress...")
for market in markets:
for listing in market.listings:
if listing.art is artwork:
if self.wallet >= listing.price:
market.remove_listing(listing)
artwork.owner.artworks_owned.remove(artwork)
artwork.owner.wallet += listing.price
artwork.owner = self
self.artworks_owned.append(artwork)
self.wallet -= listing.price
else:
print(f"{self} doesn't have enough money to buy this artwork.")
return
for client in Client.all_clients:
if artwork in client.wishlist:
client.wishlist.remove(artwork)
time.sleep(1)
time.sleep(1)
print(f"{artwork.owner} just bought '{artwork.title}'!")
else:
print("This artwork is not for sale at the moment or the listing has expired.")
else:
print(f"{self} already owns this artwork.")
return
def add_to_wishlist(self, artwork):
if artwork.is_for_sale():
self.wishlist.append("---> " + repr(artwork))
print(self.wishlist)
else:
print("The artwork is not currently listed for sale.")
class Listing:
def __init__(self, art, price, expiration_date, seller):
self.art = art # instance of Art
self.price = price
self.expiration_date = datetime.fromisoformat(expiration_date)
self.seller = seller # instance of Client
def __repr__(self):
return f"{self.art.artist}, {self.art.title}, {self.expiration_date}, {self.price}"
# Marketplaces:
veneer = Marketplace("Veneer")
another_market = Marketplace("Another Market")
# Clients:
edytta = Client("Edytta Halpirt", False, wallet=11_000_000)
moma = Client("The MOMA", True, "New York", wallet=60_000_000)
# Artworks:
girl_with_mandolin = Art("Picasso, Pablo", "Girl with a Mandolin (Fanny Tellier)", "oil on canvas", 1910, edytta)
# print(moma.wallet)
# print(edytta.wallet)
# Creating an offer at the specified marketplace or at all of them
edytta.list_artwork_for_sale(girl_with_mandolin, 6000000, "2020-12-10 22:39", *Marketplace.marketplaces)
# Printing out available listings at the marketplaces
# another_market.show_listings()
# veneer.show_listings()
# # Checking if the artwork is for sale at the marketplaces and checking at which one we could buy it
# print(girl_with_mandolin.is_for_sale())
# print(girl_with_mandolin.where_for_sale())
# # Testing if Client.artworks_owned works
# print(edytta.artworks_owned)
print(edytta.artworks_owned)
print(moma.artworks_owned)
moma.buy_artwork(girl_with_mandolin)
print(edytta.artworks_owned)
print(moma.artworks_owned)
# veneer.show_listings()
# another_market.show_listings()
# print(moma.wallet)
# print(edytta.wallet)
# moma.add_to_wishlist(girl_with_mandolin)
# print(moma.wishlist)
# print(veneer.listings)
# Checking if we call the same argument
moma.buy_artwork(girl_with_mandolin)
# print(edytta.artworks_owned)
# print(moma.artworks_owned)