Demo entry 6341680

Library MAnager

   

Submitted by Shubham on Jan 01, 2017 at 22:10
Language: Python. Code size: 6.4 kB.

import pickle 
class Books:
    def __init__(self,Id,name,tot):
        self.id=Id
        self.name=name
        self.total=tot
        self.left=tot
         
       
def issue(book,member):
    if book.left==0:
        print 'Sorry. The demanded book is currently out of stock.'
        return
    if member.book<>None:
        print 'Member has already issued a book.'
        return
    if book.left>0:
        book.left=book.left-1
        member.book=book.id


        #To Update books.txt
        File=open('books.txt','rb')
        bklist=pickle.load(File)
        File.close()
        indexl=0
        for i in bklist:
            if i.id==book.id:
                bklist[indexl]=book
            indexl=indexl+1
        File=open('books.txt','wb')
        pickle.dump(bklist,File)
        File.close()
            

        #To Update members.txt
        File=open('members.txt','rb')
        mbrlist=pickle.load(File)
        File.close()
        indexl=0
        for i in mbrlist:
            print member.book
            if member.num==i.num:
                mbrlist[indexl]=member
            indexl=indexl+1
        File=open('members.txt','wb')
        pickle.dump(mbrlist,File)
        File.close()
        print 'Book Successfully Issued.'
        
            
            
        return


class Member:
    def __init__(self,num,name):
        self.num=num
        self.name=name
        self.book=None





def add_book(Id,name,tot):   
    newbook=Books(Id,name,tot)
    File=open('books.txt','rb')
    booklist=pickle.load(File)
    File.close()
    booklist=booklist+[newbook]
    File=open('books.txt','wb')
    pickle.dump(booklist,File)
    File.close()
def add_member(num,name):
    newmem=Member(num,name)
    File=open('members.txt','rb')
    memlist=pickle.load(File)
    File.close()
    memlist=memlist+[newmem]
    File=open('members.txt','wb')
    pickle.dump(memlist,File)
    File.close()


Ans='y'
while Ans in ['y','Y','yes','YES','Yes']:
    print '_________________________'
    print 'Choose desired operation:\n 1. Issue a Book\n 2. Return a Book\n 3. Add new book to Library \n 4. Create new Membership'
    choice=raw_input('Enter Here: ')
    while choice not in ['1','2','3','4']:
        print 'Please Enter a Valid Input'
        choice=raw_input('Enter Here: ')
    choice=int(choice)

    
    if choice==3:
        n=raw_input('Enter Book Title: ')
        bid=raw_input('Enter Book ID: ')
        total=input('Enter Number of copies: ')
        add_book(bid,n,total)

        
    if choice==4:
        name=raw_input("Enter Member Name: ")
        num=raw_input('Enter Membership Number: ')
        add_member(num,name)


        
    if choice==1:
        print 'Do you wish to view a complete list of books?'
        a=raw_input('(y/n): ')
        while a not in ['y','n','Y','N']:
             a=raw_input('(y/n): ')
        if a in ['y','Y']:            
            File=open('books.txt','rb')
            booklist=pickle.load(File)
            File.close()
            print 'ID','\t','Title'
            for i in booklist:
                print i.id,'\t',i.name
            print '_________________________'

        print 'Do you wish to view a complete list of members?'
        a=raw_input('(y/n): ')
        while a not in ['y','n','Y','N']:
            a=raw_input('(y/n): ')
        if a in ['y','Y']:            
            File=open('members.txt','rb')
            booklist=pickle.load(File)
            File.close()
            print 'Num','\t','Name'
            for i in booklist:
                print i.num,'\t',i.name
            print '_________________________'
        
             
        
        p=0
        while p==0:
            imem=raw_input('Enter Membership Number of Candidate: ')
            File=open('members.txt','rb')
            mlist=pickle.load(File)
            File.close()
            for i in mlist:
                if i.num==imem:
                    p=1
                    M=i
            if p<>1:
                print 'Member does not exist. Enter valid member.'
        
        ibook=raw_input('Enter id of book to be issued: ')
        j=None
        File=open('books.txt','rb')
        booklist=pickle.load(File)
        File.close()
        indx=0 #To Track Book List item index
        for i in booklist:
            if i.id==ibook:    #Matching Required Book ID to Books in Book List
                issue(i,M)

    if choice==2:
        p=0
        while p==0:
            imem=raw_input('Enter Membership Number of Candidate: ')
            File=open('members.txt','rb')
            mlist=pickle.load(File)
            File.close()
            index=0
            for i in mlist:
                if i.num==imem:
                    p=1
                    M=i
                    memind=index
                index+=1
            if p<>1:
                print 'Member does not exist. Enter valid member.'
                

        bukid=M.book
        File=open('books.txt','rb')
        booklist=pickle.load(File)
        File.close()
        indx=0
        

        for i in booklist:
            print 'Checkpoint'
            print bukid,'is bukid'
            print i.id
            if i.id==bukid:
                
                M.book=None
                i.left+=1
                File=open('books.txt','rb')
                boklist=pickle.load(File)
                File.close()
                boklist[indx]=i
                File=open('books.txt','wb')
                pickle.dump(boklist,File)
                File.close()

                File=open('members.txt','rb')
                boklist=pickle.load(File)
                File.close()
                boklist[memind]=M
                File=open('members.txt','wb')
                pickle.dump(boklist,File)
                File.close()
                print 'Book Returned Successfully'
                
   
            indx+=1

    Ans=raw_input('Continue? (y/n): ')
                           
        
        
        
        
                
                    
                
                    
        
        

    


    
    


    

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).