Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
3.3 kB
0
Indexable
Never
import pickle
import os
def read():
    record={}
    file=open('company.dat','rb')
    file1='company.dat'
    if os.stat(file1).st_size==0:
        return record
    else:
        record=pickle.load(file)
    file.close()
    return record 

def write(record):
    
    file=open('company.dat','wb')
    pickle.dump(record,file)
    file.close()
    
def Add():
    try:
        file=open('company.dat','ab')
        file.close()
        record=read()
        do='y'
        while do=='y':
            dep=input('Enter Department name : ')  
            value=[]
            do2='y'
            while do2=='y':
                info=[]
                name=input('Enter Employee name : ')
                salary=int(input('Enter Salary : '))
                info.append(name)
                info.append(salary)
                value.append(info)
                do2=input('Do you want add new employee (y,n)?')
                
            if dep not in record:    
                record[dep]=value  
            else:
                print('the value in record')         
            do=input('Do you want add new department (y,n)?')
        write(record)
    except ValueError:
        print('Wrong enter')
                   
                
                
def Add_E():
    record=read()
    info=[]
    name=input('Enter Employee name :')
    salary=int(input('Enter Salary : '))
    info.append(name)
    info.append(salary)
    dep=input('Enter Department name : ')               
    if dep  in record:
        list1=record[dep]
        list1.append(info)
        record[dep]=list1
        
    else:
        print('Sorry The department doesn’t exist.')
                           
    write(record)       
        
def View():
    print('Department\t\tEmployee\t\tSalary')   
    record=read()
    for dep in record:
        list1=record[dep]
        for n in list1:
            print(dep,'\t\t',n[0],'\t\t',n[1])
        

    
def Delete_E():
    dep=input('enter the name of the Department :')
    name=input('enter the name of the employee to delete :')
    record=read()
    for dep in record:
        list1=record[dep]
        for n in list1:
            if n[0]==name:
                list1.remove(n)
                record[dep]=list1
    write(record)              
          
def Delete_d():
    record=read()
    dep=input('enter the name of the department to delete: ')
    if dep in record:
        record.pop(dep,'not found')
        #del record[dep]
    write(record)     
     
def main():
    flag=True
    while flag:
        print('**********************Menu*********************')
        print('1- Add Department\n2- Add Employee\n3- View\n4- Delete Employee\n5-Delete Department\n6- Exit')
        print('************************************************')
        choice=int(input('Please enter your choice:'))
        if choice==1:
            Add()
        elif choice==2:
            Add_E()  
        elif choice==3:
            View()
        elif choice==4:
            Delete_E()
        elif choice==5:
            Delete_d()
        elif choice==6:
            print('Good Bye')
            flag=False
        else:
            print('Wrong valu')
main()
Leave a Comment