Project 1 - English Dictionary App & Library Book Management System
{
"cells": [
{
"cell_type": "markdown",
"id": "5f8d9bbf",
"metadata": {},
"source": [
"#1"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "fa3b25f7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Welcome to the Dictionary App\n",
"Please select your choice\n",
"1. Add a word\n",
"2. Find the meaning\n",
"3. Update the word\n",
"4. Exit\n",
"Enter the Choice:1\n",
"Enter a word : hello\n",
"Enter its meaning : used as a greeting or to being a phone coversation\n",
"Enter the Choice:1\n",
"Enter a word : Ant\n",
"Enter its meaning : it is an insect\n",
"Enter the Choice:4\n",
"Thank you for choosing Dictionary app\n"
]
}
],
"source": [
"import json\n",
"\n",
"print('Welcome to the Dictionary App')\n",
"print('Please select your choice')\n",
"print('1. Add a word')\n",
"print('2. Find the meaning')\n",
"print('3. Update the word')\n",
"print('4. Exit')\n",
"\n",
"\n",
"f = open('words.txt','w')\n",
"coll_words = {}\n",
"json.dump(coll_words,f)\n",
"f.close()\n",
"\n",
"while True:\n",
" n = int(input('Enter the Choice:'))\n",
" if n == 1:\n",
" import json\n",
" f = open('words.txt','w')\n",
" word = input('Enter a word : ')\n",
" meaning = input('Enter its meaning : ')\n",
" coll_words[word] = meaning\n",
" json.dump(coll_words,f)\n",
" f.close()\n",
"\n",
" elif n == 2:\n",
" import json\n",
" f = open('words.txt','r+')\n",
" dic = json.load(f)\n",
" find = input('Enter the word to find: ')\n",
" if find in coll_words:\n",
" print('The meaning is',coll_words.get(find))\n",
" else:\n",
" print('Sorry the word is not found')\n",
" json.dump(coll_words,f)\n",
" f.close()\n",
" elif n == 3:\n",
" import json\n",
" f = open('words.txt','w')\n",
" word1 = input('Enter the word:')\n",
" update_meaning = input('Enter the updated meaning: ')\n",
" coll_words[word1] = update_meaning\n",
" json.dump(coll_words,f)\n",
" f.close()\n",
" elif n == 4:\n",
" print('Thank you for choosing Dictionary app')\n",
" break\n",
" else:\n",
" print('Please enter the mentioned choice')\n",
" print('1. Add a word')\n",
" print('2. Find the meaning')\n",
" print('3. Update the word')\n",
" print('4. Exit')"
]
},
{
"cell_type": "code",
"execution_count": 79,
"id": "a0270ee8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"GUI loop INITIATING\n",
"Database connection established successfully\n",
"adding book\n",
"Database connection de-established successfully\n",
"Database connection established successfully\n",
"adding book\n",
"Database connection de-established successfully\n",
"Database connection established successfully\n",
"adding book\n",
"Database connection de-established successfully\n",
"Database connection established successfully\n",
"listing books\n",
"Database connection de-established successfully\n",
"GUI loop TERMINATED\n"
]
}
],
"source": [
"import sqlite3\n",
"\n",
"def init_db():\n",
" '''\n",
" call me, i'll initiate and give you a connection and a cursor to my database\n",
" '''\n",
" connection = sqlite3.connect(\"LBMS.db\")\n",
" cursor = connection.cursor()\n",
" print(\"Database connection established successfully\")\n",
" create_table(cursor)\n",
" return(connection, cursor)\n",
"\n",
"\n",
"def term_db(connection):\n",
" '''\n",
" give me a database connection i'll terminate it\n",
" '''\n",
" connection.close()\n",
" print(\"Database connection de-established successfully\")\n",
"\n",
"\n",
"def create_table(cursor):\n",
" '''\n",
" give me a cursor to database i'll create book table in it\n",
" '''\n",
" sql_command = \"\"\"CREATE TABLE if not exists books (\n",
" title varchar(60) not null,\n",
" author varchar(30) not null,\n",
" year INTEGER not null,\n",
" ISBN INTEGER,\n",
" unique (title, author, year, ISBN)\n",
" );\"\"\"\n",
" cursor.execute(sql_command) \n",
"\n",
"\n",
"def read_table(cursor):\n",
" '''\n",
" give me a cursor to database, i'll read it all and send you\n",
" '''\n",
" sql_command = \"SELECT * FROM books\"\n",
" cursor.execute(sql_command) \n",
" data = cursor.fetchall()\n",
" return(data)\n",
"\n",
"\n",
"def add_to_table(cursor, title, author, year, isbn):\n",
" '''\n",
" give me a cursor to database, and a book, i'll add it to the books table\n",
" '''\n",
" sql_command = f\"INSERT INTO books VALUES ('{title}', '{author}', {year}, {isbn});\"\n",
" try:\n",
" cursor.execute(sql_command)\n",
" return(0, \"success\")\n",
" except sqlite3.IntegrityError as e:\n",
" return(111, e)\n",
" except sqlite3.OperationalError as e:\n",
" return(112, e)\n",
"\n",
"\n",
"def remove_from_table(cursor, title, author, year, isbn):\n",
" '''\n",
" give me a cursor to database, and a book, i'll remove it.\n",
" '''\n",
" try:\n",
" sql_command = f'''DELETE FROM books WHERE title=\"{title}\";'''\n",
" cursor.execute(sql_command)\n",
" sql_command = f'''DELETE FROM books WHERE author=\"{author}\";'''\n",
" cursor.execute(sql_command)\n",
" sql_command = f'''DELETE FROM books WHERE year=\"{year}\";'''\n",
" cursor.execute(sql_command)\n",
" sql_command = f'''DELETE FROM books WHERE isbn=\"{isbn}\";'''\n",
" cursor.execute(sql_command)\n",
" except:\n",
" return(-1)\n",
"\n",
"\n",
"def search_from_table(cursor, title, author, year, isbn):\n",
" '''\n",
" give me a cursor to database, and a book, i'll search it.\n",
" '''\n",
" try:\n",
" cursor.execute(\"SELECT * FROM books WHERE title=?\", (title,))\n",
" searched_titles = cursor.fetchall()\n",
" cursor.execute(\"SELECT * FROM books WHERE author=?\", (author,))\n",
" searched_authors = cursor.fetchall()\n",
" cursor.execute(\"SELECT * FROM books WHERE year=?\", (year,))\n",
" searched_years = cursor.fetchall()\n",
" cursor.execute(\"SELECT * FROM books WHERE ISBN=?\", (isbn,))\n",
" searched_isbns = cursor.fetchall()\n",
" return(searched_titles + searched_authors + searched_years + searched_isbns)\n",
" except:\n",
" return(-1)\n",
"\n",
"\n",
"def save_changes(connection):\n",
" '''\n",
" give me a connection to database i'll save the changes\n",
" '''\n",
" connection.commit()\n",
" \n",
"# database_gui_interface\n",
"\n",
"def list_all_books(listArea):\n",
" connection, cursor = init_db()\n",
" print(\"listing books\")\n",
" books = read_table(cursor)\n",
" listArea.delete('0','end')\n",
" for i in range(len(books)):\n",
" listArea.insert(i,f\"{books[i][0]}, {books[i][1]}, {books[i][2]}, {books[i][3]}\")\n",
" term_db(connection)\n",
"\n",
"\n",
"def search_book(listArea, title, author, year, isbn):\n",
" connection, cursor = init_db()\n",
" print(\"search book\")\n",
" title = title.get()\n",
" author = author.get()\n",
" year = year.get()\n",
" isbn = isbn.get()\n",
" res = search_from_table(cursor, title, author, year, isbn)\n",
" if res==-1:\n",
" messagebox.showerror(\"Error!\", \"something went wrong!\")\n",
" if res!=-1:\n",
" listArea.delete('0','end')\n",
" for i in range(len(res)):\n",
" listArea.insert(i,res[i]) \n",
" term_db(connection)\n",
"\n",
"\n",
"def add_book(title, author, year, isbn):\n",
" connection, cursor = init_db()\n",
" print(\"adding book\")\n",
" title = title.get()\n",
" author = author.get()\n",
" year = year.get()\n",
" isbn = isbn.get()\n",
" code, error = add_to_table(cursor, title, author, year, isbn)\n",
" if code==111:\n",
" messagebox.showerror(\"Error!\", f\"Book Already Exists!\\n\\nerror code: {code}\\nerror:{error}\")\n",
" if code==112:\n",
" 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}\")\n",
" save_changes(connection)\n",
" term_db(connection)\n",
"\n",
"def issue_book(title, author, year, isbn):\n",
" connection, cursor = init_db()\n",
" print(\"issue book\")\n",
" messagebox.showwarning(\"Undefined\", \"this function is not yet defined!\")\n",
" term_db(connection)\n",
"\n",
"def delete_book(title, author, year, isbn):\n",
" connection, cursor = init_db()\n",
" print(\"delte book\")\n",
" title = title.get()\n",
" author = author.get()\n",
" year = year.get()\n",
" isbn = isbn.get()\n",
" code = remove_from_table(cursor, title, author, year, isbn)\n",
" if code==-1:\n",
" messagebox.showerror(\"Error!\", \"something went wrong!\")\n",
" messagebox.showinfo(\"Deleted!\", f\"Deleted the book: {title}, {author}, {year}, {isbn}\") \n",
" save_changes(connection)\n",
" term_db(connection)\n",
" \n",
"# gui\n",
"\n",
"from tkinter import *\n",
"from tkinter import ttk\n",
"from tkinter import messagebox\n",
"\n",
"def gui():\n",
" # 1. root\n",
" root = Tk()\n",
" root.title(\"LBMS | Library Book Management System\")\n",
"\n",
" # 2. widgets\n",
" title = StringVar()\n",
" author = StringVar()\n",
" year = StringVar()\n",
" isbn = StringVar()\n",
"\n",
" title_label = Label(root, text=\"title:\", padx=10)\n",
" author_label = Label(root, text=\"author:\", padx=10)\n",
" year_label = Label(root, text=\"year:\", padx=10)\n",
" isbn_label = Label(root, text=\"isbn:\", padx=10)\n",
"\n",
" title_entry = Entry(root, textvariable=title)\n",
" author_entry = Entry(root, textvariable=author)\n",
" year_entry = Entry(root, textvariable=year)\n",
" isbn_entry = Entry(root, textvariable=isbn)\n",
"\n",
"\n",
" scrollbar = Scrollbar(root, orient=\"vertical\")\n",
" listArea = Listbox(root, bg='grey', width=100, yscrollcommand=scrollbar.set)\n",
"\n",
" list_all_btn = Button(root, text=\"List All\", command=lambda:list_all_books(listArea))\n",
" search_btn = Button(root, text=\"Search\", command=lambda:search_book(listArea, title, author, year, isbn))\n",
" add_btn = Button(root, text=\"Add\", command=lambda:add_book(title, author, year, isbn))\n",
" issue_btn = Button(root, text=\"Issue\", command=lambda:issue_book(title, author, year, isbn))\n",
" del_btn = Button(root, text=\"delete\", command=lambda:delete_book(title, author, year, isbn))\n",
" exit_btn = Button(root, text=\"Exit\", command=root.destroy)\n",
"\n",
" scrollbar.config(command=listArea.yview)\n",
"\n",
" title_label.grid(row=0, column=0)\n",
" author_label.grid(row=0, column=2)\n",
" year_label.grid(row=0, column=4)\n",
" isbn_label.grid(row=0, column=6)\n",
"\n",
"\n",
" title_entry.grid(row=0, column=1)\n",
" author_entry.grid(row=0, column=3)\n",
" year_entry.grid(row=0, column=5)\n",
" isbn_entry.grid(row=0, column=7)\n",
"\n",
" listArea.grid(row=1, column=0, columnspan=8, sticky=\"nsew\")\n",
" scrollbar.grid(row=1, column=9, sticky=\"ns\")\n",
"\n",
" list_all_btn.grid(row=2, column=0)\n",
" search_btn.grid(row=2, column=1)\n",
" add_btn.grid(row=2, column=2)\n",
" issue_btn.grid(row=2, column=3)\n",
" del_btn.grid(row=2, column=4)\n",
" exit_btn.grid(row=2, column=7)\n",
"\n",
" # 3. loop\n",
" print(\"GUI loop INITIATING\")\n",
" root.mainloop()\n",
" print(\"GUI loop TERMINATED\")\n",
" \n",
"# run\n",
"gui()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0c4c9ba9",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}