Menu
Coddy logo textTech

הצגת כל אנשי הקשר

חלק מהיחידה לוגיקה וזרימת התוכנית במסלול ה-Lua של Coddy. שיעור 44 מתוך 54.

challenge icon

אתגר

קל

ממשו את הפונקציונליות להצגת הרשימה באפליקציית רשימת אנשי הקשר שלכם. כשהמשתמש מזין list, התוכנית צריכה להציג את כל אנשי הקשר השמורים בפורמט מסודר.

בהמשך לאתגר הקודם, הוסיפו פונקציונליות לטיפול בפקודה list. השתמשו ב-ipairs() כדי לעבור על הטבלה contacts ולהציג את הפרטים של כל איש קשר. אם הרשימה ריקה, הודיעו למשתמש בהתאם.

דרישות:

  • השאירו את כל הקוד מהאתגר הקודם (הגדרה, הצגת תפריט, לולאה ראשית ופונקציונליות ההוספה)
  • כשהמשתמש מזין list, בדקו אם הטבלה contacts ריקה
  • אם היא ריקה, הדפיסו: No contacts found.
  • אם היא אינה ריקה, הדפיסו: Contact List:
  • השתמשו ב-ipairs() כדי לעבור על הטבלה contacts
  • עבור כל איש קשר, הדפיסו בפורמט: [index]. [name] - [phone]
  • הפקודות add ו-quit עדיין צריכות לפעול כמו קודם
  • פקודות אחרות עדיין צריכות להדפיס Command not yet implemented

אינטראקציה לדוגמה:

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!

נסו בעצמכם

-- 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
end

כל השיעורים ביחידה לוגיקה וזרימת התוכנית

תרגלו בעצמכם: קומפיילר Lua אונליין