Managing a Library of Books with Struct Methods
This challenge focuses on implementing methods within a Go struct to represent a simple library management system. You'll define a Book struct and then implement methods to add, remove, and display book information, demonstrating your understanding of how to encapsulate data and behavior within a struct. This is a fundamental concept in object-oriented programming and crucial for building more complex Go applications.
Problem Description
You are tasked with creating a Book struct to represent books in a library. This struct should have fields for the title, author, and publication year. You need to implement three methods for this struct: AddBook(), RemoveBook(), and DisplayBooks().
AddBook(title string, author string, year int): This method should take the title, author, and publication year as input and add a newBookto a slice ofBookstructs (which will be a field within aLibrarystruct - see Notes).RemoveBook(title string): This method should take the title of a book as input and remove the book with that title from the slice ofBookstructs. If the book is not found, the method should do nothing.DisplayBooks(): This method should iterate through the slice ofBookstructs and print the title, author, and publication year of each book to the console.
The overall goal is to create a functional library management system using structs and methods in Go.
Examples
Example 1:
Input:
Library: nil
AddBook("The Lord of the Rings", "J.R.R. Tolkien", 1954)
AddBook("Pride and Prejudice", "Jane Austen", 1813)
DisplayBooks()
RemoveBook("Pride and Prejudice")
DisplayBooks()
Output:
The Lord of the Rings by J.R.R. Tolkien (1954)
Pride and Prejudice by Jane Austen (1813)
The Lord of the Rings by J.R.R. Tolkien (1954)
Explanation: Initially, the library is empty. Two books are added, displayed, and then "Pride and Prejudice" is removed, resulting in only "The Lord of the Rings" being displayed.
Example 2:
Input:
Library: contains books: ["The Hobbit", "The Silmarillion"]
RemoveBook("The Fellowship of the Ring")
DisplayBooks()
Output:
The Hobbit by J.R.R. Tolkien (Unknown)
The Silmarillion by J.R.R. Tolkien (Unknown)
Explanation: The RemoveBook method is called with a title that doesn't exist in the library. The library remains unchanged, and all existing books are displayed.
Example 3: (Edge Case - Empty Library)
Input:
Library: nil
DisplayBooks()
Output:
(No output)
Explanation: If the library is empty, DisplayBooks() should not produce any output.
Constraints
- The
Bookstruct should have fieldsTitle(string),Author(string), andYear(int). - The
Librarystruct should contain a slice ofBookstructs namedBooks. - The
AddBookmethod should not allow duplicate book titles. If a book with the same title already exists, it should not be added. - The
RemoveBookmethod should handle cases where the book to be removed is not found gracefully (no error, just do nothing). - The
DisplayBooksmethod should print each book's information on a new line in the format: "Title by Author (Year)". - Assume all input strings are valid and the year is a positive integer.
Notes
You will need to define a Library struct that contains a slice of Book structs. The AddBook, RemoveBook, and DisplayBooks methods will be defined on the Library struct. Consider how to handle the case where the library is initially empty. Think about how to efficiently search for books when removing them. The "Unknown" year in the example output is a placeholder; your code should use the actual year value. The Year field in the Book struct should be initialized to a default value (e.g., 0 or -1) if no year is provided.