fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12.  
  13.  
  14. List<SupportedProduct> configuredProducts = buildProducts();
  15.  
  16. // 2wheeler will return [oil]
  17. // 4wheeler will return [tires, oil]
  18. List<String> productNames = getProductNames(configuredProducts, "4wheeler");
  19.  
  20. System.out.println(productNames);
  21.  
  22. }
  23.  
  24. private static List<SupportedProduct> buildProducts() {
  25. CategoryDetails c1 = new CategoryDetails("cars", null);
  26. SupportedProduct p1 = new SupportedProduct("tires", c1);
  27.  
  28. CategoryDetails c2 = new CategoryDetails(null, Arrays.asList(new Category("cars"), new Category("bikes")));
  29. SupportedProduct p2 = new SupportedProduct("oil", c2);
  30.  
  31. return Arrays.asList(p1, p2);
  32.  
  33. }
  34.  
  35. private static List<String> getProductNames(List<SupportedProduct> configuredProducts, String requestedGroup) {
  36.  
  37. List<String> productNames = new ArrayList<>();
  38.  
  39. for (SupportedProduct supportedProduct : configuredProducts) {
  40. List<String> categoryNameList = new ArrayList<>();
  41. String activeCategoryName = supportedProduct.getCategoryDetails().getActiveCategoryName();
  42. if (activeCategoryName == null) {
  43. Optional.ofNullable(supportedProduct.getCategoryDetails().getCategories())
  44. .orElse(Collections.emptyList()).forEach(category -> categoryNameList.add(category.getName()));
  45. } else {
  46. categoryNameList.add(activeCategoryName);
  47. }
  48. for (String catName : categoryNameList) {
  49. Division division = DivisionRepo.getDivisionByCatName(catName);
  50. if (division != null && division.getGroup() == requestedGroup) {
  51. productNames.add(supportedProduct.getProductName());
  52. }
  53. }
  54. }
  55.  
  56. return productNames;
  57. }
  58. }
  59.  
  60. class SupportedProduct {
  61.  
  62. String productName;
  63. CategoryDetails categoryDetails;
  64.  
  65. public SupportedProduct(String productName, CategoryDetails categoryDetails) {
  66. this.productName = productName;
  67. this.categoryDetails = categoryDetails;
  68. }
  69.  
  70. public String getProductName() {
  71. return productName;
  72. }
  73.  
  74. public CategoryDetails getCategoryDetails() {
  75. return categoryDetails;
  76. }
  77.  
  78. }
  79.  
  80. class CategoryDetails {
  81.  
  82. String activeCategoryName;
  83. List<Category> categories;
  84.  
  85. public CategoryDetails(String activeCategoryName, List<Category> categories) {
  86. this.activeCategoryName = activeCategoryName;
  87. this.categories = categories;
  88. }
  89.  
  90. public String getActiveCategoryName() {
  91. return activeCategoryName;
  92. }
  93.  
  94. public List<Category> getCategories() {
  95. return categories;
  96. }
  97. }
  98.  
  99. class Category {
  100. String name;
  101.  
  102. public Category(String name) {
  103. this.name = name;
  104. }
  105.  
  106. public String getName() {
  107. return name;
  108. }
  109. }
  110.  
  111. class Division {
  112.  
  113. String group;
  114.  
  115. public Division(String group) {
  116. this.group = group;
  117. }
  118.  
  119. public String getGroup() {
  120. return group;
  121. }
  122.  
  123. }
  124.  
  125. class DivisionRepo {
  126.  
  127. static Division getDivisionByCatName(String catName) {
  128. if (catName.equals("cars")) {
  129. return new Division("4wheeler");
  130. } else if (catName.equals("bikes")) {
  131. return new Division("2wheeler");
  132. }
  133. return null;
  134. }
  135. }
  136.  
Success #stdin #stdout 0.09s 33436KB
stdin
Standard input is empty
stdout
[tires, oil]