Last active
November 23, 2018 18:04
-
-
Save darya-makarenko/b9e9f9923bab79d3e762da1516fe200c to your computer and use it in GitHub Desktop.
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
class Game | |
attr_reader :won_balls | |
attr_reader :missed_balls | |
def initialize(won_balls, missed_balls) | |
@won_balls = won_balls | |
@missed_balls = missed_balls | |
end | |
def result | |
@result ||= result_text(won_balls <=> missed_balls) | |
end | |
def result_description | |
"you #{result} (#{won_balls}, #{missed_balls})" | |
end | |
private | |
def result_text(value) | |
case value | |
when -1 then "lose" | |
when 0 then "draw" | |
when 1 then "win" | |
end | |
end | |
end |
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
$LOAD_PATH.unshift(".") | |
require "team_statistics" | |
puts "Enter the elements of won balls array: (f.e. 1 2 3 4)" | |
won = gets.chomp.split(" ").map{ |el| el.to_i } | |
puts "Enter the elements of missed balls array: (f.e. 4 3 2 1)" | |
lost = gets.chomp.split(" ").map{ |el| el.to_i } | |
team_stat = TeamStatistics.new(won, lost) | |
team_stat.print_report |
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
class TeamStatistics | |
attr_reader :games | |
def initialize(win, lose) | |
@games = win.zip(lose).map { |scores| Game.new(scores[0], scores[1]) } | |
end | |
def print_report | |
games.each_with_index { |game, i| puts "game #{i + 1}: #{game.result_description}" } | |
end | |
end |
- Подправлю
- Думаю, можно сделать объект класса Game, в котором будет храниться счет игры, и класс Team, в котором будет сделана агрегация всех игр, в которых участвовала команда. Также понадобится класс GamesetAnalyser, которому будет поступать запрос от команды на получение статистики по играм, и который будет выдавать результаты.
Получается Game - событие, которое произошло в конкретную дату и закончилось с конкретным счетом. СОГЛАСЕН!
Какие атрибуты нам понадобятся?
Хм. Необходимо сделать атрибут date (дата), далее won_balls, missed_balls. Можно добавить место проведения - Place и название матча (Имена команд-участников?)
🥇
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Вывод:
Теперь давай ее оптимизируем: