A library prints a label for each book. Write the class Book, so that printing a book prints its label.
A Book is made from a title, an author and a year. It keeps all three.
The label starts with the title and the year in parentheses. Then come the word by and the author. For example, Dune (1965) by Frank Herbert.
In Swift, print(book) uses the book's description. Below the class, make Book adopt CustomStringConvertible in an extension. Its description returns the label.
The program makes the books in the input. Then it prints each book on its own line.
Example 1:
Input:
("Dune", "Frank Herbert", 1965)
Output:
Dune (1965) by Frank Herbert
Explanation: The title comes first.
The year comes next, in parentheses.
The author comes last, after "by".
Example 2:
Input:
("The Hobbit", "J.R.R. Tolkien", 1937)
("1984", "George Orwell", 1949)
Output:
The Hobbit (1937) by J.R.R. Tolkien
1984 (1949) by George Orwell
Explanation: Each book prints its own label, on its own line.
A title can be a number, like "1984".
Example 3:
Input:
("Confessions", "Augustine", 400)
Output:
Confessions (400) by Augustine
Explanation: The year is printed as it is, with no zeros in front.
Constraints:
- 1 to 100 books.
- A title or an author has 1 to 50 characters.
- They are letters, digits, spaces and punctuation.
- They never start or end with a space.
- The year is a whole number from 1 to 2025.
Free plan: 30 tutor messages. Premium is unlimited.
