我正在从事这样的任务,在一家公司中,他们想销售名为'computer'
的产品。
例如
valuation = quantity * purchase price
我的代码
import datetime
class Supplier:
def add_supplier(self, name, address, email, contact_no):
"""
Function: Used to add supplier details.
Parameter:name: Supplier Name
address: Supplier address
email: Supplier email
contact_no: Supplier Contact detail
Return: Nothing
"""
self.name = name
self.address = address
self.email = email
self.contact_no = contact_no
class Product:
def add_product(self, name):
"""
Function: To add product in to an inventory.
Parameter: Product name
Return: Nothing
"""
self.name = name
class Company(Supplier, Product):
data_dict = []
def purchase(self, product_obj, qty, price, date=datetime.date.today()):
"""
Function: Used to purchase product.
Parameter: product_obj: Product object.
qty: Product quantity.
price: Product price.
date: Product date.
Return: Nothing
"""
self.data_dict.append({'product': product_obj.name, 'qty': qty, 'price': price, 'date': str(date)})
def sale(self, sale_qty, sale_date):
"""
Function: To sale product from warehouse.
Parameter: sale_qty: Product sell quantity.
sale_date: Product sell date.
Return: Nothing
"""
self.newdict = (sorted(self.data_dict, key=lambda x: x['date']))
assert sale_qty > 0
unit_val = 0
rqilr = 0
rpilr = 0
qty = 0
price = 0
sum_qty = 0
sum_price = 0
#print(newdict) # testing.
for i, row in enumerate(self.newdict):
if row['date'] > sale_date:
print("Sorry, not enough quantity.\nStart the purchase order procedure pls!\n")
qty += row['qty']
price += row['price']
unit_val = price/qty
# we have enough Computers in stock.
if qty >= sale_qty:
break
else: # loop completes normally
for i in self.newdict:
sum_price += i['price']
sum_qty += i['qty']
print("Sorry, not enough qty. Operation cancelled.\nCurrent quantity = {} and valuation = {}".format(sum_qty, sum_price))
# remaining_qty_in_last_row sort name = rqilr
rqilr = qty - sale_qty
rpilr = rqilr * unit_val
self.newdict = [{**row, 'price': rpilr, 'qty': rqilr}] + self.newdict[i + 1:]
#print(self.newdict)
# For current quantity and valuation
for i in self.newdict:
sum_price += i['price']
sum_qty += i['qty']
print("Sold!\n available quantity = {} and valuation = {}".format(sum_qty, sum_price))
C = Company()
PRODUCT_OBJ = Product()
PRODUCT_OBJ.add_product('Computer')
while True:
option = int(input("""1. You want to add stock of the product!"
2. Want to sale product?
"""))
if option == 1:
qty = int(input("Enter the qty of the product.\n"))
price = float(input("Enter the price of the product.\n"))
purchase_date = input("Enter purchase date.\n")
C.purchase(PRODUCT_OBJ, qty, price, purchase_date)
elif option == 2:
qty = int(input("Enter the qty you wanna sale, pal!"))
sale_date = input("Enter sale date.\n")
C.sale(qty, sale_date)
else:
print("Enter only 1 & 2 option.\n")
执行程序后,我在程序中有菜单。
,但输出未得到其余quantity
和valuation
的正确输出。
任何建议,如何使其正确?