Instantly share code, notes, and snippets.
Created
August 19, 2014 13:15
-
Star
0
(0)
You must be signed in to star a gist -
Fork
1
(1)
You must be signed in to fork a gist
-
Save adityasatrio/695ccdf15f90752e741d to your computer and use it in GitHub Desktop.
Menu : parent and child menu, collapsing the child under the parent menu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package menu.test; | |
import java.io.Serializable; | |
import java.util.List; | |
public class Menu implements Serializable { | |
private Long id; | |
private Long parentid; | |
private String name; | |
private List<Menu> childMenu; | |
/** | |
* @return the id | |
*/ | |
public Long getId() { | |
return id; | |
} | |
/** | |
* @param id | |
* the id to set | |
*/ | |
public void setId(Long id) { | |
this.id = id; | |
} | |
/** | |
* @return the parentid | |
*/ | |
public Long getParentid() { | |
return parentid; | |
} | |
/** | |
* @param parentid | |
* the parentid to set | |
*/ | |
public void setParentid(Long parentid) { | |
this.parentid = parentid; | |
} | |
/** | |
* @return the name | |
*/ | |
public String getName() { | |
return name; | |
} | |
/** | |
* @param name | |
* the name to set | |
*/ | |
public void setName(String name) { | |
this.name = name; | |
} | |
/** | |
* @return the childMenu | |
*/ | |
public List<Menu> getChildMenu() { | |
return childMenu; | |
} | |
/** | |
* @param childMenu | |
* the childMenu to set | |
*/ | |
public void setChildMenu(List<Menu> childMenu) { | |
this.childMenu = childMenu; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package menu.test; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class MenuService { | |
public static void main(String[] args) { | |
List<Menu> Listmenu = (List<Menu>) getMenu(); | |
System.out.println("UnOrdered Menu : "); | |
for (Menu m : Listmenu) { | |
System.out.println(m.getName()); | |
} | |
Menu myMenu = new Menu(); | |
Map<Long, Menu> parentMenu = new HashMap<>(); | |
for (Menu menuOri : Listmenu) { | |
populateMenu(menuOri, myMenu, parentMenu); | |
} | |
System.out.println("Ordered Menu : "); | |
showPopulatedMenu(parentMenu); | |
} | |
private static void populateMenu(Menu menuOri, Menu myMenu, Map<Long, Menu> parentMenu) { | |
if (menuOri.getParentid() == null) { | |
// check if parent has exist in map, if exist dont put it again | |
if (parentMenu.get(menuOri.getId()) == null) { | |
// get parent | |
myMenu = new Menu(); | |
myMenu.setId(menuOri.getId()); | |
myMenu.setName(menuOri.getName()); | |
parentMenu.put(myMenu.getId(), myMenu); | |
} | |
} else { | |
// get child | |
getChild(menuOri, myMenu, parentMenu); | |
} | |
// populateMenu(menuOri, myMenu, parentMenu); | |
} | |
private static void getChild(Menu menuOri, Menu myMenu, Map<Long, Menu> parentMenu) { | |
// check if parent menu is exist in map | |
if (parentMenu.get(menuOri.getParentid()) != null) { | |
Menu childMenu = new Menu(); | |
// get its parent | |
Menu parentMenuTemp = parentMenu.get(menuOri.getParentid()); | |
// if childMenu doesnt exist | |
if (parentMenuTemp.getChildMenu() == null) { | |
List<Menu> childMenuList = new ArrayList<>(); | |
// set up the child | |
childMenu.setId(menuOri.getId()); | |
childMenu.setName(menuOri.getName()); | |
childMenu.setParentid(menuOri.getParentid()); | |
childMenuList.add(childMenu); | |
// insert to its parent | |
parentMenuTemp.setChildMenu(childMenuList); | |
} else { | |
// if there is/are another childMenu does exist | |
List<Menu> childMenuList = parentMenuTemp.getChildMenu(); | |
// set up the child | |
childMenu.setId(menuOri.getId()); | |
childMenu.setName(menuOri.getName()); | |
childMenu.setParentid(menuOri.getParentid()); | |
childMenuList.add(childMenu); | |
// insert to its parent | |
parentMenuTemp.setChildMenu(childMenuList); | |
} | |
// put it again the parent with new added child | |
parentMenu.put(parentMenuTemp.getId(), parentMenuTemp); | |
} | |
} | |
private static void showPopulatedMenu(Map<Long, Menu> parentMenu) { | |
for (Map.Entry<Long, Menu> parentMap : parentMenu.entrySet()) { | |
Menu parent = parentMap.getValue(); | |
System.out.println("(+) " + parent.getName()); | |
if (parent.getChildMenu() != null) { | |
for (Menu child : parent.getChildMenu()) { | |
System.out.println("--- " + child.getName()); | |
} | |
} | |
} | |
} | |
private static List<Menu> getMenu() { | |
List<Menu> list = new ArrayList<>(); | |
Menu m = new Menu(); | |
m.setId(1l); | |
m.setName("Elektronik"); | |
list.add(m); | |
m = new Menu(); | |
m.setId(2l); | |
m.setName("kulkas"); | |
m.setParentid(1l); | |
list.add(m); | |
m = new Menu(); | |
m.setId(3l); | |
m.setName("rice cooker"); | |
m.setParentid(1l); | |
list.add(m); | |
m = new Menu(); | |
m.setId(4l); | |
m.setName("Pakaian"); | |
list.add(m); | |
m = new Menu(); | |
m.setId(5l); | |
m.setName("T-shirt"); | |
m.setParentid(4l); | |
list.add(m); | |
m = new Menu(); | |
m.setId(6l); | |
m.setName("Celana"); | |
m.setParentid(4l); | |
list.add(m); | |
m = new Menu(); | |
m.setId(7l); | |
m.setName("Gadget"); | |
list.add(m); | |
m = new Menu(); | |
m.setId(8l); | |
m.setName("samsung"); | |
m.setParentid(7l); | |
list.add(m); | |
m = new Menu(); | |
m.setId(8l); | |
m.setName("apple"); | |
m.setParentid(7l); | |
list.add(m); | |
m = new Menu(); | |
m.setId(9l); | |
m.setName("Dvd player"); | |
m.setParentid(1l); | |
list.add(m); | |
m = new Menu(); | |
m.setId(8l); | |
m.setName("htc"); | |
m.setParentid(7l); | |
list.add(m); | |
m = new Menu(); | |
m.setId(6l); | |
m.setName("jaket"); | |
m.setParentid(4l); | |
list.add(m); | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
menu parent and child
membuat menu, yang terdapat child and parent menu