Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions book_transactions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# book_transactions.py
import file_transactions, drawing
import os

def add_book(books, books_file):
"""
Adds a new book to the list of books.
"""
barcode = int(input("Enter the book's barcode: "))
title = input("Enter the book's title: ")
author = input("Enter the book's author: ")
publisher = input("Enter the book's publisher: ")
language = input("Enter the book's language: ")
price = float(input("Enter the book's price: "))
book = dict()
book["Barkod"] = barcode
book["Kitap_Adi"]= title
book["Yazar"]= author
book["Yayinevi"]= publisher
book["Dil"]= language
book["Fiyat"]= price
books.append(book)
print("Book added successfully.")
file_transactions.save_file(books_file, books)
return books

def update_book(books, books_file):
"""
Updates the information of a book.
"""
book_barcode = int(input("Enter the book's barcode: "))
for book in books:
if book["Barkod"] == book_barcode:
title = input("Enter the book's new title: ")
author = input("Enter the book's new author: ")
publisher = input("Enter the book's new publisher: ")
language = input("Enter the book's new language: ")
price = float(input("Enter the book's new price: "))
book["Kitap_Adi"] = title
book["Yazar"] = author
book["Yayinevi"] = publisher
book["Dil"] = language
book["Fiyat"] = price
print("Book updated successfully.")
file_transactions.save_file(books_file, books)
return books
print("Book not found.")
return books

def search_books(books):
"""
Searches for books based on a given criteria.
"""
search_dicttionary = {}
while True:
os.system("cls" if os.name == "nt" else "clear")
print("Search Criteria:")
print("1. Barcode")
print("2. Title")
print("3. Author")
print("4. Publisher")
print("5. Language")
print("6. Price")
print("Q. Exit")
print("Now, choose your search criteria: ")
for i in search_dicttionary:
print(i," : ", search_dicttionary[i])
Choose = input("Enter your choice: ")
if Choose in ["Q", "q", "exit", "quit"]:
break
elif Choose == "1":
search_dicttionary["Barkod"] = int(input("Enter the book's barcode: "))
elif Choose == "2":
search_dicttionary["Kitap_Adi"] = input("Enter the book's title: ")
elif Choose == "3":
search_dicttionary["Yazar"] = input("Enter the book's author: ")
elif Choose == "4":
search_dicttionary["Yayinevi"] = input("Enter the book's publisher: ")
elif Choose == "5":
search_dicttionary["Dil"] = input("Enter the book's language: ")
elif Choose == "6":
search_dicttionary["Fiyat"] = float(input("Enter the book's price: "))
else:
print("Invalid choice.")

list_books(books, search_dicttionary)


def delete_book(books, books_file):
"""
Deletes a book from the list of books.
"""
book_barcode = int(input("Enter the book's barcode: "))
for book in books:
if book["Barkod"] == book_barcode:
books.remove(book)
print("Book deleted successfully.")
file_transactions.save_file(books_file, books)
return books
print("Book not found.")
return books

def list_books(books, kwargs=None):
"""
Lists all the books in the library.
"""
if kwargs:
books_list = []
for book in books:
for key, value in kwargs.items():
if book[key] == value:
books_list.append(book)
else:
books_list = books

for i in drawing.create_grid(books_list, ["Barkod", "Kitap_Adi", "Yazar", "Yayinevi", "Dil", "Fiyat"], [17, 80, 50, 70, 20, 10], "Kitaplar"):
print(i)

69 changes: 69 additions & 0 deletions drawing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

def create_grid(glist, field_names, field_widths, grid_name):
"""
Creates a grid of glist.
"""
start_first_row = "╔"
end_first_row = "╗"
start_middle_row = "╠"
end_middle_row = "╣"
start_last_row = "╚"
end_last_row = "╝"
separator_column = "║"
separator_row = "═"
separator_sub_row = "─"
separator_sub_column = "│"
separator_middle_row = "╬"
start_middle_column = "╦"
end_middle_column = "╩"
grid = []
grid_first_row = start_first_row+separator_row*(sum(field_widths)+((len(field_widths)-1)*3))+end_first_row
grid.append(grid_first_row)
grid_middle_row = separator_column+grid_name.center(sum(field_widths)+((len(field_widths)-1)*3)," ")+separator_column
grid.append(grid_middle_row)
grid_middle_row = start_middle_row+separator_row*(sum(field_widths)+((len(field_widths)-1)*3))+end_middle_row
grid.append(grid_middle_row)
field_counter = 0
for field_name in field_names:
if field_counter == 0:
grid_middle_row = separator_column+field_name.upper().center(field_widths[field_names.index(field_name)]," ")
if field_counter != len(field_widths)-1:
grid_middle_row += " "+separator_column+" "
else:
grid_middle_row += " "+separator_sub_column+" "
elif field_counter == len(field_widths)-1:
grid_middle_row += field_name.upper().center(field_widths[field_counter]," ")+separator_column
else:
grid_middle_row += field_name.upper().center(field_widths[field_names.index(field_name)]," ")+" "+separator_sub_column+" "
field_counter += 1
grid.append(grid_middle_row)
grid_middle_row = separator_column+separator_sub_row * (sum(field_widths)+((len(field_widths)-1)*3))+separator_column
grid.append(grid_middle_row)
for row in glist:
field_counter = 0
for field_name in field_names:
if field_counter == 0:
grid_middle_row = separator_column + str(row[field_name]).center(field_widths[field_counter]," ")
if field_counter == len(field_widths)-1:
grid_middle_row += " "+separator_column+" "
else:
grid_middle_row += " "+separator_sub_column+" "
elif field_counter == len(field_widths)-1:
if row.get(field_name) == None:
grid_middle_row += " ".center(field_widths[field_counter]," ")
else:
grid_middle_row += str(row[field_name]).center(field_widths[field_counter]," ")+separator_column
else:
if row.get(field_name) == None:
grid_middle_row +=" ".center(field_widths[field_names.index(field_name)]," ")+" "+separator_sub_column+" "
else:
grid_middle_row += str(row[field_name]).center(field_widths[field_names.index(field_name)]," ")+" "+separator_sub_column+" "
field_counter += 1
grid.append(grid_middle_row)
if glist.index(row) != len(glist)-1:
grid_middle_row = separator_column+separator_sub_row*(sum(field_widths)+((len(field_widths)-1)*3))+separator_column
grid.append(grid_middle_row)
grid_last_row = start_last_row+separator_row*(sum(field_widths)+((len(field_widths)-1)*3))+end_last_row
grid.append(grid_last_row)
return grid

28 changes: 28 additions & 0 deletions file_transactions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# File: file_transactions.py
import json
import os

def save_file(file_path, data):
try:
with open(file_path, 'w', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False, indent=4)
# print(f"Data was successfully written to file: {file_path}")
except Exception as e:
print(f"Error: Data could not be written to file. Details: {e}")

def load_file(file_path):
"""
Reads a list from a file in JSON format.
"""
if not os.path.exists(file_path):
save_file(file_path, [])
return []
try:
with open(file_path, 'r', encoding='utf-8') as file:
return json.load(file)
except json.JSONDecodeError:
print("Error: JSON format is invalid.")
return []
except Exception as e:
print(f"Error: File could not be read. Details: {e}")
return []
94 changes: 94 additions & 0 deletions hackerring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# # Week 4 Hackering Questions
# # Q1
# # https://www.hackerrank.com/challenges/diagonal-difference/problem

# #!/bin/python3

# import math
# import os
# import random
# import re
# import sys

# #
# # Complete the 'diagonalDifference' function below.
# #
# # The function is expected to return an INTEGER.
# # The function accepts 2D_INTEGER_ARRAY arr as parameter.
# #

# def diagonalDifference(arr):
# # Write your code here
# diagonal1 = 0
# diagonal2 = 0
# for i in range(len(arr)):
# diagonal1 += arr[i][i]
# diagonal2 += arr[i][len(arr) - 1 - i]
# return abs(diagonal1 - diagonal2)

# if __name__ == '__main__':
# fptr = open(os.environ['OUTPUT_PATH'], 'w')

# n = int(input().strip())

# arr = []

# for _ in range(n):
# arr.append(list(map(int, input().rstrip().split())))

# result = diagonalDifference(arr)

# fptr.write(str(result) + '\n')

# fptr.close()

"""--------------------------------------------------------"""
# # Q2
# # https://www.hackerrank.com/challenges/array-left-rotation/problem

# #!/bin/python3

# import math
# import os
# import random
# import re
# import sys

# #
# # Complete the 'rotateLeft' function below.
# #
# # The function is expected to return an INTEGER_ARRAY.
# # The function accepts following parameters:
# # 1. INTEGER d
# # 2. INTEGER_ARRAY arr
# #

# def rotateLeft(d, arr):
# # Write your code here
# new_arr = []
# for i in range(len(arr)):
# new_arr.append(arr[(i+d) % len(arr)])
# return new_arr
# if __name__ == '__main__':
# fptr = open(os.environ['OUTPUT_PATH'], 'w')

# first_multiple_input = input().rstrip().split()

# n = int(first_multiple_input[0])

# d = int(first_multiple_input[1])

# arr = list(map(int, input().rstrip().split()))

# result = rotateLeft(d, arr)

# fptr.write(' '.join(map(str, result)))
# fptr.write('\n')

# fptr.close()

"""--------------------------------------------------------"""
# # Q3
# # https://www.hackerrank.com/challenges/counter-game/problem


Loading