Project 1 - English Dictionary App & Library Book Management System
1A.
import os
import json
def saveDictionary(tmp_dict):
try:
with open('word.txt','w') as dictionary:
json.dump(tmp_dict,dictionary)
print('Successfully entered')
except Exception as e:
print(f"ERROR:{e}")
def getDictionary():
try:
with open('word.txt','r') as dictionary:
tmp_dict=json.load(dictionary)
print('Successfully Read')
return tmp_dict
except Exception as e:
print(f"ERROR:{e}")
def addnewWord():
print("_______________")
print("New Word Adder:")
print("_______________")
print('Enter a word')
word=input("->")
print(f"Enter the meaning of {word} to add")
meaning=input("->")
dictionary=getDictionary()
dictionary[word]=meaning
saveDictionary(dictionary)
main_menu()
def findmeaning():
print("_______________")
print(" Word Finder:")
print("_______________")
print('Enter a word')
word=input("->")
dictionary=getDictionary()
if word in dictionary:
print(f"Found : [{word} :{dictionary[word]}]")
else:
print("The word doesn't exist!..")
main_menu()
def updateWord():
print("_______________")
print(" Word Updater:")
print("_______________")
print('Enter a word to edit')
word=input("->")
dictionary=getDictionary()
if word in dictionary:
print(f"Enter a new meaning [{word} :{dictionary[word]}]")
new_meaning=input("->")
dictionary[word]=new_meaning
saveDictionary(dictionary)
print(f"Successfully Updated [{word} :{dictionary[word]}]")
else:
print("The word doesn't exist!..")
updateWord()
main_menu()
def main_menu():
if not os.path.exists(os.path.join("word.txt")):
try:
with open('word.txt','w') as dictionary:
json.dump({},dictionary)
print('Successfully Entered')
except Exception as e:
print(f"Error :{e}")
print("____________")
print("ENGLISH DICTIONARY APP")
print("____________")
print("1. Add a new word")
print("2. Find a new meaning")
print("3. update word")
print("4. Exit")
print("____________")
n=int(input("->"))
if n==1:
addnewWord()
elif n==2:
findmeaning()
elif n==3:
updateWord()
elif n==4:
exit()
else:
print('Please Enter the correct digit according to the menu')
main_menu()
1B.
from tkinter import*
import sqlite3
import tkinter.ttk as ttk
import tkinter.messagebox as messagebox
root = Tk()
root.title("LBMS | Library Book Management System")
def init_db():
connection = sqlite3.connect("library.db")
cursor = connection.cursor()
create_table(cursor)
return(connection, cursor)
def term_db(connection):
connection.close()
def create_table(cursor):
sql_command = """CREATE TABLE if not exists books (
title varchar(60) not null,
author varchar(30) not null,
year INTEGER not null,
ISBN INTEGER,
unique (title, author, year, ISBN)
);"""
cursor.execute(sql_command)
def read_table(cursor):
sql_command = "SELECT * FROM books"
cursor.execute(sql_command)
data = cursor.fetchall()
return(data)
def add_to_table(cursor, title, author, year, isbn):
sql_command = f"INSERT INTO books VALUES ('{title}', '{author}', {year}, {isbn});"
try:
cursor.execute(sql_command)
return(0, "success")
except sqlite3.IntegrityError as e:
return(111, e)
except sqlite3.OperationalError as e:
return(112, e)
def remove_from_table(cursor, title, author, year, isbn):
try:
sql_command = f'''DELETE FROM books WHERE title="{title}";'''
cursor.execute(sql_command)
sql_command = f'''DELETE FROM books WHERE author="{author}";'''
cursor.execute(sql_command)
sql_command = f'''DELETE FROM books WHERE year="{year}";'''
cursor.execute(sql_command)
sql_command = f'''DELETE FROM books WHERE isbn="{isbn}";'''
cursor.execute(sql_command)
except:
return(-1)
def search_from_table(cursor, title, author, year, isbn):
try:
cursor.execute("SELECT * FROM books WHERE title=?", (title,))
searched_titles = cursor.fetchall()
cursor.execute("SELECT * FROM books WHERE author=?", (author,))
searched_authors = cursor.fetchall()
cursor.execute("SELECT * FROM books WHERE year=?", (year,))
searched_years = cursor.fetchall()
cursor.execute("SELECT * FROM books WHERE ISBN=?", (isbn,))
searched_isbns = cursor.fetchall()
return(searched_titles + searched_authors + searched_years + searched_isbns)
except:
return(-1)
def save_changes(connection):
connection.commit()
def list_all_books(listArea):
connection, cursor = init_db()
print("listing books")
books = read_table(cursor)
listArea.delete('0','end')
for i in range(len(books)):
listArea.insert(i,f"{books[i][0]}, {books[i][1]}, {books[i][2]}, {books[i][3]}")
term_db(connection)
def search_book(listArea, title, author, year, isbn):
connection, cursor = init_db()
print("search book")
title = title.get()
author = author.get()
year = year.get()
isbn = isbn.get()
res = search_from_table(cursor, title, author, year, isbn)
if res==-1:
messagebox.showerror("Error!", "something went wrong!")
if res!=-1:
listArea.delete('0','end')
for i in range(len(res)):
listArea.insert(i,res[i])
term_db(connection)
def add_book(title, author, year, isbn):
connection, cursor = init_db()
print("adding book")
title = title.get()
author = author.get()
year = year.get()
isbn = isbn.get()
code, error = add_to_table(cursor, title, author, year, isbn)
if code==111:
messagebox.showerror("Error!", f"Book Already Exists!\n\nerror code: {code}\nerror:{error}")
if code==112:
messagebox.showerror("Error!", f"Please, fill in the input fields appropriately.\n\ntitle: string, author: string, year: integer, year: integer.\n\nerror code: {code}\nerror:{error}")
save_changes(connection)
term_db(connection)
def issue_book(title, author, year, isbn):
connection, cursor = init_db()
print("issue book")
messagebox.showwarning("Undefined", "this function is not yet defined!")
term_db(connection)
def delete_book(title, author, year, isbn):
connection, cursor = init_db()
print("delte book")
title = title.get()
author = author.get()
year = year.get()
isbn = isbn.get()
code = remove_from_table(cursor, title, author, year, isbn)
if code==-1:
messagebox.showerror("Error!", "something went wrong!")
else:
messagebox.showinfo("Deleted!", f"Deleted the book: {title}, {author}, {year}, {isbn}")
save_changes(connection)
term_db(connection)
title = StringVar()
author = StringVar()
year = StringVar()
isbn = StringVar()
Top=Frame(root,width=900,height=80,bd=8,relief='raise')
Top.pack(side=TOP)
Left=Frame(root,width=300,height=500,bd=8,relief='raise')
Left.pack(side=LEFT)
Right=Frame(root,width=600,height=500,bd=8,relief='raise')
Right.pack(side=RIGHT)
Forms = Frame(Left,width=300,height=450)
Forms.pack(side=TOP)
Buttons=Frame(Left,width=300,height=100,bd=8,relief='raise')
Buttons.pack(side=BOTTOM)
txt_title=Label(Top,width=500,text='Library Management App ')
txt_title.pack()
title_label = Label(Forms, text="title:")
title_label.grid(row=0,stick='e')
author_label = Label(Forms, text="author:")
author_label.grid(row=1, stick='e')
year_label = Label(Forms, text="year:")
year_label.grid(row=2, stick='e')
isbn_label = Label(Forms, text="isbn:")
isbn_label.grid(row=3,stick='e')
title_entry = Entry(Forms,textvariable=title,width=30)
title_entry.grid(row=0, column=1)
author_entry = Entry(Forms,textvariable=author,width=30)
author_entry.grid(row=1, column=1)
year_entry = Entry(Forms, textvariable=year,width=30)
year_entry.grid(row=2, column=1)
isbn_entry = Entry(Forms, textvariable=isbn,width=30)
isbn_entry.grid(row=3, column=1)
scrollbar = Scrollbar(Right, orient="vertical")
listArea = Listbox(Buttons, bg='white',width=100, yscrollcommand=scrollbar.set)
listArea.pack(side=RIGHT)
list_all_btn = Button(Buttons,text="List All", command=lambda:list_all_books(listArea))
list_all_btn.pack(side=LEFT)
search_btn = Button(Buttons, text="Search", command=lambda:search_book(listArea, title, author, year, isbn))
search_btn.pack(side=LEFT)
add_btn = Button(Buttons, text="Add", command=lambda:add_book(title, author, year, isbn))
add_btn.pack(side=LEFT)
issue_btn = Button(Buttons, text="Issue", command=lambda:issue_book(title, author, year, isbn))
issue_btn.pack(side=LEFT)
del_btn = Button(Buttons, text="delete", command=lambda:delete_book(title, author, year, isbn))
del_btn.pack(side=LEFT)
exit_btn = Button(Buttons, text="Exit", command=root.destroy)
exit_btn.pack(side=LEFT)
scrollbar.config(command=listArea.yview)
scrollbar.pack(side=RIGHT,fill=Y)
if __name__ == '__main__':
root.mainloop()