Quantcast
Viewing all articles
Browse latest Browse all 9164

Re: how to display alphabetic pyramid in ABAP

Here's the class:

 

CLASS ZCL_ALPHABETIC_PYRAMID DEFINITION

  PUBLIC

  FINAL

  CREATE PUBLIC .

 

 

  PUBLIC SECTION.

    METHODS: PYRAMID IMPORTING IV_LETTER TYPE CHAR1

                     RETURNING VALUE(RT_STRINGS) TYPE ZTTY_STRING.

  PROTECTED SECTION.

  PRIVATE SECTION.

 

 

ENDCLASS.

 

 

CLASS ZCL_ALPHABETIC_PYRAMID IMPLEMENTATION.

  METHOD PYRAMID.

    DATA: LV_CNT TYPE I,

          LV_COUNTER TYPE I,

          LV_SPACE_COUNTER TYPE I,

          LV_STRING TYPE STRING.

 

 

    FIND FIRST OCCURRENCE OF IV_LETTER IN SY-ABCDE MATCH OFFSET LV_CNT.

    LV_SPACE_COUNTER = LV_CNT + 1.

    DO LV_CNT + 1 TIMES.

      CLEAR: LV_COUNTER, LV_STRING.

      WHILE LV_COUNTER LE LV_CNT.

        IF LV_COUNTER GE LV_SPACE_COUNTER.

          CONCATENATE LV_STRING SPACE SPACE INTO LV_STRING RESPECTING BLANKS.

        ELSE.

          CONCATENATE LV_STRING SY-ABCDE+LV_COUNTER(1) SPACE INTO LV_STRING RESPECTING BLANKS.

        ENDIF.

        ADD 1 TO LV_COUNTER.

      ENDWHILE.

      LV_COUNTER = LV_CNT - 1.

      WHILE LV_COUNTER GE 0.

        IF LV_COUNTER GE LV_SPACE_COUNTER.

          CONCATENATE LV_STRING SPACE SPACE INTO LV_STRING RESPECTING BLANKS.

        ELSE.

          CONCATENATE LV_STRING SY-ABCDE+LV_COUNTER(1) SPACE INTO LV_STRING RESPECTING BLANKS.

        ENDIF.

        SUBTRACT 1 FROM LV_COUNTER.

      ENDWHILE.

      SUBTRACT 1 FROM LV_SPACE_COUNTER.

      APPEND LV_STRING TO RT_STRINGS.

    ENDDO.

  ENDMETHOD.

ENDCLASS.

 

Here's the test class:

*"* use this source file for your ABAP unit test classes

CLASS ABAPUNIT_TESTCLASS DEFINITION FINAL FOR TESTING

  DURATION SHORT

  RISK LEVEL HARMLESS

  INHERITING FROM CL_AUNIT_ASSERT.

 

 

  PRIVATE SECTION.

    METHODS:

      SETUP,

      TEST_A FOR TESTING,

      TEST_B FOR TESTING,

      TEST_C FOR TESTING,

      TEST_D FOR TESTING.

 

 

    DATA: M_REF TYPE REF TO ZCL_ALPHABETIC_PYRAMID.

ENDCLASS.

 

 

 

 

CLASS ABAPUNIT_TESTCLASS IMPLEMENTATION.

  METHOD SETUP.

    CREATE OBJECT M_REF.

  ENDMETHOD.

 

 

  METHOD TEST_A.

    DATA: LT_STRING TYPE ZTTY_STRING.

    FIELD-SYMBOLS: <LINE> TYPE CHAR255.

    LT_STRING = M_REF->PYRAMID( 'A' ).

    ASSERT_EQUALS( ACT = LINES( LT_STRING ) EXP = 1 ).

    READ TABLE LT_STRING INDEX 1 ASSIGNING <LINE>.

    ASSERT_EQUALS( ACT = <LINE> EXP = 'A' ).

  ENDMETHOD.

 

 

  METHOD TEST_B.

    DATA: LT_STRING TYPE ZTTY_STRING.

    FIELD-SYMBOLS: <LINE> TYPE CHAR255.

    LT_STRING = M_REF->PYRAMID( 'B' ).

    ASSERT_EQUALS( ACT = LINES( LT_STRING ) EXP = 2 ).

    READ TABLE LT_STRING INDEX 1 ASSIGNING <LINE>.

    ASSERT_EQUALS( ACT = <LINE> EXP = 'A B A' ).

    READ TABLE LT_STRING INDEX 2 ASSIGNING <LINE>.

    ASSERT_EQUALS( ACT = <LINE> EXP = 'A   A' ).

  ENDMETHOD.

 

 

  METHOD TEST_C.

    DATA: LT_STRING TYPE ZTTY_STRING.

    FIELD-SYMBOLS: <LINE> TYPE CHAR255.

    LT_STRING = M_REF->PYRAMID( 'C' ).

    ASSERT_EQUALS( ACT = LINES( LT_STRING ) EXP = 3 ).

    READ TABLE LT_STRING INDEX 1 ASSIGNING <LINE>.

    ASSERT_EQUALS( ACT = <LINE> EXP = 'A B C B A' ).

    READ TABLE LT_STRING INDEX 2 ASSIGNING <LINE>.

    ASSERT_EQUALS( ACT = <LINE> EXP = 'A B   B A' ).

    READ TABLE LT_STRING INDEX 3 ASSIGNING <LINE>.

    ASSERT_EQUALS( ACT = <LINE> EXP = 'A       A' ).

  ENDMETHOD.

  METHOD TEST_D.

    DATA: LT_STRING TYPE ZTTY_STRING.

    FIELD-SYMBOLS: <LINE> TYPE CHAR255.

    LT_STRING = M_REF->PYRAMID( 'D' ).

    ASSERT_EQUALS( ACT = LINES( LT_STRING ) EXP = 4 ).

    READ TABLE LT_STRING INDEX 1 ASSIGNING <LINE>.

    ASSERT_EQUALS( ACT = <LINE> EXP = 'A B C D C B A' ).

    READ TABLE LT_STRING INDEX 2 ASSIGNING <LINE>.

    ASSERT_EQUALS( ACT = <LINE> EXP = 'A B C   C B A' ).

    READ TABLE LT_STRING INDEX 3 ASSIGNING <LINE>.

    ASSERT_EQUALS( ACT = <LINE> EXP = 'A B       B A' ).

    READ TABLE LT_STRING INDEX 4 ASSIGNING <LINE>.

    ASSERT_EQUALS( ACT = <LINE> EXP = 'A           A' ).

  ENDMETHOD.

ENDCLASS.

 

 

And here's a runner program to test with your input:

REPORT  ZTEST_PYRAMID.

 

 

DATA: O_PYRAMID TYPE REF TO ZCL_ALPHABETIC_PYRAMID,

      LT_STRING TYPE ZTTY_STRING.

 

 

FIELD-SYMBOLS: <LINE> TYPE CHAR255.

 

 

CREATE OBJECT O_PYRAMID.

LT_STRING = O_PYRAMID->PYRAMID( 'I' ).

 

 

LOOP AT LT_STRING ASSIGNING <LINE>.

  WRITE: / <LINE>.

ENDLOOP.

 

 

 

 

Image may be NSFW.
Clik here to view.
Capture.PNG


Viewing all articles
Browse latest Browse all 9164

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>