small code improvements
This commit is contained in:
parent
ba6a889d35
commit
bb406ead06
9 changed files with 31 additions and 24 deletions
|
@ -3,7 +3,6 @@ package main;
|
|||
import category.CategoryRepository;
|
||||
import category.CategoryUseCase;
|
||||
import category.RandomCategoryIdGenerator;
|
||||
import cli.CliError;
|
||||
import cli.Subcommand;
|
||||
import cli.category.CategoryCliAdapter;
|
||||
import cli.category.CategoryCommands;
|
||||
|
@ -42,20 +41,27 @@ public class CommandHandler {
|
|||
|
||||
public void executeCommand(String[] args) {
|
||||
|
||||
if(args.length == 0){
|
||||
//TODO: print usage
|
||||
if (args.length == 0) {
|
||||
printUsage();
|
||||
return;
|
||||
}
|
||||
|
||||
var command = subcommands.get(args[0]);
|
||||
|
||||
if (command == null) {
|
||||
throw new CliError("Unknown Command");
|
||||
System.out.println("Unkown subcommand!");
|
||||
printUsage();
|
||||
}
|
||||
|
||||
var output = command.executeSubcommand(Arrays.copyOfRange(args, 1, args.length));
|
||||
System.out.println(output);
|
||||
}
|
||||
|
||||
private void printUsage() {
|
||||
System.out.println("All available subcommands: ");
|
||||
subcommands.forEach((name, subcommand) -> System.out.println(System.lineSeparator() + subcommand.getUsage()));
|
||||
}
|
||||
|
||||
private void addSubcommands() {
|
||||
|
||||
CategoryRepository categoryRepository =
|
||||
|
|
|
@ -34,7 +34,7 @@ public class GenericCSVDAO<T extends CSVSerializable> implements GenericDAO<T> {
|
|||
}
|
||||
}
|
||||
|
||||
public Set<T> getALl() {
|
||||
public Set<T> getAll() {
|
||||
var lines = new ArrayList<String>();
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(this.file))) {
|
||||
for (String line; (line = br.readLine()) != null; ) {
|
||||
|
@ -53,7 +53,7 @@ public class GenericCSVDAO<T extends CSVSerializable> implements GenericDAO<T> {
|
|||
|
||||
@Override
|
||||
public void remove(T value) {
|
||||
var allEntities = this.getALl();
|
||||
var allEntities = this.getAll();
|
||||
this.removeAll();
|
||||
|
||||
allEntities.stream().filter(entity -> !entity.equals(value)).forEach(this::add);
|
||||
|
|
|
@ -36,10 +36,10 @@ class GenericCSVDAOTest {
|
|||
|
||||
@Test
|
||||
public void addWorks() throws IOException {
|
||||
assertEquals(0, sut.getALl().size());
|
||||
assertEquals(0, sut.getAll().size());
|
||||
|
||||
CategoryEntity entityToAdd = addDummyEntity("categoryName", 99);
|
||||
var foundEntity = sut.getALl().stream().findFirst().orElseThrow();
|
||||
var foundEntity = sut.getAll().stream().findFirst().orElseThrow();
|
||||
|
||||
assertEquals(entityToAdd, foundEntity);
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ class GenericCSVDAOTest {
|
|||
|
||||
sut.remove(entityToAdd);
|
||||
|
||||
assertEquals(0, sut.getALl().size());
|
||||
assertEquals(0, sut.getAll().size());
|
||||
}
|
||||
|
||||
|
||||
|
@ -58,10 +58,10 @@ class GenericCSVDAOTest {
|
|||
public void removeAllWorks() throws IOException {
|
||||
addDummyEntity("categoryName1", 101);
|
||||
addDummyEntity("categoryName2", 102);
|
||||
assertEquals(2, sut.getALl().size());
|
||||
assertEquals(2, sut.getAll().size());
|
||||
sut.removeAll();
|
||||
|
||||
assertEquals(0, sut.getALl().size());
|
||||
assertEquals(0, sut.getAll().size());
|
||||
}
|
||||
|
||||
private CategoryEntity addDummyEntity(String categoryName, int id) {
|
||||
|
|
Reference in a new issue