Compare commits
3
Commits
55030af64d
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3f7a3a5f98 | ||
|
|
1736f62660 | ||
|
|
7049ba14b4 |
No files matched your search
@@ -48,5 +48,36 @@
|
||||
# => { action: :invalid }
|
||||
|
||||
def parse_command(input)
|
||||
# À compléter.
|
||||
response = {}
|
||||
parts = input.strip.split
|
||||
|
||||
case parts[0]
|
||||
when "help"
|
||||
if parts.size == 1
|
||||
response[:action] = :help
|
||||
else
|
||||
response[:action] = :invalid
|
||||
end
|
||||
when "quit"
|
||||
if parts.size == 1
|
||||
response[:action] = :quit
|
||||
else
|
||||
response[:action] = :invalid
|
||||
end
|
||||
when "play"
|
||||
if parts.size == 2 && parts[1].match?(/\A\d+\z/) && parts[1].to_i > 0
|
||||
response[:action] = :play
|
||||
response[:card] = parts[1].to_i
|
||||
else
|
||||
response[:action] = :invalid
|
||||
end
|
||||
else
|
||||
response[:action] = :invalid
|
||||
end
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
print "Entrez une action : "
|
||||
input = gets.chomp
|
||||
puts parse_command(input)
|
||||
@@ -39,9 +39,9 @@ def meal_totals(meals)
|
||||
total = {}
|
||||
meals.each do |meal|
|
||||
unless total.key?(meal[:food])
|
||||
total[(meal[:food])] = 0
|
||||
total[meal[:food]] = 0
|
||||
end
|
||||
total[(meal[:food])] += meal[:quantity]
|
||||
total[meal[:food]] += meal[:quantity]
|
||||
end
|
||||
total
|
||||
end
|
||||
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
class PeopleDirectory
|
||||
def initialize(people)
|
||||
@people = people
|
||||
end
|
||||
|
||||
def find_people(name)
|
||||
@people.select { |person| person[:name] == name }
|
||||
end
|
||||
end
|
||||
|
||||
class UserInput
|
||||
def ask_name
|
||||
print "De quelle personne souhaites-tu connaître l'âge ? "
|
||||
gets.chomp
|
||||
end
|
||||
end
|
||||
|
||||
class PeopleView
|
||||
def hello_everybody(people)
|
||||
people.each do |person|
|
||||
puts "Hello #{person[:name]}!"
|
||||
end
|
||||
end
|
||||
|
||||
def show_people(people)
|
||||
if people.empty?
|
||||
puts "Aucune personne trouvée."
|
||||
else
|
||||
people.each do |person|
|
||||
puts "#{person[:name]} qui a #{person[:age]} ans"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Données
|
||||
people = [
|
||||
{ name: "Alice", age: 30},
|
||||
{ name: "Bob", age: 27},
|
||||
{ name: "Alice", age: 22}
|
||||
]
|
||||
|
||||
# Instancier les classes
|
||||
directory = PeopleDirectory.new(people)
|
||||
input = UserInput.new
|
||||
view = PeopleView.new
|
||||
|
||||
# Saluer tout le monde
|
||||
view.hello_everybody(people)
|
||||
|
||||
# Demander un nom
|
||||
name = input.ask_name
|
||||
|
||||
# Rechercher
|
||||
matches = directory.find_people(name)
|
||||
|
||||
# Afficher
|
||||
view.show_people(matches)
|
||||
|
||||
Reference in new issue
Block a user