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
55 changes: 55 additions & 0 deletions book.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@


class Book():
def __init__(self,title,author,publication_year,is_borrowed,borrowed_by):
self.title = title
self.author = author
self.publication_year = publication_year
self.is_borrowed = is_borrowed
self.borrowed_by = borrowed_by

def show_info(self):
print("Title: ",self.title)
print("Author: ",self.author)
print("Publication_year: ",self.publication_year)
print("Is borrowed: ",self.is_borrowed)
print("Borrowed by: ",self.borrowed_by)

def borrow(self,user):
if self.is_borrowed==True:
print("The book was already borrowed by: ",self.borrowed_by)
else:
print("The book is available.")
self.is_borrowed = True # kitabı ödünç aldık
self.borrowed_by = user # kullanıcıyı atadık
print(f"{self.title} has been borrowed by {user}.")

def return_book(self,):
if self.is_borrowed == False:
print("The book is not borrowed")
else:
user = self.borrowed_by
self.is_borrowed = False
self.borrowed_by = None
print(f"The book returned by: {user}")

def to_dict(self):
return {
"title": self.title,
"author": self.author,
"publication_year": self.publication_year,
"is_borrowed": self.is_borrowed,
"borrowed_by": self.borrowed_by
}


class Novel(Book):
def __init__(self,title,author,publication_year,is_borrowed,borrowed_by,genre):
Book.__init__(self,title,author,publication_year,is_borrowed,borrowed_by)
self.genre = genre

class Magazine(Book):
def __init__(self,title,author,publication_year,is_borrowed,borrowed_by, issue):
Book.__init__(self,title,author,publication_year,is_borrowed,borrowed_by)
self.issue = issue

18 changes: 18 additions & 0 deletions library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"books": [
{
"title": "Ada",
"author": "Ahmet",
"publication_year": 1900,
"is_borrowed": false,
"borrowed_by": null
}
],
"users": [
{
"name": "nesli",
"password": "123",
"borrowed_books": []
}
]
}
95 changes: 95 additions & 0 deletions library.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
from book import Book
from user import User
import json
import os

class Library():
def __init__(self,name):
self.name = name
self.books = []
self.users = []

def add_book(self,book):
for b in self.books:
if b.title == book.title:
print(f"The book '{book.title}' already exixsts!")
return
self.books.append(book)
print(f"The book '{book.title}' added to the library")

def add_user(self,user):
for u in self.users:
if u.name == user.name:
print("User already exists!")
return
self.users.append(user)
print(f"User '{user.name}' added to the library!")

def show_all_books(self):
for book in self.books:
print("-" * 40)
print("Title:",book.title)
print("Author:",book.author)
print("Available:", not book.is_borrowed)
print("Borrowed by:",book.borrowed_by)
print("-" * 40)

def login(self,name,password):
for user in self.users:
if user.name == name and user.password == password:
return user

print("user not found or password incorrect")
return None

def borrow_book(self,user,title):
title = title.strip()
for book in self.books:
if book.title == title:
user.borrow_book(book)
return
print("Book not found")

def return_book(self,user,title):
title = title.strip()
for book in self.books:
if book.title == title:
user.return_book(book)
return
print("Book not found.")

def save(self,file): #save to json
book_list =[book.to_dict() for book in self.books]
user_list =[user.to_dict() for user in self.users]
data ={
"books":book_list,
"users": user_list
}
with open(file, "w", encoding="utf-8") as f:
json.dump(data,f,ensure_ascii=False, indent=4)

def load(self,file): #load from json
if not os.path.exists(file):
return
with open(file, "r", encoding="utf-8") as f:
data = json.load(f)

self.books = []
for book_data in data["books"]:
book = Book(**book_data)
self.books.append(book)

self.users = []
for user_data in data["users"]:
# Önce borrowed_books başlıklarını boş liste yap
borrowed_books_titles = user_data.pop("borrowed_books")
user = User(**user_data, borrowed_books=[]) # boş liste ile yarat
self.users.append(user)

# Sonra borrowed_books listesini gerçek Book objeleri ile doldur
for title in borrowed_books_titles:
book = next((b for b in self.books if b.title == title), None)
if book:
user.borrowed_books.append(book)
book.is_borrowed = True
book.borrowed_by = user.name
100 changes: 100 additions & 0 deletions login_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from book import Book, Novel, Magazine
from user import User
from library import Library

data_file = "library.json"

library = Library("My Library")

library.load(data_file)

library.add_book(Book("Ada", "Ahmet", 1900, False, None))

current_user = None


while True:

print("-" * 60)
print()
print(" 1- LOGIN ")
print(" 2- CREATE ACOUNT ")
print(" 3- EXIT. ")
print()
print("-" * 60)

choice = input("Enter your choice please: ")
if choice =="1":
name = input("Enter a name: ")
password = input("Enter a password: ")
user = library.login(name,password)

if user:
current_user = user
print(f"\nWelcome back, {current_user.name}!")

while True:
print("-" * 60)
print()
print(" 1- LIST ALL BOOKS ")
print(" 2- BORROW A BOOK ")
print(" 3- RETURN A BOOK ")
print(" 4- SHOW MY BORROWED BOOKS")
print(" 5- SAVE AND EXIT")
print()
print("-" * 60)

option = input("Enter your choice please: ")

if option == "1":
library.show_all_books()

elif option == "2":
title = input("Enter book title: ")
library.borrow_book(current_user, title)

elif option == "3":
title = input("Enter book title: ")
library.return_book(current_user, title)

elif option == "4":
current_user.list_borrowed_books()

elif option == "5":
current_user = None
print("Logged out. Saving data...")
library.save(data_file)
break

else:
print("Invalid option.")

else:
print("Login failed.")

elif choice == "2":
name = input("Enter a name: ")
password = input("Enter a password: ")
new_user = User(name,password,[])
library.add_user(new_user)
print("Account created!")

elif choice == "3":
library.save(data_file)
print("Data saved.")
break

else:
print("Invalid choice!")




# def borrow_by_title(user, all_books, title):
# book_to_borrow = next((b for b in all_books if b.title == title), None)
# if book_to_borrow:
# user.borrow_book(book_to_borrow)
# else:
# print(f"Book '{title}' not found!")


56 changes: 56 additions & 0 deletions user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from book import Book
import json



class User():
def __init__(self,name,password,borrowed_books):
self.name = name
self.password = password
self.borrowed_books = borrowed_books

def borrow_book(self,book):
if book.is_borrowed == True:
print("The book was already borrowed.")
else:
book.borrow(self.name)
self.borrowed_books.append(book)

def return_book(self,book):
if book not in self.borrowed_books:
print("You did not borrow this book.")
else:
book.return_book()
self.borrowed_books.remove(book)


def list_borrowed_books(self):
if not self.borrowed_books:
print("You have no borrowed books.")
else:
for book in self.borrowed_books:
print("title:", book.title, "|Author:" ,book.author )


def to_dict(self):
return {
"name": self.name,
"password": self.password,
"borrowed_books": [book.title for book in self.borrowed_books]
}


# # Test için kullanıcı
# test_user = User("Ayşe", "123",[])

# # Örnek kitaplar
# all_books = [
# Book("Ada", "Ahmet", 1900, False, None),
# Book("Deniz", "Mehmet", 1920, False, None)
# ]

# # Ödünç alma testi
# #borrow_by_title(test_user, all_books, "Ada") # Tek satırla ödünç al

# # Kullanıcının ödünç aldığı kitapları listele
# #test_user.list_borrowed_books()