/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
List<SupportedProduct> configuredProducts = buildProducts();
// 2wheeler will return [oil]
// 4wheeler will return [tires, oil]
List<String> productNames = getProductNames(configuredProducts, "4wheeler");
System.
out.
println(productNames
);
}
private static List<SupportedProduct> buildProducts() {
CategoryDetails c1 = new CategoryDetails("cars", null);
SupportedProduct p1 = new SupportedProduct("tires", c1);
CategoryDetails c2
= new CategoryDetails
(null,
Arrays.
asList(new Category
("cars"),
new Category
("bikes"))); SupportedProduct p2 = new SupportedProduct("oil", c2);
}
private static List
<String
> getProductNames
(List
<SupportedProduct
> configuredProducts,
String requestedGroup
) {
List<String> productNames = new ArrayList<>();
for (SupportedProduct supportedProduct : configuredProducts) {
List<String> categoryNameList = new ArrayList<>();
String activeCategoryName
= supportedProduct.
getCategoryDetails().
getActiveCategoryName(); if (activeCategoryName == null) {
Optional.ofNullable(supportedProduct.getCategoryDetails().getCategories())
.
orElse(Collections.
emptyList()).
forEach(category
-> categoryNameList.
add(category.
getName())); } else {
categoryNameList.add(activeCategoryName);
}
for (String catName
: categoryNameList
) { Division division = DivisionRepo.getDivisionByCatName(catName);
if (division != null && division.getGroup() == requestedGroup) {
productNames.add(supportedProduct.getProductName());
}
}
}
return productNames;
}
}
class SupportedProduct {
CategoryDetails categoryDetails;
public SupportedProduct
(String productName, CategoryDetails categoryDetails
) { this.productName = productName;
this.categoryDetails = categoryDetails;
}
public String getProductName
() { return productName;
}
public CategoryDetails getCategoryDetails() {
return categoryDetails;
}
}
class CategoryDetails {
List<Category> categories;
public CategoryDetails
(String activeCategoryName, List
<Category
> categories
) { this.activeCategoryName = activeCategoryName;
this.categories = categories;
}
public String getActiveCategoryName
() { return activeCategoryName;
}
public List<Category> getCategories() {
return categories;
}
}
class Category {
public Category
(String name
) { this.name = name;
}
return name;
}
}
class Division {
public Division
(String group
) { this.group = group;
}
return group;
}
}
class DivisionRepo {
static Division getDivisionByCatName
(String catName
) { if (catName.equals("cars")) {
return new Division("4wheeler");
} else if (catName.equals("bikes")) {
return new Division("2wheeler");
}
return null;
}
}