Untitled

 avatar
unknown
python
4 years ago
3.7 kB
5
Indexable
from random import randint

# Program description

# =-=-=-=-=-= #
# VARIABLES
# =-=-=-=-=-= #
books = []
serial_nums = []
books_issued = []

# =-=-=-=-=-= #
# FUNCTIONS
# =-=-=-=-=-= #
def print_menu():

    print("1. Search for books") # 
    print("2. View record of books issued") #
    print("3. Issue a book") #
    print("4. Return a book") #
    print("5. Add books") #
    print("6. Edit book record")
    print("7. Exit")


def read_choice():
    user_choice = int(input("What do you want to do?: "))
    
    if user_choice == 1:
        print(search_book())

    elif user_choice == 2:
        view_issued_books()

    elif user_choice == 3:
        issue_book()

    elif user_choice == 4:
        return_book()

    elif user_choice == 5:
        add_books()

    elif user_choice == 7:
        return 7
        

def search_book():
    # ask for keyword
    keyword = input("Insert a keyword: ")
    list_of_books = []


    # print results & add books into a list
    for book in books:
        if keyword.lower() in book["book_name"].lower():
            list_of_books.append(book)

    return list_of_books
 

def issue_book():
    # 
    list_of_books = search_book()

    counter = 1
    for book in list_of_books:
        print(f'{counter}. Title: {book["book_name"]} | Author: {book["book_author"]} | Year: {book["year_of_publishing"]}')
        counter += 1

    book = int(input("Which book you want to issue?: ")) - 1 
    books_issued.append(list_of_books[book])



    # 1. Title: Alice in Wonderland | Author: Someone | Year: 2000
    # 2. Title: Alice at home

    # Which book you want to issue:  

    # You are about to issue "Alice in Wonderland", is it correct?: Y/N 

def view_issued_books():
    for book in books_issued:
        print(f'1. Title: {book["book_name"]} | Author: {book["book_author"]} | Year: {book["year_of_publishing"]}')


def return_book():
    # print all books issued
    for book in books_issued:
        print(f'1. Title: {book["book_name"]} | Author: {book["book_author"]} | Year: {book["year_of_publishing"]}')

    # ask which 
    book = int(input("Which book you want to return?: ")) - 1
    books_issued.pop(book)


def add_books():
    serial_num = unique_id()
    book_name = input("Book Name: ")
    book_author = input("Book Author: ")
    publisher_name = input("Publisher Name: ")
    year_of_publishing = input("Year of publishing: ")
    book_edition = input("Book Edition: ")
    brief_of_abstract = input("Brief Abstract: ")
    book_department = input("Department: ")


    book = {
        "serial_num": serial_num,
        "book_name": book_name,
        "book_author": book_author,
        "publisher_name": publisher_name,
        "year_of_publishing": year_of_publishing,
        "book_edition": book_edition,
        "brief_of_abstract": brief_of_abstract,
        "book_department": book_department
    }

    books.append(book)


def unique_id():
    while True:
        serial_num = randint(1000, 9999)

        if serial_num not in serial_nums:
            serial_nums.append(serial_num)
            break
        else:
            serial_num = randint(1000, 9999)
    return serial_num


# =-=-=-=-=-=-=-= #
# PROGRAM LOGIC
# =-=-=-=-=-=-=-= #
user_choice = 0
while user_choice != 7:
    # Print menu to the user
    print_menu()

    # Read user choice
    user_choice = read_choice()

    # add space
    print("")
    print("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=")
    print("")


###
print(f'There are {len(books)} books.')
print(books)

for book in books:
    print(book["serial_num"])

print(serial_nums)

print("All issued books:")
print(books_issued)


Editor is loading...