def buy(buyer, product): # get the seller for product from catalogue seller = Market.catalogue[product] # call seller's sold function seller.sold() logging.info("[Market]:Notify Seller %s about the sale of product %s ", seller.name, product.name) # deduct price from user's balance buyer.deduct(product.price) logging.info( "[Market]:Get the amount $ %s from buyer %s for product %s ", product.price, buyer.name, product.name) # track user GoogleAds.track_user_purchase(buyer, product) logging.info( "[Market]:Add buyer %s purchased the product %s to google Add track history", buyer.name, product.name)
def buy(buyer, product_list): # give discount for more than 2 products if len(product_list) >= 2: discount_factor = 0.9 else: discount_factor = 1 for product in product_list: # get the seller for product from catalogue seller = Market.catalogue[product] # call seller's sold function which returns True if product is available; otherwise transaction fails if seller.sold(product): # deduct price from user's balance buyer.deduct(product.price*discount_factor) # track user GoogleAds.track_user_purchase(buyer, product) else: return False return True
def buy(buyer, product): Market.lock.acquire() # get the seller for product from catalogue storage_dict = Market.catalogue[product] seller_list = [seller for seller in storage_dict if storage_dict[seller] > 0] market_storage = sum([seller.product_storage[product] for seller in seller_list]) # call seller's sold function if len(seller_list) > 0: if market_storage > shortage: seller_index = 0 min_price = seller_list[0].price_history[product][-1] for seller in seller_list: index = 0 if seller.price_history[product][-1] < min_price: seller_index = index index += 1 seller = seller_list[seller_index] seller.sold(product) Market.catalogue[product][seller] -= 1 # deduct price from user's balance buyer.deduct(seller.price_history[product][-1]) # track user GoogleAds.track_user_purchase(buyer, product) Market.lock.release() return seller else: price = max([seller.price_history[product][-1]] for seller in seller_list) deal, seller, buyer = Auctioneer.bid_buy(buyer, product, market_storage, seller_list, price) if deal: Market.catalogue[product][seller] -= 1 GoogleAds.track_user_purchase(buyer, product) else: Market.lock.release() return 0
def buy(buyer, products): # products: list of product of the same type, such as the iPhone XS product_id = products[0].product_id product_name = products[0].product_name product_price = products[0].stock_price seller_id = products[0].seller_id promotion_id = -1 # flag to check if a promotion applied most_valuable_customer_id = mysql.find_most_valuable_customer( seller_id) # get the seller for product from catalogue for product in products: # # random choose seller # seller_index = choice(Market.catalogue[product.product_name]) # index = Market.catalogue[product.product_name].index(seller_index) # seller = Market.catalogue[product.product_name][index] # find seller in the market catalogue for x in Market.catalogue.get(product.product_name): if x.id == seller_id: seller = x # find the buyer who has spent the most in the same seller and offer promotion with id 1 if most_valuable_customer_id == buyer.id: product_price = product.stock_price * 0.95 promotion_id = 1 # call seller's sold function seller.sold(product) # deduct price from user's balance buyer.deduct(product.stock_price) # track user GoogleAds.track_user_purchase(buyer, product) # update the stock of product products[0].stock_quantity = products[0].stock_quantity - len(products) if products[0].stock_quantity < 0: # print("stock < 0") pass else: logging.info( "[Market]: Update the stock quantity to %d of product %s of seller %s %d", products[0].stock_quantity, product_name, seller.name, seller_id) # update the stock of product in database mysql.update_stock(product_id, seller_id, products[0].stock_quantity, products[0].stock_cost, seller.wallet) dt = datetime.datetime.now() transaction = Transaction(datetime=dt, seller_id=seller.id, customer_id=buyer.id, product_id=product_id, quantity=len(products), total_amount=product_price * len(products)) logging.info( "[Market]:Transaction between Seller %s and Customer %s with the product %s at %s " "in year %s and quarter %s", seller.name, buyer.name, product_name, transaction.timestamp, transaction.year, transaction.quarter) # add the profit to seller's wallet seller.wallet += product_price * len(products) # if a promotion has been applied in calculating, then update the transaction promotion id property if promotion_id != -1: transaction.set_promotion_id(promotion_id) # if a customer makes a single transaction > $2000, offer a $50 off if transaction.total_amount >= 2000: transaction.total_amount -= 50 # if no promotion has been applied to this customer, then update the transaction promotion to id 5 if promotion_id == -1: transaction.set_promotion_id(5) # write to database mysql.save_txn(transaction)