31 lines
1.1 KiB
Java
31 lines
1.1 KiB
Java
package me.zinch.commands;
|
||
|
||
import me.zinch.console.Console;
|
||
import me.zinch.exceptions.CommandActionException;
|
||
import me.zinch.wrapper.ProductCollection;
|
||
|
||
import java.util.regex.Pattern;
|
||
|
||
/**
|
||
* A command to remove an element from the collection by its id.
|
||
*/
|
||
public class Remove extends Command {
|
||
public Remove() {
|
||
super("remove_by_id id", "удалить элемент из коллекции по его id", Pattern.compile("^remove_by_id .+"));
|
||
}
|
||
|
||
@Override
|
||
public String action(ProductCollection productCollection) throws CommandActionException {
|
||
try {
|
||
Long id = Long.parseLong(Console.getLastCommand().split(" ")[1]);
|
||
if (productCollection.isProductIdExists(id)) {
|
||
var product = productCollection.removeProduct(id);
|
||
return String.format("Продукт %s был удалён", product.getName());
|
||
}
|
||
return "Продукта с таким id не существует";
|
||
} catch (NumberFormatException e) {
|
||
return "Неверный аргумент, id может быть только число";
|
||
}
|
||
}
|
||
}
|