Menu
Coddy logo textTech

Geçersiz Girişleri Yönetme

Coddy'nin Lua Journey'sinin Logic & Flow bölümünün bir parçası — ders 47 / 54.

challenge icon

Görev

Kolay

Geçersiz menü komutları için uygun bir işleme mekanizması ekleyerek kişi listesi uygulamanızı tamamlayın. Kullanıcı mevcut seçeneklerden herhangi biriyle eşleşmeyen bir komut girdiğinde, program onları bilgilendirmeli ve çalışmaya devam etmelidir.

Önceki görevin üzerine inşa ederek; add, list, search, delete veya quit olmayan herhangi bir komutu işlemek için bir else koşulu ekleyin. Bu, tanınmayan bir komut girildiğinde net bir geri bildirim sağlayarak kullanıcı deneyimini iyileştirir.

Gereksinimler:

  • Önceki görevdeki tüm kodları koruyun (kurulum, menü ekranı, ana döngü, ekleme, listeleme, arama ve silme işlevleri)
  • Geçerli seçeneklerle eşleşmeyen herhangi bir komutu işlemek için bir else koşulu ekleyin
  • Geçersiz bir komut girildiğinde şunu yazdırın: Invalid command. Please try again.
  • Program, geçersiz komut mesajını görüntüledikten sonra çalışmaya devam etmelidir
  • Mevcut tüm komutlar (add, list, search, delete, quit) eskisi gibi çalışmaya devam etmelidir

Örnek etkileşim:

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:
show
Invalid command. Please try again.

Enter command:
remove
Invalid command. Please try again.

Enter command:
list
Contact List:
1. Alice - 555-1234

Enter command:
help
Invalid command. Please try again.

Enter command:
quit
Goodbye!

Kendin dene

-- Contact List Manager with Delete Functionality
local contacts = {}

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

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 == "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 == "delete" then
        print("Enter name to delete:")
        local deleteName = io.read()
        local found = false
        
        for i, contact in ipairs(contacts) do
            if contact.name == deleteName then
                table.remove(contacts, i)
                print("Contact deleted successfully!")
                found = true
                break
            end
        end
        
        if not found then
            print("Contact not found.")
        end
        print()
        
    elseif command == "quit" then
        print("Goodbye!")
        break
    end
end

Logic & Flow bölümündeki tüm dersler