forked from pabulaner/vivi
51 lines
1.7 KiB
Java
51 lines
1.7 KiB
Java
|
|
package de.vivi;
|
||
|
|
|
||
|
|
import de.vivi.bank.Bank;
|
||
|
|
|
||
|
|
import java.util.HashMap;
|
||
|
|
import java.util.Map;
|
||
|
|
import java.util.function.BiConsumer;
|
||
|
|
|
||
|
|
public class Command {
|
||
|
|
|
||
|
|
private static final Map<String, Command> COMMANDS = new HashMap<String, Command>() {{
|
||
|
|
put("create", new Command(1, (bank, args) -> bank.create(args[1])));
|
||
|
|
put("info", new Command(1, (bank, args) -> bank.info(Integer.parseInt(args[1]))));
|
||
|
|
put("add", new Command(1, (bank, args) -> bank.add(Integer.parseInt(args[1]), Integer.parseInt(args[2]))));
|
||
|
|
put("remove", new Command(1, (bank, args) -> bank.remove(Integer.parseInt(args[1]), Integer.parseInt(args[2]))));
|
||
|
|
put("transfer", new Command(1, (bank, args) -> bank.transfer(Integer.parseInt(args[1]), Integer.parseInt(args[2]), Integer.parseInt(args[3]))));
|
||
|
|
put("transactions", new Command(1, (bank, args) -> bank.transactions(Integer.parseInt(args[1]))));
|
||
|
|
}};
|
||
|
|
|
||
|
|
private final int args;
|
||
|
|
|
||
|
|
private final BiConsumer<Bank, String[]> consumer;
|
||
|
|
|
||
|
|
private Command(int args, BiConsumer<Bank, String[]> consumer) {
|
||
|
|
this.args = args;
|
||
|
|
this.consumer = consumer;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void exec(Bank bank, String[] args) {
|
||
|
|
if (args.length > 0) {
|
||
|
|
Command command = COMMANDS.get(args[0]);
|
||
|
|
|
||
|
|
if (command == null) {
|
||
|
|
System.out.println("Command not found");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (command.args != args.length) {
|
||
|
|
System.out.println("Invalid argument count");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
command.consumer.accept(bank, args);
|
||
|
|
} catch (NumberFormatException e) {
|
||
|
|
System.out.println("Invalid argument");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|