Afficher tous les contacts
Fait partie de la section Logic & Flow du Journey Lua de Coddy — leçon 44 sur 54.
Défi
FacileImplémentez la fonctionnalité de liste pour votre application de gestion de contacts. Lorsque l'utilisateur saisit list, le programme doit afficher tous les contacts enregistrés de manière formatée.
En vous basant sur le défi précédent, ajoutez la fonctionnalité pour gérer la commande list. Utilisez ipairs() pour parcourir la table contacts et afficher les informations de chaque contact. Si la liste est vide, informez l'utilisateur en conséquence.
Exigences :
- Conservez tout le code du défi précédent (configuration, affichage du menu, boucle principale et fonctionnalité d'ajout)
- Lorsque l'utilisateur saisit
list, vérifiez si la tablecontactsest vide - Si elle est vide, affichez :
No contacts found. - Si elle n'est pas vide, affichez :
Contact List: - Utilisez
ipairs()pour parcourir la tablecontacts - Pour chaque contact, affichez au format :
[index]. [name] - [phone] - Les commandes
addetquitdoivent toujours fonctionner comme avant - 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:
list
No contacts found.
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:
list
Contact List:
1. Alice - 555-1234
2. Bob - 555-5678
Enter command:
quit
Goodbye!Essayez vous-même
-- Initialize contacts list
local contacts = {}
-- Display welcome message and menu
print("Welcome to Contact List Manager!")
print()
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()
local contact = {name = name, phone = phone}
table.insert(contacts, contact)
print("Contact added successfully!")
print()
elseif command == "quit" then
print("Goodbye!")
break
else
print("Command not yet implemented")
print()
end
endToutes les leçons de Logic & Flow
1Itération avancée des tables
Itérer avec pairs()Itérer avec ipairs()pairs() vs. ipairs()Récapitulatif - Fiche de personnage2Plus de fonctions de la bibliothèque table
table.concat()Construction de table & unpack()table.sort()Tri personnalisé avec des fonctionsRécapitulatif - Tableau des scores5Projet : Moteur de jeu d'aventure textuel
Configuration du projet : La pièceRelier les pièces8Projet : Liste de contacts
Configuration du projetLa boucle principale3Concepts avancés sur les fonctions
Retourner plusieurs valeursFonctions variadiques (...)Les fonctions comme valeurs de première classeFonctions anonymesQu'est-ce qu'une closure ?Récapitulatif - Gestionnaire d'événements simple