Skip to content

Instantly share code, notes, and snippets.

@camathieu
Created November 28, 2024 19:58
Show Gist options
  • Save camathieu/037afb0d2291ecd459fc9792b3132753 to your computer and use it in GitHub Desktop.
Save camathieu/037afb0d2291ecd459fc9792b3132753 to your computer and use it in GitHub Desktop.
Library Management System
=========================
Objective:
Create a Python program to manage a small library. Users can add books, remove books, and query the library.
Functionalities:
- Add a Book: Users can add a book by entering its details in the format TITLE,AUTHOR,RELEASE_YEAR.
- Ensure that the TITLE and AUTHOR are not empty
- Ensure that the RELEASE_YEAR is between 0 and the current year.
- Print All Books: Display all books currently in the library.
- Print Books by Author or Year: Allow users to input an author or a year. If the input is a valid integer, treat it as a year. Otherwise, treat it as an author’s name.
- Count Books by Author and Year: Display the number of books grouped by author and release year.
- Remove Books Before a Certain Year: Remove all books with a release year before a specified year.
- Exit: Quit the program.
---
Example Ineraction:
Options:
1. Add a book (TITLE,AUTHOR,RELEASE_YEAR)
2. Print all books
3. Print all books by author or year
4. Count books by author and year
5. Remove all books before a certain year
6. Exit
Enter your choice: 1
Enter book details (TITLE,AUTHOR,RELEASE_YEAR): Dune,Frank Herbert,1965
Book "Dune" added.
Enter your choice: 2
Books:
- 1984 by George Orwell (1949)
- To Kill a Mockingbird by Harper Lee (1960)
- The Great Gatsby by F. Scott Fitzgerald (1925)
- Brave New World by Aldous Huxley (1932)
- Harry Potter and the Philosopher's Stone by J.K. Rowling (1997)
- Dune by Frank Herbert (1965)
Enter your choice: 3
Enter an author name or a year: 1960
Books from the year 1960:
- To Kill a Mockingbird by Harper Lee (1960)
Enter your choice: 4
Book counts by author:
- George Orwell: 1 book(s)
- Harper Lee: 1 book(s)
- F. Scott Fitzgerald: 1 book(s)
- Aldous Huxley: 1 book(s)
- J.K. Rowling: 1 book(s)
- Frank Herbert: 1 book(s)
Book counts by year:
- 1925: 1 book(s)
- 1932: 1 book(s)
- 1949: 1 book(s)
- 1960: 1 book(s)
- 1965: 1 book(s)
- 1997: 1 book(s)
Enter your choice: 5
Remove books before which year? 1950
2 book(s) removed.
Enter your choice: 6
Goodbye!
---
Starting point:
```
def add_book(library):
pass
def print_books(library):
pass
def print_books_by_author_or_year(library):
pass
def count_books_by_author_and_year(library):
pass
def remove_books_before_year(library):
pass
def main():
library = [
{"title": "1984", "author": "George Orwell", "release_year": 1949},
{"title": "To Kill a Mockingbird", "author": "Harper Lee", "release_year": 1960},
{"title": "The Great Gatsby", "author": "F. Scott Fitzgerald", "release_year": 1925},
{"title": "Brave New World", "author": "Aldous Huxley", "release_year": 1932},
{"title": "Harry Potter and the Philosopher's Stone", "author": "J.K. Rowling", "release_year": 1997},
]
while True:
print_menu()
choice = input("Enter your choice: ").strip()
if choice == "1":
add_book(library)
elif choice == "2":
print_books(library)
elif choice == "3":
print_books_by_author_or_year(library)
elif choice == "4":
count_books_by_author_and_year(library)
elif choice == "5":
remove_books_before_year(library)
elif choice == "6":
print("Goodbye!")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment