Compare commits

..
3 Commits
Author SHA1 Message Date
julien 3f7a3a5f98 add mvc 2026-10-26 09:52:44 +01:00
julien 1736f62660 exercise done 2026-10-18 17:07:57 +02:00
julien 7049ba14b4 exercise done 2026-10-16 16:11:23 +02:00
3 changed files with 95 additions and 3 deletions

No files matched your search

@@ -48,5 +48,36 @@
# => { action: :invalid } # => { action: :invalid }
def parse_command(input) 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 end
print "Entrez une action : "
input = gets.chomp
puts parse_command(input)
@@ -39,9 +39,9 @@ def meal_totals(meals)
total = {} total = {}
meals.each do |meal| meals.each do |meal|
unless total.key?(meal[:food]) unless total.key?(meal[:food])
total[(meal[:food])] = 0 total[meal[:food]] = 0
end end
total[(meal[:food])] += meal[:quantity] total[meal[:food]] += meal[:quantity]
end end
total total
end end
+61
View File
@@ -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)