Menu
Coddy logo textTech

Rechercher un contact

Fait partie de la section Logic & Flow du Journey Lua de Coddy — leçon 45 sur 54.

challenge icon

Défi

Facile

Implémentez la fonctionnalité de recherche pour votre application de liste de contacts. Lorsque l'utilisateur saisit search, le programme doit demander un nom et afficher tous les contacts correspondants.

En vous basant sur le défi précédent, ajoutez la fonctionnalité pour gérer la commande search. Parcourez la table contacts pour trouver les contacts dont les noms correspondent au terme de recherche. Affichez toutes les correspondances ou informez l'utilisateur si aucun contact n'est trouvé.

Exigences :

  • Conservez tout le code du défi précédent (configuration, affichage du menu, boucle principale, fonctionnalité d'ajout et fonctionnalité de liste)
  • Lorsque l'utilisateur saisit search, affichez : Enter name to search:
  • Lisez le terme de recherche en utilisant io.read()
  • Utilisez ipairs() pour itérer à travers la table contacts
  • Pour chaque contact, vérifiez si le champ name correspond exactement au terme de recherche
  • Si une correspondance est trouvée, affichez au format : Found: [name] - [phone]
  • Si aucune correspondance n'est trouvée après avoir vérifié tous les contacts, affichez : Contact not found.
  • Les commandes add, list, et quit doivent toujours fonctionner comme auparavant
  • Les autres commandes doivent toujours afficher Command not yet implemented

Exemple d'interaction :

Welcome to Contact List Manager!

Available Commands:
add - Add a new contact
list - Display all contacts
search - Search for a contact
delete - Delete a contact
quit - Exit the program

Enter command:
add
Enter name:
Alice
Enter phone number:
555-1234
Contact added successfully!

Enter command:
add
Enter name:
Bob
Enter phone number:
555-5678
Contact added successfully!

Enter command:
search
Enter name to search:
Alice
Found: Alice - 555-1234

Enter command:
search
Enter name to search:
Charlie
Contact not found.

Enter command:
quit
Goodbye!

Essayez vous-même

-- Contact List Manager
local contacts = {}

-- Display welcome message
print("Welcome to Contact List Manager!")
print()

-- Display menu
print("Available Commands:")
print("add - Add a new contact")
print("list - Display all contacts")
print("search - Search for a contact")
print("delete - Delete a contact")
print("quit - Exit the program")
print()

-- Main loop
while true do
    print("Enter command:")
    local command = io.read()
    
    if command == "add" then
        print("Enter name:")
        local name = io.read()
        print("Enter phone number:")
        local phone = io.read()
        
        table.insert(contacts, {name = name, phone = phone})
        print("Contact added successfully!")
        print()
        
    elseif command == "list" then
        if #contacts == 0 then
            print("No contacts found.")
        else
            print("Contact List:")
            for i, contact in ipairs(contacts) do
                print(i .. ". " .. contact.name .. " - " .. contact.phone)
            end
        end
        print()
        
    elseif command == "quit" then
        print("Goodbye!")
        break
        
    else
        print("Command not yet implemented")
        print()
    end
end

Toutes les leçons de Logic & Flow