feat(command): add RemoveById command
This commit is contained in:
@@ -6,6 +6,7 @@ import command.commands.Clear
|
|||||||
import command.commands.Exit
|
import command.commands.Exit
|
||||||
import command.commands.Help
|
import command.commands.Help
|
||||||
import command.commands.Info
|
import command.commands.Info
|
||||||
|
import command.commands.RemoveById
|
||||||
import command.commands.Show
|
import command.commands.Show
|
||||||
import command.commands.Update
|
import command.commands.Update
|
||||||
import io.IOHandler
|
import io.IOHandler
|
||||||
@@ -26,5 +27,6 @@ class AppInitializer {
|
|||||||
commandManager.register(Show(io, collectionManager))
|
commandManager.register(Show(io, collectionManager))
|
||||||
commandManager.register(Info(io, collectionManager))
|
commandManager.register(Info(io, collectionManager))
|
||||||
commandManager.register(Update(io, collectionManager))
|
commandManager.register(Update(io, collectionManager))
|
||||||
|
commandManager.register(RemoveById(io, collectionManager))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
28
app/src/main/kotlin/command/commands/RemoveById.kt
Normal file
28
app/src/main/kotlin/command/commands/RemoveById.kt
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package command.commands
|
||||||
|
|
||||||
|
import command.Command
|
||||||
|
import io.IOHandler
|
||||||
|
import manager.CollectionManager
|
||||||
|
|
||||||
|
class RemoveById(
|
||||||
|
private val io: IOHandler,
|
||||||
|
private val collectionManager: CollectionManager,
|
||||||
|
) : Command {
|
||||||
|
override val name = "remove_by_id"
|
||||||
|
override val description = "remove element by id"
|
||||||
|
|
||||||
|
override fun execute(args: String) {
|
||||||
|
val id = args.trim().toLongOrNull()
|
||||||
|
if (id == null) {
|
||||||
|
io.println("введите id, к примеру: remove_by_id 5")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (collectionManager.getCollection().none { it.id == id }) {
|
||||||
|
io.println("элемент с id=$id не найден")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
collectionManager.removeById(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -38,10 +38,7 @@ class CollectionManager(
|
|||||||
id: Long,
|
id: Long,
|
||||||
newProduct: Product,
|
newProduct: Product,
|
||||||
) {
|
) {
|
||||||
val index =
|
val index = list.indexOfFirst { it.id == id }
|
||||||
list.indexOfFirst {
|
|
||||||
it.id == id
|
|
||||||
}
|
|
||||||
|
|
||||||
val old = list[index]
|
val old = list[index]
|
||||||
|
|
||||||
@@ -56,6 +53,12 @@ class CollectionManager(
|
|||||||
io.println("элемент обновлен")
|
io.println("элемент обновлен")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun removeById(id: Long) {
|
||||||
|
val index = list.indexOfFirst { it.id == id }
|
||||||
|
list.removeAt(index)
|
||||||
|
io.println("элемент удален")
|
||||||
|
}
|
||||||
|
|
||||||
fun getCollection(): LinkedList<Product> = list
|
fun getCollection(): LinkedList<Product> = list
|
||||||
|
|
||||||
fun clear() {
|
fun clear() {
|
||||||
|
|||||||
Reference in New Issue
Block a user