Skip to Content

I wrote this code for answer of this question :

http://scn.sap.com/thread/3390691

I inspired from Steve Rumsby blog : http://scn.sap.com/community/abap/blog/2013/04/23/object-oriented-sieve-of-eratosthenes

Here is my code… If you see any misuse of OO or better way to do this, plase let me know.

DATA : BEGIN OF ls_bom,
         stufe TYPE i,
         level TYPE char20,
        END OF ls_bom,
        it_bom LIKE STANDARD TABLE OF ls_bom.
 *----------------------------------------------------------------------*
 *       CLASS lcl_determine_level DEFINITION
 *----------------------------------------------------------------------*
 *
 *----------------------------------------------------------------------*
 CLASS lcl_determine_level DEFINITION FINAL.
   PUBLIC SECTION.
     METHODS : constructor
                IMPORTING
                  i_stufe TYPE i.
     METHODS : get_level
                IMPORTING
                  i_stufe TYPE i
                RETURNING value(r_level) TYPE char20,
                get_level_chr
                 RETURNING value(r_level) TYPE char20.
   PROTECTED SECTION.
     DATA : level TYPE REF TO lcl_determine_level.
   PRIVATE SECTION.
     DATA : value TYPE i,
            lvl TYPE i,
            level_chr TYPE char20.
 ENDCLASS.                    "lcl_determine_level DEFINITION
 *----------------------------------------------------------------------*
 *       CLASS lcl_determine_level IMPLEMENTATION
 *----------------------------------------------------------------------*
 *
 *----------------------------------------------------------------------*
 CLASS lcl_determine_level IMPLEMENTATION.
   METHOD constructor.
     super->constructor( ).
     value = i_stufe.
   ENDMETHOD.                    "constructor
   METHOD get_level.
     IF value = i_stufe.
       ADD 1 TO lvl.
       WRITE : lvl TO level_chr.
       CONDENSE level_chr.
       r_level = level_chr.
     ENDIF.
   ENDMETHOD.                    "get_level
   METHOD get_level_chr.
     r_level = level_chr.
   ENDMETHOD.                    "get_level_chr
 ENDCLASS.                    "lcl_determine_level IMPLEMENTATION
 *----------------------------------------------------------------------*
 *       CLASS lcl_bom DEFINITION
 *----------------------------------------------------------------------*
 *
 *----------------------------------------------------------------------*
 CLASS lcl_bom DEFINITION.
   PUBLIC SECTION.
     METHODS: constructor
               IMPORTING
                 i_stufe TYPE i,
               get_level
                 IMPORTING
                   i_stufe TYPE i
                 RETURNING value(r_level) TYPE char20.
   PRIVATE SECTION.
     DATA: level TYPE REF TO lcl_determine_level,
           bom TYPE REF TO lcl_bom.
 ENDCLASS.                    "lcl_bom DEFINITION
 *----------------------------------------------------------------------*
 *       CLASS lcl_bom IMPLEMENTATION
 *----------------------------------------------------------------------*
 *
 *----------------------------------------------------------------------*
 CLASS lcl_bom IMPLEMENTATION.
   METHOD constructor.
     DATA : lo_level TYPE REF TO lcl_determine_level.
     CREATE OBJECT lo_level
       EXPORTING
         i_stufe = i_stufe.
     level = lo_level.
   ENDMETHOD.                    "constructor
   METHOD get_level.
     DATA : level_chr TYPE char20.
     r_level = level->get_level( i_stufe ).
     IF r_level EQ 0.
       level_chr = level->get_level_chr( ).
       IF bom IS NOT BOUND.
         CREATE OBJECT bom
           EXPORTING
             i_stufe = i_stufe.
       ENDIF.
       r_level = bom->get_level( i_stufe ).
       CONCATENATE level_chr
                   '.'
                   r_level
              INTO r_level.
       CONDENSE r_level.
     ELSE.
       FREE bom.
     ENDIF.
   ENDMETHOD.                    "geT_level
 ENDCLASS.                    "lcl_bom IMPLEMENTATION
 FIELD-SYMBOLS : <wa> LIKE LINE OF it_bom.
 DATA : lo_bom TYPE REF TO lcl_bom.
 START-OF-SELECTION.
   ls_bom-stufe = 1.
   APPEND ls_bom
       TO it_bom.
   ls_bom-stufe = 2.
   APPEND ls_bom
       TO it_bom.
   ls_bom-stufe = 2.
   APPEND ls_bom
       TO it_bom.
   ls_bom-stufe = 3.
   APPEND ls_bom
     TO it_bom.
   ls_bom-stufe = 3.
   APPEND ls_bom
       TO it_bom.
   ls_bom-stufe = 4.
   APPEND ls_bom
       TO it_bom.
   ls_bom-stufe = 4.
   APPEND ls_bom
       TO it_bom.
   ls_bom-stufe = 2.
   APPEND ls_bom
       TO it_bom.
   ls_bom-stufe = 2.
   APPEND ls_bom
       TO it_bom.
   ls_bom-stufe = 3.
   APPEND ls_bom
       TO it_bom.
   ls_bom-stufe = 4.
   APPEND ls_bom
       TO it_bom.
   ls_bom-stufe = 4.
   APPEND ls_bom
       TO it_bom.
   ls_bom-stufe = 4.
   APPEND ls_bom
       TO it_bom.
   ls_bom-stufe = 5.
   APPEND ls_bom
       TO it_bom.
   ls_bom-stufe = 5.
   APPEND ls_bom
       TO it_bom.
   BREAK-POINT.
   DATA : lv_level TYPE i.
   LOOP AT it_bom
      ASSIGNING <wa>.
     IF lo_bom IS NOT BOUND.
       CREATE OBJECT lo_bom
         EXPORTING
           i_stufe = <wa>-stufe.
     ENDIF.
     <wa>-level = lo_bom->get_level( <wa>-stufe ).
   ENDLOOP.
   BREAK-POINT.

this is the result :

Level.JPG

Regards

Tolga POLAT

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply