Last active
December 20, 2018 18:41
-
-
Save shamritskiy3468/a44ac2d1f07bdd1a1921e0ab0eab2e7d to your computer and use it in GitHub Desktop.
Task 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#all classes in one file to fit whole code in one gist file | |
#В одном массиве записан рост некоторых студентов, а в другом (с тем же числом элементов) - | |
#их фамилии в том же порядке, в котором указан рост. Известно, что все студенты разного роста. | |
#апечатайте фамилию самого высокого студента. | |
class Student | |
attr_reader :last_name, :height | |
def initialize(last_name, height) | |
@height = height | |
@last_name = last_name | |
end | |
end | |
class School | |
attr_reader :students_number, :students, :service | |
def initialize(student_number, student_height_generator) | |
@service = student_height_generator | |
@students_number = student_number | |
@students = [] | |
@students_number.times { |i| @students << Student.new(student_height_generator.names[i], student_height_generator.heights[i]) } | |
end | |
def find_tallest | |
student_height_generator.heights.max | |
end | |
end | |
class StudentHeightGenerator | |
attr_accessor :names, :heights, :scholars | |
def initialize(schoolars) | |
@scholars = schoolars | |
@names = ["Sidorov", "Petrov", "Klimovich", "Rakutka", "Shamritskiy", "Glas", "Orlov", "Zhigalov", "Dolgiy", "Dyagel", "Kolenda", "Sidor", "Avddeev", "Rusak", "Krent", "Kharik", "Kuzmenkov", "Civis", "Lyshcik", "Veresovoy"]; | |
@heights = generate_height | |
end | |
private | |
def generate_height | |
result = [] | |
while (result.size < scholars) | |
(scholars - result.size).times { result << rand(160..200) } | |
result.uniq! | |
end | |
result | |
end | |
end | |
class Report | |
attr_accessor :school | |
def initialize(school) | |
@school = school | |
end | |
def print_info | |
school.students.each_with_index do |item, index| | |
if index.equal?(school.find_tallest) | |
puts "#{index}. #{item.last_name} --> #{item.height}sm <<<<<<<-------------------(+)" | |
else | |
puts "#{index}. #{item.last_name} --> #{item.height}sm" | |
end | |
end | |
end | |
end | |
puts "Enter student number: " | |
number_stud = gets.chomp.to_i | |
student_height_generator = StudentHeightGenerator.new(number_stud) | |
school_1 = School.new(number_stud, student_height_generator) | |
school_1.fill_students_information | |
printer = Report.new(school_1) | |
printer.print_info |
aya-soft
commented
Dec 20, 2018
- при вызове методов скобки используй только, если есть аргументы: gets.chomp.to_i
- Service - очень общее название, мы говорили, что классы нужно называть как можно более конкретно. По его названию не понятно, что он делает
- Что мешает вызвать fill_students_information в initialize? Зачем лишняя настройка созданного объекта?
- Printer - как ты догадываешься тоже очень общее название. Ну и потом какой же он принтер? он наверное Report како-нить?
- В этой строчке ты сто раз считаешь school.find_tallest, очень накладно
- Везде у тебя attr_accessor, зачем ты позволяешь изменять то, что только читаешь?
Зачем Школе знать о каком-то сервисе с именами и высотами? Ей занятая нечем? :)
service.heights.rindex(service.heights.max)
Почему ты берешь последнего высокого? service.heights.rindex(service.heights.max)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment