Menu
Coddy logo textTech

מחיקת איש קשר

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

challenge icon

אתגר

קל

ממשו את פונקציונליות המחיקה באפליקציית רשימת אנשי הקשר שלכם. כשהמשתמש מזין delete, התוכנית צריכה לבקש שם, למצוא את איש הקשר הזה ברשימה ולהסיר אותו.

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

דרישות:

  • השאירו את כל הקוד מהאתגר הקודם (הגדרה, הצגת התפריט, הלולאה הראשית, ופונקציונליות ההוספה, הרשימה והחיפוש)
  • כשהמשתמש מזין delete, הדפיסו: Enter name to delete:
  • קראו את השם באמצעות io.read()
  • השתמשו ב-ipairs() כדי לעבור על הטבלה contacts
  • עבור כל איש קשר, בדקו אם השדה name תואם בדיוק לשם שיש למחוק
  • אם נמצאה התאמה, השתמשו ב-table.remove() עם האינדקס של איש הקשר כדי למחוק אותו
  • לאחר מחיקה מוצלחת, הדפיסו: Contact deleted successfully!
  • אם לא נמצאה התאמה לאחר בדיקת כל אנשי הקשר, הדפיסו: Contact not found.
  • כאשר משתמשים בפקודה list ואין אנשי קשר, הדפיסו: No contacts available.
  • הפקודות add,‏ list,‏ search ו-quit עדיין צריכות לפעול כבעבר

הערה: הודעת הרשימה הריקה באתגר הזה היא No contacts available.. הקפידו להשתמש במחרוזת המדויקת הזאת, גם אם בשיעור קודם נעשה שימוש בניסוח אחר.

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

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:
list
Contact List:
1. Alice - 555-1234
2. Bob - 555-5678

Enter command:
delete
Enter name to delete:
Alice
Contact deleted successfully!

Enter command:
list
Contact List:
1. Bob - 555-5678

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

Enter command:
quit
Goodbye!

נסו בעצמכם

-- 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 available.")
        else
            print("Contact List:")
            for i, contact in ipairs(contacts) do
                print(i .. ". " .. contact.name .. " - " .. contact.phone)
            end
        end
        print()
        
    elseif command == "search" then
        print("Enter name to search:")
        local searchName = io.read()
        local found = false
        
        for i, contact in ipairs(contacts) do
            if contact.name == searchName then
                print("Found: " .. contact.name .. " - " .. contact.phone)
                found = true
                break
            end
        end
        
        if not found then
            print("Contact not found.")
        end
        print()
        
    elseif command == "quit" then
        print("Goodbye!")
        break
        
    else
        print("Command not yet implemented")
        print()
    end
end

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

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