2013-06-28 2 views
-2

개체에 문제가 있습니다. 사실 나는 다른 객체로 객체를 변환해야합니다. 여기 내 범주 개체에는 범주 (하위 범주) 목록이 들어 있습니다. 이 코드의 주요 문제점은 모든 하위 카테고리 (Category 객체)를 CatgoryUI로 변환해야한다는 것입니다.개체 목록을 포함하여 개체를 다른 형식으로 변환하십시오.

import java.util.ArrayList; 
import java.util.List; 

public class TestMain { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     System.out.println("In main"); 
     TestMain tm = new TestMain(); 
     List<Category> categories= tm.prepareList(); 
     tm.displayCategories(categories); 
     List<CategoryUI> categoryList = tm.convertCategories(categories); 
     System.out.println("------Converted------"); 
     tm.displayConvertedCategories(categoryList); 
    } 

    private List<Category> prepareList(){ 
     //Category category = new Category(); 
     List<Category> level3List1 = new ArrayList<Category>(); 

     level3List1.add(new Category(4L, "Sentence Equalence", new ArrayList<Category>())); 
     level3List1.add(new Category(5L, "Antonyms", new ArrayList<Category>())); 
     level3List1.add(new Category(6L, "Text Completion", new ArrayList<Category>())); 

     List<Category> level3List2 = new ArrayList<Category>(); 
     level3List2.add(new Category(7L, "Problem Solving", new ArrayList<Category>())); 
     level3List2.add(new Category(8L, "Logical Reasoning", new ArrayList<Category>())); 

     List<Category> level2List = new ArrayList<Category>(); 
     level2List.add(new Category(2L, "Verbal", level3List1)); 
     level2List.add(new Category(3L, "Quantative", level3List2)); 

     List<Category> level1List = new ArrayList<Category>(); 
     level1List.add(new Category(1L, "GRE", level2List)); 

     return level1List; 

    } 

    private void displayCategories(List<Category> categories){ 
     System.out.println("<ul>"); 
     for(Category category : categories){ 
      System.out.println("<li>"); 
      System.out.println(category.getName()); 
      System.out.println("</li>"); 
      if(category.getSubCategory().size()>0){ 
       displayCategories(category.getSubCategory()); 
      } 
     } 
     System.out.println("</ul>"); 
    } 
} 

package net.sankhya.debug; 

import java.util.List; 

public class Category { 

    private Long id; 
    private String name; 
    private List<Category> subCategory; 



    public Category(Long id, String name, List<Category> subCategory) { 
     super(); 
     this.id = id; 
     this.name = name; 
     this.subCategory = subCategory; 
    } 
    public Long getId() { 
     return id; 
    } 
    public void setId(Long id) { 
     this.id = id; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public List<Category> getSubCategory() { 
     return subCategory; 
    } 
    public void setSubCategory(List<Category> subCategory) { 
     this.subCategory = subCategory; 
    } 
} 

package net.sankhya.debug; 

import java.util.List; 

public class CategoryUI { 

    private Long id; 
    private String name; 
    private List<CategoryUI> subCategory; 

    public CategoryUI(){ 

    } 

    public CategoryUI(Long id, String name, List<CategoryUI> subCategory) { 
     super(); 
     this.id = id; 
     this.name = name; 
     this.subCategory = subCategory; 
    } 
    public Long getId() { 
     return id; 
    } 
    public void setId(Long id) { 
     this.id = id; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public List<CategoryUI> getSubCategory() { 
     return subCategory; 
    } 
    public void setSubCategory(List<CategoryUI> subCategory) { 
     this.subCategory = subCategory; 
    } 
} 

ul 및 li의 카테고리 목록을 html보기로 표시하면 다음과 같이 표시됩니다. CategoryUI 객체로 변환 한 후 목록을 인쇄하면 동일한 방식으로 재생됩니다. 카테고리를 CategoryUI로 변환하는 방법에 대한 제안을 해주십시오.

<ul> 
<li>GRE</li> 
<ul> 
    <li>Verbal</li> 
    <ul> 
     <li>Sentence Equalence</li> 
     <li>Antonyms</li> 
     <li>Text Completion</li> 
    </ul> 
    <li>Quantative</li> 
    <ul> 
     <li>Problem Solving</li> 
     <li>Logical Reasoning</li> 
    </ul> 
</ul> 

감사

+0

내 질문에 당신의 문제는 아무 상관 없지만 왜 CategoryUI와 Category는 같은 인터페이스를 구현하지 않습니까? –

+0

나는 @ C.Champagne에 동의한다. 올바른 접근법이다. 내 대답은 이것이 할 수 없다는 가정에 근거합니다. – wattostudios

답변

1

public List<CategoryUI> convertArray(List<Category> categoryArray){ 
    List<CategoryUI> convertedArray = new ArrayList<CategoryUI>(); 

    for (int i=0;i<categoryArray.size();i++){ 
     convertedArray.add(convertObject(categoryArray.get(i))); 
    } 

    return convertedArray; 
} 


public CategoryUI convertObject(Category category){ 
    return new CategoryUI(category.getId(),category.getName(),convertArray(category.getSubCategory())); 
} 

그냥 변환 할 각 개체에 대해 convertObject(category) 전화를 ..., 이런 식으로 뭔가를 자신의 변환 방법을 작성합니다. 올바른 유형의 새 오브젝트를 효과적으로 작성한 다음 이전 오브젝트의 모든 데이터로 채 웁니다. 또한 하위 범주의 배열을 변환합니다. 트리의 최상위 노드를 전달하면 한 번의 호출로 트리의 모든 하위 범주를 통해 작업하고 모든 내용을 변환하므로 멋지고 간단합니다.

1

더 자주 신청해야 할 경우 Dozer을 입력 할 수도 있습니다. 자동으로 매핑을 수행합니다.

관련 문제