-
Notifications
You must be signed in to change notification settings - Fork 0
/
7 - Book.rb
67 lines (53 loc) · 1.51 KB
/
7 - Book.rb
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
# Write a Ruby program to create a class called "Book" with attributes for title, author, and ISBN,
# and methods to add and remove books from a collection.
class Book
attr_accessor :title, :author, :isbn
def initialize(title, author, isbn)
@title = title
@author = author
@isbn = isbn
end
end
class Library
def initialize
@books = []
end
def add_book(book)
@books << book
puts "#{book.title} by #{book.author} has been added to the library."
end
def remove_book(identifier)
book_to_remove = @books.find { |book| book.title == identifier || book.isbn == identifier }
if book_to_remove
@books.delete(book_to_remove)
puts "#{book_to_remove.title} has been removed from the library."
else
puts "Book not found."
end
end
def list_books
if @books.empty?
puts "The library is empty."
else
puts "Books in the library:"
@books.each { |book| puts "- #{book.title} by #{book.author} (ISBN: #{book.isbn})" }
end
end
end
library = Library.new
# Creating book instances
book1 = Book.new("i will", "ahmed", "123")
book2 = Book.new("good nothing", "khaled", "321")
book3 = Book.new("maght", "ahmed", "213")
# Adding books to the library
library.add_book(book1)
library.add_book(book2)
library.add_book(book3)
# Listing all books in the library
library.list_books
# Removing a book by title
library.remove_book("i will")
# Removing a book by ISBN
library.remove_book("123")
# Listing remaining books in the library
library.list_books