-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode.py
More file actions
94 lines (73 loc) · 2.68 KB
/
code.py
File metadata and controls
94 lines (73 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class Book:
def __init__(self, title, author, available_copies):
self.title = title
self.author = author
self.available_copies = available_copies
class Library:
def __init__(self):
self.books = []
def add_book(self, book):
self.books.append(book)
def display_books(self):
for book in self.books:
print(f"Title: {book.title}, Author: {book.author}, Available Copies: {book.available_copies}")
class User:
def __init__(self, username, password):
self.username = username
self.password = password
def main():
users = [User("user1", "pass1"), User("user2", "pass2")]
logged_in_user = None
while True:
if logged_in_user is None:
print("\nLibrary Management System")
print("1. Register")
print("2. Login")
print("3. Exit")
choice = input("Enter your choice: ")
if choice == '1':
register(users)
elif choice == '2':
logged_in_user = login(users)
elif choice == '3':
print("Goodbye!")
break
else:
print(f"\nWelcome, {logged_in_user.username}!")
print("1. Add Book")
print("2. Display Books")
print("3. Logout")
choice = input("Enter your choice: ")
if choice == '1':
title = input("Enter book title: ")
author = input("Enter author: ")
available_copies = int(input("Enter available copies: "))
book = Book(title, author, available_copies)
library.add_book(book)
print("Book added successfully!")
elif choice == '2':
library.display_books()
elif choice == '3':
logged_in_user = None
if __name__ == "__main__":
library = Library()
main()
def register(users):
while True:
username = input("Enter a new username: ")
password = input("Enter a password: ")
if any(user.username == username for user in users):
print("Username already exists. Please choose another username.")
else:
users.append(User(username, password))
print("Registration successful!")
break
def login(users):
while True:
username = input("Enter your username: ")
password = input("Enter your password: ")
for user in users:
if user.username == username and user.password == password:
print("Login successful!")
return user
print("Invalid username or password. Please try again.")