Code: Alles auswählen.
CLASS zcl_market_location DEFINITION.
PUBLIC SECTION.
METHODS:
constructor,
calculate_check_digit
IMPORTING
iv_market_location TYPE clike
RETURNING
VALUE(rv_check_digit) TYPE string.
ENDCLASS.
CLASS zcl_market_location IMPLEMENTATION.
METHOD constructor.
ENDMETHOD.
METHOD calculate_check_digit.
DATA: lt_digits TYPE TABLE OF string,
lv_odd_sum TYPE i,
lv_even_sum TYPE i,
lv_total_sum TYPE i,
lv_check_digit TYPE i,
lv_char TYPE char1,
lv_position TYPE i.
SPLIT iv_market_location AT space INTO TABLE lt_digits.
LOOP AT lt_digits INTO lv_char.
lv_position = sy-tabix.
IF lv_position MOD 2 = 1.
lv_odd_sum = lv_odd_sum + lv_char.
ELSE.
lv_even_sum = lv_even_sum + lv_char.
ENDIF.
ENDLOOP.
lv_total_sum = lv_odd_sum + ( lv_even_sum * 2 ).
lv_check_digit = 10 - ( lv_total_sum MOD 10 ).
rv_check_digit = lv_check_digit.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(lo_market_location) = NEW zcl_market_location( ).
DATA(lv_market_location) = '4 1 3 7 3 5 5 9 2 4'.
DATA(lv_check_digit) = lo_market_location->calculate_check_digit(
iv_market_location = lv_market_location ).
WRITE: 'Prüfziffer für die Marktlokations-Id ', lv_market_location,
' ist ', lv_check_digit.
Critical Assertion Error: 'Even position test failed'
Critical Assertion Error: 'Large market location test failed'
Critical Assertion Error: 'Multiple of 10 test failed'
Critical Assertion Error: 'Odd position test failed'
Critical Assertion Error: 'All zeros test failed'
Code: Alles auswählen.
CLASS zcl_market_location_test DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION short.
PRIVATE SECTION.
METHODS:
calc_check_digit_odd for TESTING,
calc_check_digit_even for TESTING,
calc_check_digit_mult_of_10 for TESTING,
calc_check_digit_zero for TESTING,
calc_check_digit_large for TESTING.
ENDCLASS.
CLASS zcl_market_location_test IMPLEMENTATION.
METHOD calc_check_digit_odd.
DATA(lo_market_location) = NEW zcl_market_location( ).
DATA(lv_market_location) = '4 1 3 7 3 5 5 9 2 4'.
data(lv_check_digit) = lo_market_location->calc_check_digit(
iv_market_location = lv_market_location ).
cl_abap_unit_assert=>assert_equals(
act = lv_check_digit
exp = '1'
msg = 'Odd position test failed' ).
ENDMETHOD.
METHOD calc_check_digit_even.
DATA(lo_market_location) = NEW zcl_market_location( ).
DATA(lv_market_location) = '1 2 3 4 5 6 7 8 9 0'.
data(lv_check_digit) = lo_market_location->calc_check_digit(
iv_market_location = lv_market_location ).
cl_abap_unit_assert=>assert_equals(
act = lv_check_digit
exp = '9'
msg = 'Even position test failed' ).
ENDMETHOD.
METHOD calc_check_digit_mult_of_10.
DATA(lo_market_location) = NEW zcl_market_location( ).
DATA(lv_market_location) = '9 8 7 6 5 4 3 2 1 0 9'.
data(lv_check_digit) = lo_market_location->calc_check_digit(
iv_market_location = lv_market_location ).
cl_abap_unit_assert=>assert_equals(
act = lv_check_digit
exp = '0'
msg = 'Multiple of 10 test failed' ).
ENDMETHOD.
METHOD calc_check_digit_zero.
DATA(lo_market_location) = NEW zcl_market_location( ).
DATA(lv_market_location) = '0 0 0 0 0 0 0 0 0 0'.
data(lv_check_digit) = lo_market_location->calc_check_digit(
iv_market_location = lv_market_location ).
cl_abap_unit_assert=>assert_equals(
act = lv_check_digit
exp = '0'
msg = 'All zeros test failed' ).
ENDMETHOD.
METHOD calc_check_digit_large.
DATA(lo_market_location) = NEW zcl_market_location( ).
DATA(lv_market_location) = '1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0'.
data(lv_check_digit) = lo_market_location->calc_check_digit(
iv_market_location = lv_market_location ).
cl_abap_unit_assert=>assert_equals(
act = lv_check_digit
exp = '8'
msg = 'Large market location test failed' ).
ENDMETHOD.
ENDCLASS.