-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
98 lines (82 loc) · 2.23 KB
/
app.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require './src/classroom'
require './src/book'
require './src/nameable'
require './src/person'
require './src/rental'
require './src/teacher'
require './src/student'
require './src/base_decorator'
require './src/capitalize_decorator'
require './src/trimmer_decorator'
# app.rb
class App
def initialize
@books = []
@people = []
@rentals = []
end
def list_books
puts 'List of Books:'
@books.each do |book|
puts "Title: #{book.title}, Author: #{book.author}"
end
puts
end
def list_people
puts 'List of People:'
@people.each do |person|
puts "ID: #{person.id}, Name: #{person.name}, Age: #{person.age}"
end
puts
end
def create_person(name, age, type, specialization = nil)
if type == 'teacher'
person = Teacher.new(specialization, name, age)
elsif type == 'student'
person = Student.new(name: name, age: age)
else
puts 'Invalid person type.'
return
end
@people << person
puts "Person with ID #{person.id} was created."
end
def create_book(title, author)
book = Book.new(title, author)
@books << book
puts "Book '#{book.title}' by #{book.author} was created."
end
def create_rental
puts 'Enter the rental date (yyyy-mm-dd):'
date = gets.chomp
puts 'Enter the book title for the rental:'
title = gets.chomp
book = @books.find { |b| b.title == title }
if book.nil?
puts "Book with title '#{title}' not found."
return
end
puts 'Enter the person ID for the rental:'
person_id = gets.chomp.to_i
person = @people.find { |p| p.id == person_id }
if person.nil?
puts "Person with ID #{person_id} not found."
return
end
rental = Rental.new(date, book, person)
@rentals << rental
puts "Rental of '#{book.title}' by #{book.author} to #{person.correct_name} on #{rental.date} has been created."
end
def list_rentals_for_person(person_id)
person = @people.find { |p| p.id == person_id }
if person.nil?
puts 'Person not found.'
return
end
puts "Rentals for #{person.name}:"
person.rentals.each do |rental|
puts "Date: #{rental.date}, Book: #{rental.book.title}, Author: #{rental.book.author}"
end
puts
end
end