Inventory and sale

This project uses the features of pickle module and manages two files(product and sale). All the reports are too basic and can be made more appealing using python print formatting features.

import os
import pickle
os.chdir('D:/csproject')

#Accepting data for Dictionary
def insertInventory():
    pid = int(input('Product ID:'))
    name = input('Enter Product Name:')
    stock = int(input('Enter Stock'))

    #Creating the dictionary
    rec = {'Pid':pid,'Name':name,'Stock':stock}

    #Writing the Dictionary
    f = open('product.dat','ab')
    pickle.dump(rec,f)
    f.close()

#Display the records
def showInventory():
    f = open('product.dat','rb')
    while True:
        try:
            rec = pickle.load(f)
            print('Produuct ID:',rec['Pid'])
            print('Name:',rec['Name'])
            print('Stock:',rec['Stock'])
        except EOFError:
            break
    f.close()

#Check Product availability
def checkProduct(pid):
    f = open('product.dat','rb')
    while True:
        try:
            rec = pickle.load(f)
            if rec['Pid'] == pid:
                return True
        except EOFError:
            break
    f.close()
    return False

# Check for stock before sale    
def checkstock(pid,qty):
    f = open('product.dat','rb')
    while True:
        try:
            rec = pickle.load(f)
            if rec['Pid'] == pid:
                if rec['Stock']<qty:
                    return False
        except EOFError:
            break
    return True
    f.close()

#Get the stock for a product
def getStock(pid):
    f = open('product.dat','rb')
    while True:
        try:
            rec = pickle.load(f)
            if rec['Pid'] == pid:
                return rec['Stock']
        except EOFError:
            break
    f.close()
    
 #Get the product name for a pid
def getPname(pid):
    f = open('product.dat','rb')
    while True:
        try:
            rec = pickle.load(f)
            if rec['Pid'] == pid:
                return rec['Name']
        except EOFError:
            break
    f.close()
    
#Searching a record based on Pid
def searchProduct(pid):
    f = open('product.dat','rb')
    flag = False
    while True:
        try:
            rec = pickle.load(f)
            if rec['Pid'] == pid:
                print('Product ID:',rec['Pid'])
                print('Product Name:',rec['Name'])
                print('Stock:',rec['Stock'])
                flag = True
        except EOFError:
            break
    if flag == False:
        print('No Records found')
    f.close()
    
# Sale a product
def sale():
    pid = int(input('Enter the product id to sale:'))
    qty = int(input('Enter the quantity to sale:'))
    price= int(input('Enter the price of product:'))
    total = qty*price
    if checkProduct(pid)==True and checkstock(pid,qty)==True:
        f = open('sale.dat','ab')
        d = {'Pid':pid,'Name':getPname(pid),'Qty':qty,'Total':total}
        pickle.dump(d,f)
        f.close()
        stock= getStock(pid)-qty
        updateStock(pid,stock)
    else:
        print('Stock is low or product not available')
        
#Display sale Report
def showSale():
    f = open('sale.dat','rb')
    print('Product ID', 'Pname', 'Qty', 'Total')
    while True:
        try:
            rec = pickle.load(f)
            print(rec['Pid'],end=' ')
            print(rec['Name'],end=' ')
            print(rec['Qty'],end=' ')
            print(rec['Total'])
        except EOFError:
            break
    f.close()
    
    
#Stock Modification for a product
def updateStock(pid,amt):
    f = open('product.dat','rb')
    reclst = []
    while True:
        try:
            rec = pickle.load(f)
            reclst.append(rec)
        except EOFError:
            break
    f.close()
    for i in range (len(reclst)):
        if reclst[i]['Pid']==pid:
            reclst[i]['Stock'] = amt
    f = open('product.dat','wb')
    for x in reclst:
        pickle.dump(x,f)
    f.close()            

#Deleting a record based on Product Id
def deleteRec(pid):
    f = open('product.dat','rb')
    reclst = []
    while True:
        try:
            rec = pickle.load(f)
            reclst.append(rec)
        except EOFError:
            break
    f.close()
    f = open('product.dat','wb')
    for x in reclst:
        if x['Pid']==pid:
            continue
        pickle.dump(x,f)
    f.close()  
    
print('Type 1 to insert Product.')
print('Type 2 to display Product.')
print('Type 3 to Search Product.')
print('Type 4 to update Stock.')
print('Type 5 to delete a product.')
print('Type 6 to sale a product.')
print('Type 7 to Sale Report.')
choice = int(input('Enter you choice:'))
if choice == 1:
    insertInventory()
if choice == 2:
    showInventory()
if choice == 3:
    pid = int(input('Enter a Pid to search:'))
    searchProduct(pid)
if choice == 4:
    pid = int(input('Enter a Pid:'))
    amt = int(input('Enter new stock:'))
    if checkProduct(pid)==True:
        updateStock(pid,amt)
    else:
        print('Invalid Product ID')
if choice == 5:
    pid = int(input('Enter a Product Id:'))
    if checkProduct(pid)==True:
        deleteRec(pid)
    else:
        print('Invalid Product ID')
if choice == 6:
    sale()
if choice == 7:
    showSale()
    

Just another Ashoksquest.org Initiativs site