Skip to content
Snippets Groups Projects
Commit f251659e authored by Karsch Lukas's avatar Karsch Lukas
Browse files

categories now use lists instead of sets. able to remove categories by name

parent 5fd1d42e
Branches
No related tags found
No related merge requests found
package mi.hdm.recipes;
import java.util.Set;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class Categories {
private static final Categories instance = new Categories();
private static final Logger log = LogManager.getLogger(Categories.class);
private final List<Category> allCategories;
private Categories() {
allCategories = new ArrayList<>();
}
private Set<Category> allCategories;
public static Categories getInstance() {
return instance;
}
/**
* Adds a category if neither the given name nor the color code have been used in a category before.
* @param name Name for the category that is to be added
* @param colourCode Color code for the category that is to be added
* @return True if the category has been added (meaning no equal category existed before), otherwise false.
*/
public boolean addCategory(String name, int colourCode){
return allCategories.add(new Category(name, colourCode));
Category c = new Category(name, colourCode);
if(allCategories.contains(c)) {
log.debug("Category: {} not added", c.getName());
return false;
} else {
allCategories.add(c);
log.debug("Category {} added successfully.", c.getName());
return true;
}
}
public boolean deleteCategory(Category c) {
return allCategories.remove(c);
}
public void removeCategory(String name) {}
public boolean deleteCategory(String name) {
Category c = getCategoryByName(name).orElseThrow();
return allCategories.remove(c);
}
public Set<Category> getAllCategories(){
public List<Category> getAllCategories(){
return allCategories;
}
public void clearCategories() {
allCategories.clear();
}
private Optional<Category> getCategoryByName(String name) {
return allCategories.stream()
.filter(c -> c.getName().equals(name))
.findFirst();
}
}
......@@ -20,7 +20,8 @@ public class Category {
return name;
}
@Override public boolean equals(Object o){
@Override
public boolean equals(Object o){
if (o instanceof Category) {
Category c = (Category) o;
return this.name.equals(c.getName()) || this.colourCode == c.getColourCode();
......@@ -28,4 +29,9 @@ public class Category {
return false;
}
@Override
public String toString() {
return String.format("Category %s with color code %d", name, colourCode);
}
}
......@@ -48,11 +48,7 @@ public class RecipeManager {
}
private Optional<Recipe> getRecipeByName(String name) {
/*boolean b = recipes.stream()
.anyMatch(r -> r.getName().equals(name));
if(!b) return Optional.empty();*/
for (final Recipe r: recipes) {
for (final Recipe r : recipes) {
if (name.equals(r.getName())) {
return Optional.of(r);
}
......
......@@ -14,7 +14,7 @@
<Logger name="mi.hdm.GuiDriver" level="debug">
<AppenderRef ref="A1"/>
</Logger>
<Root level="info">
<Root level="debug">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
......
......@@ -7,10 +7,11 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class CategoryTest {
Categories underTest = new Categories();
Categories underTest = Categories.getInstance();
@Test
public void testEquals() {
underTest.clearCategories();
Category c1 = new Category("Abend", 0x00FF00);
Category c2 = new Category("LEcker", 0x13);
Category c3 = new Category("ew", 0x13);
......@@ -23,14 +24,41 @@ public class CategoryTest {
@Test
public void shouldNotBeAbleToAddIdenticalName() {
underTest.clearCategories();
underTest.addCategory("Mittag", 0x100000);
underTest.addCategory("Mittag", 0x100000);
underTest.addCategory("Mittag", 0x100040);
Assert.assertEquals(1, underTest.getAllCategories().size());
}
@Test
public void shouldNotBeAbleToAddIdenticalCode() {
underTest.clearCategories();
underTest.addCategory("Morgen", 0x100040);
underTest.addCategory("Mittag", 0x100040);
Assert.assertEquals(1, underTest.getAllCategories().size());
}
@Test
public void shouldBeAbleToAddCategory() {
underTest.clearCategories();
underTest.addCategory("Morgen", 0x100000);
underTest.addCategory("Abend", 0x100010);
Assert.assertEquals(2, underTest.getAllCategories().size());
}
@Test
public void canRemoveCategoryByName() {
//given
underTest.clearCategories();
underTest.addCategory("Jan ist cool", 0xFF0000);
//when
underTest.deleteCategory("Jan ist cool");
//expect
assertEquals(0, underTest.getAllCategories().size());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment