├── Library ├── menu.py ├── __pycache__ │ └── book.cpython-310.pyc ├── book.py └── library.py └── Notebook ├── menu.py └── notebook.py /Library/menu.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Library/__pycache__/book.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alireza-hashemii/PythonOOP/HEAD/Library/__pycache__/book.cpython-310.pyc -------------------------------------------------------------------------------- /Library/book.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | 3 | id = 0 4 | 5 | class Book: 6 | def __init__(self,name: str,genre: str): 7 | self.name = name 8 | self.date = datetime.datetime.now() 9 | self.genre = genre 10 | global id 11 | id += 1 12 | self.id = id 13 | 14 | def find(self,name:str): 15 | return name in self.name 16 | 17 | def __str__(self) -> str: 18 | return f"Name: {self.name}\nDate: {self.date}\nGenre: {self.genre}" 19 | 20 | -------------------------------------------------------------------------------- /Library/library.py: -------------------------------------------------------------------------------- 1 | from book import Book 2 | 3 | class Library: 4 | def __init__(self): 5 | self.books = [] 6 | 7 | def add_book(self,name:str,genre:str): 8 | self.books.append(Book(name,genre)) 9 | print("It has added") 10 | 11 | def modify_name(self,id,name): 12 | for book in self.books: 13 | if book.id == id: 14 | book.name = name 15 | print("Name has changed") 16 | 17 | def modify_genre(self,id,genre): 18 | for book in self.books: 19 | if book.id == id: 20 | book.genre = genre 21 | print("Genre has changed") 22 | 23 | def search(self,pattern): 24 | return [book for book in self.books if book.find(pattern)] 25 | 26 | -------------------------------------------------------------------------------- /Notebook/menu.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from notebook import Notebook 3 | 4 | 5 | class Menu: 6 | """Display a menu and respond to choices when run.""" 7 | def __init__(self): 8 | self.notebook = Notebook() 9 | self.choices = { 10 | "1": self.show_notes, 11 | "2": self.search_notes, 12 | "3": self.add_note, 13 | "4": self.modify_note, 14 | "5": self.quit, 15 | } 16 | 17 | def display_menu(self): 18 | print( 19 | """ 20 | Notebook Menu 21 | 1. Show all Notes 22 | 2. Search Notes 23 | 3. Add Note 24 | 4. Modify Note 25 | 5. Quit 26 | """ 27 | ) 28 | 29 | def run(self): 30 | """Display the menu and respond to choices.""" 31 | while True: 32 | self.display_menu() 33 | choice = input("Enter an option: ") 34 | action = self.choices.get(choice) 35 | if action: 36 | action() 37 | else: 38 | print("{0} is not a valid choice".format(choice)) 39 | 40 | def show_notes(self, notes=None): 41 | if not notes: 42 | notes = self.notebook.notes 43 | for note in notes: 44 | print("{0}: {1}\n{2}".format(note.id, note.tags, note.memo)) 45 | 46 | def search_notes(self): 47 | filter = input("Search for: ") 48 | notes = self.notebook.search(filter) 49 | self.show_notes(notes) 50 | 51 | def add_note(self): 52 | memo = input("Enter a memo: ") 53 | self.notebook.new_note(memo) 54 | print("Your note has been added.") 55 | 56 | def modify_note(self): 57 | id = input("Enter a note id: ") 58 | memo = input("Enter a memo: ") 59 | tags = input("Enter tags: ") 60 | if memo: 61 | self.notebook.modify_memo(id, memo) 62 | if tags: 63 | self.notebook.modify_tags(id, tags) 64 | 65 | def quit(self): 66 | print("Thank you for using your notebook today.") 67 | sys.exit(0) 68 | 69 | 70 | 71 | if __name__ == "__main__": 72 | Menu().run() -------------------------------------------------------------------------------- /Notebook/notebook.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | 3 | # Store the next available id for all new notes 4 | last_id = 0 5 | 6 | class Note: 7 | """Represent a note in the notebook. Match against a 8 | string in searches and store tags for each note.""" 9 | 10 | def __init__(self, memo, tags=""): 11 | """initialize a note with memo and optional 12 | space-separated tags. Automatically set the note's 13 | creation date and a unique id.""" 14 | 15 | self.memo = memo 16 | self.tags = tags 17 | self.creation_date = datetime.date.today() 18 | global last_id 19 | last_id += 1 20 | self.id = last_id 21 | 22 | def match(self, filter): 23 | """Determine if this note matches the filter 24 | text. Return True if it matches, False otherwise. 25 | Search is case sensitive and matches both text and 26 | tags.""" 27 | 28 | return filter in self.memo or filter in self.tags 29 | 30 | def __str__ (self): 31 | return f"MEMO: {self.memo}\nID: {self.id}" 32 | 33 | 34 | class Notebook: 35 | """Represent a collection of notes that can be tagged, 36 | modified, and searched.""" 37 | 38 | def __init__(self): 39 | """Initialize a notebook with an empty list.""" 40 | 41 | self.notes = [] 42 | 43 | def new_note(self, memo, tags=""): 44 | """Create a new note and add it to the list.""" 45 | 46 | self.notes.append(Note(memo, tags)) 47 | 48 | def modify_memo(self, note_id, memo): 49 | """Find the note with the given id and change its 50 | memo to the given value.""" 51 | 52 | for note in self.notes: 53 | if note.id == note_id: 54 | note.memo = memo 55 | break 56 | 57 | def modify_tags(self, note_id, tags): 58 | """Find the note with the given id and change its 59 | tags to the given value.""" 60 | 61 | for note in self.notes: 62 | if note.id == note_id: 63 | note.tags = tags 64 | break 65 | 66 | def search(self, filter): 67 | """Find all notes that match the given filter 68 | string.""" 69 | 70 | return [note for note in self.notes if note.match(filter)] 71 | 72 | --------------------------------------------------------------------------------