हिंदी

Create a class item to store information of different items, existing in a shop. At least following is to be stored w.r.t. each item code, name, price, qty.

Advertisements
Advertisements

प्रश्न

Create a class item to store information of different items, existing in a shop. At least following is to be stored w.r.t. each item code, name, price, qty. Write a program to accept the data from user and store it permanently in the file. Also provide user with facility of searching and updating the data in file based on code of item.

कोड लेखन
Advertisements

उत्तर

import pickle

class Item:
    def __init__(self, code, name, price, quantity):
        self.code = code
        self.name = name
        self.price = price
        self.quantity = quantity

    def __str__(self):
        return (f"Code: {self.code}, Name: {self.name}, "
                f"Price: {self.price}, Quantity: {self.quantity}")


def add_item(filename):
    item = Item(
        int(input("Enter item code: ")),
        input("Enter item name: "),
        float(input("Enter price: ")),
        int(input("Enter quantity: "))
    )

    with open(filename, "ab") as file:
        pickle.dump(item, file)


def read_items(filename):
    items = []
    try:
        with open(filename, "rb") as file:
            while True:
                try:
                    items.append(pickle.load(file))
                except EOFError:
                    break
    except FileNotFoundError:
        pass
    return items


def search_item(filename, code):
    for item in read_items(filename):
        if item.code == code:
            return item
    return None


def update_item(filename, code):
    items = read_items(filename)
    found = False

    for item in items:
        if item.code == code:
            item.name = input("Enter new name: ")
            item.price = float(input("Enter new price: "))
            item.quantity = int(input("Enter new quantity: "))
            found = True
            break

    if found:
        with open(filename, "wb") as file:
            for item in items:
                pickle.dump(item, file)
        print("Item updated successfully.")
    else:
        print("Item not found.")


filename = "items.dat"
add_item(filename)

code = int(input("Enter code to search: "))
item = search_item(filename, code)
print(item if item else "Item not found.")

update_code = int(input("Enter code to update: "))
update_item(filename, update_code)

The records are serialized with pickle and stored permanently in the binary file items.dat. Updating is done by rewriting the records because inserting or changing a variable-length record directly in a binary file is unsafe.

shaalaa.com
  क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
अध्याय 7: Data File Handling - EXERCISE [पृष्ठ १४६]

APPEARS IN

सीबीएसई Computer Science with Python [English] Class 12
अध्याय 7 Data File Handling
EXERCISE | Q 22. | पृष्ठ १४६
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×