Menu
Coddy logo textTech

連絡先の削除

CoddyのLuaジャーニー「ロジックと制御フロー」セクションの一部。レッスン 46/54。

challenge icon

チャレンジ

簡単

連絡先リストアプリケーションのdelete機能を実装してください。ユーザーがdeleteと入力したら、programは名前の入力を求め、その連絡先をリストから見つけて削除します。

前のチャレンジを基に、delete commandを処理するFunctionalityを追加してください。contacts tableをループして、一致する名前の連絡先を見つけ、table.remove()を使用してリストから削除します。削除がsuccessfully行われたかどうかをユーザーに知らせてください。

Requirements:

  • 前のチャレンジのすべてのcode(セットアップ、menuのDisplay、main loop、add、list、search Functionality)を保持する
  • ユーザーがdeleteを入力したら、次をprintする:Enter name to delete:
  • io.read()を使用して名前をreadする
  • ipairs()を使用してcontacts tableを反復処理する
  • 各contactについて、name fieldが削除対象の名前と完全に一致するか確認する
  • 一致するcontactがfoundしたら、contactのindexとともにtable.remove()を使用して削除する
  • 削除がsuccessfully行われた後、次をprintする:Contact deleted successfully!
  • すべてのcontactsを確認しても一致するものがfoundしなかった場合、次をprintする:Contact not found.
  • list commandが使用され、contactsがない場合、次をprintする:No contacts available.
  • addlistsearchquit commandsは以前と同様に動作する必要がある

Note: このチャレンジでの空のlistのmessageはNo contacts available.です。前のlessonで異なる表現が使われていた場合でも、このexactなstringを使用してください。

Example 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:
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オンラインコンパイラ