-1

The code should Capture products in stock (i.e. name, price, date supplied, supplier name, quantity), Retrieve the price when name is given upon each purchase and deducts the quantity in stock, Calculate the total price for each purchase and prints value indicating date and time of purchase, Sends an alert when quantity reaches 5 to shopkeeper and places order to supplier.

My code right now is not looping so that I can add a number of products then be able to access them, I tried using a while loop but it is running forever. Kindly help

import datetime
class Stock:
    def __init__(self, name, price, supplier, quantity,date):
        self.name = name
        self.price = price
        self.date = date
        self.supplier = supplier
        self. quantity = quantity

    def check_item(self, name):
        for i in range(len(ls)):
            if (ls[i].name == name):
                return i

    def sale(self):
        n = int(input("How many products to sale: "))
        total = 0
        for j in range(n):
            name = input("Enter Product name : ")
            quantity = int(input("Enter quantity: "))
            i = obj.check_item(name)
            if i and ls[i].quantity >= quantity:
                ls[i].quantity -= quantity
                if ls[i].quantity < 5:
                    print("Low Stock! Low Stock! Order Placed")
                    obj.place_order(name, ls[i].supplier, quantity+10)
                print("....Uncle G Shop....")
                print(datetime.date.today())
                print("Product Name  | Quantity  | Cost $")
                print(ls[i].name, end=" ")
                print(quantity, end=" ")
                print(ls[i].price * quantity)
                total += ls[i].price * quantity
                print("\n")
                print("Total Cost----->", "$" + total)
            else:
                print("Product out of stock or not enough quantity")


    def purchase(self):
        name = input("Enter Product name: ")
        date = datetime.date.today()
        i = obj.check_item(name)
        if i:
            ls[i].quantity += int(input("Enter quantity: "))
            ls[i].price = int(input("Enter product price: "))
            ls[i].date = date
        else:
            quantity = int(input("Enter quantity: "))
            price = int(input("Enter product price: "))
            supplier = input("Enter Supplier: ")
            ob = Stock(name, price, supplier, quantity, date)
            ls.append(ob)


    def place_order(self,name, supplier, quantity):
        return name, supplier, quantity

    def print_products(self):
        def __repr__(self):
            return str(self.name) + str(self.price) + str(supplier) + str(self.quantity) + str(self.date)
        return __repr__

    def main(self):
        print("Welcome To Uncle G Shop")
        print("choose an option below")
        print("\n1.Enter a Product\n2.Make a sale \n3.See all Products\n4.Exit")
        option = int(input("Enter option here: "))
        while True:
                

            if option == 1:
                obj.purchase()
            elif option == 2:
                obj.sale()
            elif option == 3:
                obj.print_products()
            elif option == 4:
                print("Have a good day")
                break
            else:
                print("Enter a valid input!")
# A list to add Products
ls = []

# an object of Stock class
obj = Stock('', 0, 0, 0, '')
obj.main()
7
  • Please present code that -- when run -- produces your issue. Or else specify which input has to be given to get an unexpected output (or error). Be specific. BTW: what about debugging? Did you step through your code, setting break points, inspecting variables, ...?
    – trincot
    Jun 5, 2021 at 16:18
  • Sorry I thought I had exhausted everything up. Right now when I run the code and I choose option 1 it runs infinitely, I want it to only a product the asks me to choose again what to do then I choose option 2 etc. In short I want it to be able to enter products the make a sale and then print whats left.
    – chaperoneg
    Jun 5, 2021 at 16:28
  • But... your loop's body does not ask for input. There is nothing in the loop that asks anything.
    – trincot
    Jun 5, 2021 at 16:35
  • @trincot I had put the loop in the main function which would call the appropriate methods.
    – chaperoneg
    Jun 5, 2021 at 17:07
  • Well yes, that input is happening, but you never ask for an option. You ask that only once before the loop
    – trincot
    Jun 5, 2021 at 17:18

2 Answers 2

0

Your main menu doesn't have a break from the while loop for option 4.

You also need to think again about how to use your class. At present, you have multiple methods referring to a variable, ls, created outside of the class. Either treat Stock as a class to deal with individual purchase records, or to deal with stock overall. You could maintain in the class itself a list of instances of the class and offer class methods, as well as instance methods, to manage that overall stock.

4
  • I forgot to put break when I had removed whilst I was doing some tests. I just started doing OOP so its still a bit tricky to me.
    – chaperoneg
    Jun 5, 2021 at 17:06
  • If you can please give me a hint on how I can achieve
    – chaperoneg
    Jun 5, 2021 at 18:22
  • Focus the class on purely representing a stock record for a single inventory item and on providing methods related to a stock record (reporting on it, changing price, increasing/decreasing quantity, etc). Jun 6, 2021 at 18:08
  • For handling overall inventory of stock records, either create an additional class, or simply write regular code/functions, to create a container (e.g. a list) of instances of those stock records and offer functions/methods to manipulate the inventory. Jun 6, 2021 at 18:22
0

Regarding the looping issue, you just need to move your option = int(input("Enter option here: ")) line down inside your while loop.

Beyond that you have a lot of other issues that you may have already resolved or completely forgot about since this is a year+ old... Lol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.