Wednesday, 9 April 2025

GRADE 12 : PRACTICAL 9 : BINARY FILES

 '''Write a program to create a binary file Course.dat

for DIIT training centre to store course information of

all the computer language courses held at their institute.

Each record should be of the form :

[ course_id, course_name, duration(in months), fee].

The program should include the following functionalities:

i) Append new course records to the binary file.

ii) Display all course records from the binary file.

iii) Search for a course by course_id and display its

details, otherwise display an error.

iv) Calculate the average fees of the courses at DIIT

centre.

v) Increase the fees of each course by 10%'''

import pickle

def menu():

    while True :

        ch=int(input("""MENU :

1) ADD record

2) DISPLAY records

3) SEARCH for record

4) CALCULATE the average fees of the course

5) UPDATE record

6) EXIT

YOUR CHOICE : """))

        if ch==1:

            add()

        elif ch==2:

            display()

        elif ch==3:

            search()

        elif ch==4:

            calculate()

        elif ch==5:

            update()

        else:

            break


def add():

    with open('course.dat','ab') as f:

        n=int(input("Enter number of records : "))

        for i in range(n):

            c_id=int(input("Enter course id : "))

            c_name=input("Enter course name : ")

            duration=int(input("Enter the course duration(months) : "))

            fee=int(input("Enter the fees : "))

            rec=[c_id,c_name,duration,fee]

            pickle.dump(rec,f)


def display():

    try:

        with open('course.dat','rb') as f:

            while True:

                rec=pickle.load(f)

                print(rec)

    except EOFError:

        pass

    except Exception as e:

        print(e)


def search():

    c_id=int(input("Enter course id : "))

    try:

        with open('course.dat','rb') as f:

            while True:

                rec=pickle.load(f)

                if c_id==rec[0]:

                    print(rec)

                    break

    except EOFError:

        print(f"Course no {c_id} not found")

    except Exception as e:

        print(e)


def calculate():

    fees=[]

    try:

        with open('course.dat','rb') as f:

            while True:

                rec=pickle.load(f)

                fees.append(rec[3])

    except EOFError:

        avg=sum(fees)/len(fees)

        print(f"Average fees ={avg}")

    except Exception as e:

        print(e)



def update():

    l=[]

    try:

        with open('course.dat','rb') as f:

            while True:

                rec=pickle.load(f)

                rec[3]=rec[3]*1.1

                l.append(rec)                

    except EOFError:

        pass

    except Exception as e:

        print(e)

    with open('course.dat','wb') as f:

        for rec in l :

            pickle.dump(rec,f)

    print("Records have been updated ")

    display()

    

menu()


No comments:

Post a Comment