关于使用字典添加和删除产品的库存问题

时间:2019-04-28 09:12:19

标签: python python-3.x dictionary inventory-management

我正在从事这样的任务,在一家公司中,他们想销售名为'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")

执行程序后,我在程序中有菜单。

  1. 您要添加产品库存!
  2. 想销售产品吗?

,但输出未得到其余quantityvaluation的正确输出。

任何建议,如何使其正确?

0 个答案:

没有答案