# Python Billing System

Write a Python Program to calculate sale bill for the selling items in a Fruit Shop.

Consider the following dictionary as a item database.

items = {

101:{'item_name':'Apple','Quantity_in_kgs':50,'Price_per_kg':200},

102:{'item_name':'Banana','Quantity_in_kgs':30,'Price_per_kg':80},

103:{'item_name':'Grapes','Quantity_in_kgs':25,'Price_per_kg':300},

104:{'item_name':'Lemon','Quantity_in_kgs':20,'Price_per_kg':70}

}

Initially the transaction dictionary is Null.

trans = {}

Code the following tasks to implement Billing System for a Fruit Shop.

1. Add Fruits to the System

a. Check whether the fruit is exists in database

b. if exists, Get id & Quantity and add the quantity to existing quantity

c. if not exists, Get id, name,quantity & price and add to dictionary

2. Calculate Bill During Sale.

a. Get id & quantity and calculate bill for one or more items

b. Calculate amount for each item

c.     Reduce the sold quantity of corresponding item in items database.

d. Add item to transaction as below

trans = {201:{101:[2,400],104:[.5,35]}}

Here in Transaction 201: 2 kg apples for \$400 and half kg lemon for

\$35 were sold.

e. Display items, quantity, amount and total on each item entry

3. Check Stock

a. Display the items and available quantity

b. Highlight the items which are available less than 5 kgs.

#### Program or Solution

``` items = { 101:{'item_name':'Apple','Quantity_in_kgs':50,'Price_per_kg':200}, 102:{'item_name':'Banana','Quantity_in_kgs':30,'Price_per_kg':80}, 103:{'item_name':'Grapes','Quantity_in_kgs':25,'Price_per_kg':300}, 104:{'item_name':'Lemon','Quantity_in_kgs':20,'Price_per_kg':70} }trans = {}#Stock Printingdef stock_check():    print("Item Name | Available Stock")    for item_id in items:                if items[item_id]['Quantity_in_kgs'] <= 5:                       print("----------------------------------------")            print("| ",items[item_id]['item_name'], " | ",items[item_id]['Quantity_in_kgs']," |")            print("----------------------------------------")        else:            print("| ",items[item_id]['item_name'], " | ",items[item_id]['Quantity_in_kgs']," |")#Add New Item to Databasedef add_new_item():    item_ids = list(items.keys())    item_id = max(item_ids) + 1    item_name = input("Enter Item Name:")    price = int(input("Enter Price Per Kilo Gram:"))    quantiy = int(input("Enter Quantity"))    item = {'item_name':item_name,'Quantity_in_kgs':quantiy,'Price_per_kg':price}    items[item_id]= item    print('Item Added')    print('Item id | Item Name | Quantity | Price ')    print(item_id, " | ", item_name, " | ", quantiy, " | ", price)#Update the Quantity of Existing Itemdef update_item(item_id):    quantiy = int(input("Enter Quantity"))    items[item_id]['Quantity_in_kgs'] = items[item_id]['Quantity_in_kgs'] + quantiy    print('Item Updated')    print('Item id | Item Name | Quantity | Price ')    print(item_id, " | ", items[item_id]['item_name'], " | ", items[item_id]['Quantity_in_kgs'], " | ", items[item_id]['Price_per_kg'] )#Stock Entrydef add_item():    item_name = input("Enter the Item Name")    print("item id | item name")    print("-----------------")    for item in items:        if item_name in items[item]['item_name']:            print(item, " | ", items[item]['item_name'])        item_id = int(input("Enter item id (if existing)/Enter 0 (if New)"))    if item_id == 0:        add_new_item()    else:        #Check for Valid Item ID        while not item_id in items: print("Not Valid Item id") item_id = int(input("Enter item id:"))        update_item(item_id)#Billingdef sale(trans_id):    total = 0    transaction_items = {}    while True:        item_id = int(input("Enter item id (0 if no item):"))        if item_id == 0:            break        else:            while not item_id in items:     print("Not Valid Item id")     item_id = int(input("Enter item id:"))            quantity = int(input("Enter Quantity:"))            amount = items[item_id]['Price_per_kg'] * quantity            items[item_id]['Quantity_in_kgs'] = items[item_id]['Quantity_in_kgs'] - quantity             total += amount            transaction_items[item_id] = [quantity,amount]            print('Item id | Item Name | Quantity | Price | Amount')            for item in transaction_items:                print(item, "| ", items[item]['item_name'], " | ",transaction_items[item][0], " | ", items[item]['Price_per_kg'], " | ", transaction_items[item][1])            print("---------------------------------------------------")            print("Total:\t\t\t ", total)    trans[trans_id] = transaction_items#Main Functionwhile True:    print("Billing System\n Enter Your Choice \n 1. Add Items \n 2. Sales \n 3. Check Stock \n 4. Exit ")    choice = int(input("Your Choice:"))    if choice == 1:        add_item()    elif choice == 2:                trans_ids = list(trans.keys())        if len(trans_ids) == 0:            trans_id = 201        else:            trans_id = max(trans_ids) + 1        sale(trans_id)    elif choice == 3:        stock_check()    elif choice == 4:        break    else:        print("Invalid Choice") ```

#### Program Explanation

add_item() method checks whether the item is existing item or new item to the fruit shop.

if item is new then it calls add_new_item() method else it call update_item() method.

sale() method gets detail of each item and fetch price from dictionary to calculate amount. amount will be added to the total. Sale() method finally decrease the sold quantity in item dictionary.

stock_check() method prints all items and its available quantity.