Menu
CoddyTech

Xóa liên hệ

Thuộc phần Logic và luồng chương trình trong hành trình Lua của Coddy. Bài học 46 trên 54.

challenge icon

Thử thách

Dễ

Triển khai chức năng xóa cho ứng dụng danh sách liên hệ. Khi người dùng nhập delete, chương trình cần yêu cầu nhập tên, tìm liên hệ đó trong danh sách rồi xóa.

Dựa trên thử thách trước, hãy thêm chức năng xử lý lệnh delete. Duyệt qua bảng contacts để tìm liên hệ có tên trùng khớp, sau đó dùng table.remove() để xóa liên hệ đó khỏi danh sách. Hãy thông báo cho người dùng biết việc xóa có thành công hay không.

Yêu cầu:

  • Giữ lại toàn bộ code từ thử thách trước: thiết lập, hiển thị menu, vòng lặp chính, chức năng thêm, liệt kê và tìm kiếm
  • Khi người dùng nhập delete, in ra: Enter name to delete:
  • Đọc tên bằng io.read()
  • Dùng ipairs() để duyệt qua bảng contacts
  • Với mỗi liên hệ, kiểm tra xem trường name có khớp chính xác với tên cần xóa hay không
  • Nếu tìm thấy kết quả khớp, dùng table.remove() cùng với chỉ số của liên hệ để xóa liên hệ đó
  • Sau khi xóa thành công, in ra: Contact deleted successfully!
  • Nếu đã kiểm tra tất cả liên hệ mà không tìm thấy kết quả khớp, in ra: Contact not found.
  • Khi dùng lệnh list mà không có liên hệ nào, in ra: No contacts available.
  • Các lệnh add, list, search và quit vẫn phải hoạt động như trước

Lưu ý: Thông báo danh sách trống trong thử thách này là No contacts available.. Hãy dùng chính xác chuỗi này, ngay cả khi bài học trước dùng cách diễn đạt khác.

Ví dụ tương tác:

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!

Tự làm thử

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

Tất cả bài học trong Logic và luồng chương trình

Tự luyện tập: Trình biên dịch Lua online