We are going to Calculator in SAP using New ABAP Syntax. SAP HANA is Hot. Fiori/UI5 is Cool. OData is Captivating. Yet, in every one of these tomfoolery, fundamental ABAP is as yet flourishing. This article is an illustration of the fundamental ABCs of ABAP which we really want till ABAP doesn’t get wiped out (which won’t ever occur) later on SAP World. Darwin’s law of Advancement holds great to ABAPers also. ABAPers need to advance and get new deceives of the game in their kitty, to remain ahead in the opposition. In this article, I have attempted to show case a portion of the New Language structures of ABAP, which makes programming more straightforward and diminishes the code impressions. Calculator in SAP using New ABAP Syntax.
The setting behind this article: Calculator in SAP using New ABAP Syntax!
Question: The outcome ought to be imprinted in Outcome box rather than the Info text. For instance, for Information ‘2+3’ I want the Outcome ‘5’ to be imprinted in outcome box. What I have done is, doled out the Info field ‘IN’ to Result field ‘RES’.
Reply from @Anask (in few seconds or less. That the excellence of this gathering):
It regards the variable as string and simply duplicates content of one field to another. You want to part at SIGN and get information into 2 separate fields and afterward do the administrator activity. Simply utilize Split and afterward figure it out activity. Use CASE articulation and split in light of the sign it contains.
Sample Code:
IF input CS "+". lv_operator = "+". ELSEIF input CS "-". lv_operator = "+". ELSEIF input CS "*". lv_operator = "*". ELSEIF input CS "/". lv_operator = "/". ENDIF.
SPLIT lv_input AT lv_operator INTO lv_operand1 lv_operand2.
I’m individuals from numerous WhatsApp and Wire and different gatherings. However, this SAP Specialized Gathering is the most dynamic one where we have lots of conversations consistently. Most Inquiries get Addressed or if nothing else get a few thoughts for the arrangement. In the event that you have not joined at this point, check it out. It is completely safe. Furthermore, nobody can realize your portable number, not even the Administrators. You really want to have Wire Application introduced in your gadget before you join the gathering utilizing the beneath connect.
All things considered,I’m a SAP ABAP Designer. I’m the pioneer behind Köster Counseling. If it’s not too much trouble, actually look at my site for more data.
Enough of me. Presently, lets return to the top story. On the off chance that you are a beginner of ABAP7.4, a portion of the focuses which you really want to note in the underneath SAP Mini-computer Program are:
1. Inline Data Declaration
DATA(lv_off) = sy-index - 1. DATA(lv_add) = abap_true. READ TABLE gt_string_tab ASSIGNING FIELD-SYMBOL(<lv_op>) WITH TABLE KEY table_line = '/'.
2. RAISE exceptions
RAISE missing_number_at_beginning. RAISE two_operator_not_allowed. RAISE division_by_zero. RAISE missing_number_at_end. RAISE unknown_error.
(Nothing new. Showing for beginners in ABAP)
3. Conversion Function
DATA(lv_result) = CONV labst( <lv_first> / <lv_second> ). rv_result = CONV #( gt_string_tab[ 1 ] ). DATA(gv_fieldname) = |RB_CALC{ CONV numc1( sy-index ) }|. cv_editable = |PA_CALC{ CONV numc1( sy-index ) }|.
4. New Syntax
INSERT |{ lv_result }| INTO gt_string_tab INDEX lv_tabix - 1.
5. Inline Data Declaration in Class Methods
* Call the Class Method to do the calculation
zcl_stkoes_calc=>do_calculation( EXPORTING iv_formula = <gv_calc> RECEIVING rv_result = DATA(gv_result) EXCEPTIONS division_by_zero = 1 " Division By Zero missing_number_at_beginning = 2 " Missing Number at the beginning missing_number_at_end = 3 " Missing Number at the end two_operator_not_allowed = 4 " Two Operator are not allowed unknown_error = 5 " Unknown Error OTHERS = 6 ).
Did you check gv_result is announced during the call of the technique do_calculation.
Note: We Can’t do comparable Inline Information Statement while calling a Capability Module. Just accessible in Class Technique.
6. MESSAGE with SWITCH
MESSAGE |{ SWITCH #( sy-subrc WHEN 1 THEN |Division by zero| WHEN 2 THEN |Missing number at the beginning| WHEN 3 THEN |Missing number at the end| WHEN 4 THEN |Two operator is not allowed| WHEN 5 THEN |Unknown Error| ELSE |Other Error| ) }| TYPE 'S' DISPLAY LIKE 'E'.
Allow me to show you, what the Adding machine can do. It does the essential +, – , X,/mathematic activities. Be that as it may, the rationale and idea which I have placed in the class can be extrapolated to many genuine use case real venture prerequisites.
I have purposely utilized the Macros to cause you to comprehend how Macros can in any case be utilized. Assuming you feel, macros are not required, you can do the immediate Radio Buttons in the Choice Screen without the Macros.
Sample Input String for the Calculator and their corresponding Results
Sample Error Handling
Here is the finished Code which you can attempt. You can likewise download the program text document at the lower part of this article.
*&---------------------------------------------------------------------* *& Report ZSTKOES_CALC *&---------------------------------------------------------------------* *& This is a Utilty Program which acts like a Simple Calculator. *& OOPs Concept along with New ABAP7.4+ Syntaxes are used *& Feel Free to refer and use it as required *&---------------------------------------------------------------------* REPORT zstkoes_calc. CLASS zcl_stkoes_calc DEFINITION. PUBLIC SECTION. CONSTANTS: BEGIN OF gcs_operators, div TYPE char1 VALUE '/' ##NO_TEXT, mod TYPE char3 VALUE 'MOD' ##NO_TEXT, mul TYPE char1 VALUE '*' ##NO_TEXT, sub TYPE char1 VALUE '-' ##NO_TEXT, add TYPE char1 VALUE '+' ##NO_TEXT, END OF gcs_operators. CLASS-METHODS: do_calculation IMPORTING VALUE(iv_formula) TYPE string RETURNING VALUE(rv_result) TYPE labst EXCEPTIONS division_by_zero missing_number_at_beginning missing_number_at_end two_operator_not_allowed unknown_error. PRIVATE SECTION. TYPES: gtty_string TYPE TABLE OF string. CLASS-METHODS: convert_formula_to_string_tab IMPORTING VALUE(iv_formula) TYPE string EXPORTING VALUE(et_string_tab) TYPE gtty_string EXCEPTIONS missing_number_at_beginning missing_number_at_end two_operator_not_allowed, calculate IMPORTING VALUE(it_string_tab) TYPE gtty_string RETURNING VALUE(rv_result) TYPE labst EXCEPTIONS division_by_zero unknown_error. ENDCLASS. CLASS zcl_stkoes_calc IMPLEMENTATION. METHOD do_calculation. convert_formula_to_string_tab( EXPORTING iv_formula = iv_formula IMPORTING et_string_tab = DATA(lt_string_tab) EXCEPTIONS missing_number_at_beginning = 1 missing_number_at_end = 2 two_operator_not_allowed = 3 ). IF sy-subrc EQ 0. calculate( EXPORTING it_string_tab = lt_string_tab RECEIVING rv_result = rv_result EXCEPTIONS division_by_zero = 4 unknown_error = 5 ). ENDIF. IF sy-subrc NE 0. CASE sy-subrc. WHEN 1. RAISE missing_number_at_beginning. WHEN 2. RAISE missing_number_at_end. WHEN 3. RAISE two_operator_not_allowed. WHEN 4. RAISE division_by_zero. WHEN 5. RAISE unknown_error. ENDCASE. ENDIF. ENDMETHOD. METHOD convert_formula_to_string_tab. FIELD-SYMBOLS: <lv_value> TYPE string. CONDENSE iv_formula NO-GAPS. DATA(lv_off) = 0. DO. IF iv_formula+lv_off(1) CN '1234567890'. DO. ASSIGN COMPONENT sy-index OF STRUCTURE gcs_operators TO FIELD-SYMBOL(<lv_operator>). IF sy-subrc NE 0. EXIT. ENDIF. DATA(lv_length) = strlen( <lv_operator> ). IF iv_formula+lv_off(lv_length) EQ <lv_operator>. IF lv_off EQ 0. RAISE missing_number_at_beginning. ELSEIF <lv_value> IS NOT ASSIGNED. RAISE two_operator_not_allowed. ENDIF. UNASSIGN <lv_value>. APPEND iv_formula+lv_off(lv_length) TO et_string_tab. ADD lv_length TO lv_off. EXIT. ENDIF. ENDDO. ELSE. IF <lv_value> IS NOT ASSIGNED. APPEND INITIAL LINE TO et_string_tab ASSIGNING <lv_value>. <lv_value> = iv_formula+lv_off(1). ELSE. <lv_value> = |{ <lv_value> }{ iv_formula+lv_off(1) }|. ENDIF. ADD 1 TO lv_off. ENDIF. IF lv_off EQ strlen( iv_formula ). EXIT. ENDIF. ENDDO. IF <lv_value> IS NOT ASSIGNED. RAISE missing_number_at_end. ENDIF. ENDMETHOD. METHOD calculate. DO. ASSIGN COMPONENT sy-index OF STRUCTURE gcs_operators TO FIELD-SYMBOL(<lv_operator>). IF sy-subrc NE 0. EXIT. ENDIF. DO. ASSIGN it_string_tab[ table_line = <lv_operator> ] TO FIELD-SYMBOL(<lv_op>). IF sy-subrc NE 0. EXIT. ENDIF. DATA(lv_from) = sy-tabix - 1. DATA(lv_to) = sy-tabix + 1. READ TABLE it_string_tab ASSIGNING FIELD-SYMBOL(<lv_first>) INDEX lv_from. READ TABLE it_string_tab ASSIGNING FIELD-SYMBOL(<lv_second>) INDEX lv_to. IF <lv_first> IS ASSIGNED AND <lv_second> IS ASSIGNED. TRY. CASE <lv_operator>. WHEN '/'. DATA(lv_result) = CONV labst( <lv_first> / <lv_second> ). WHEN 'MOD'. lv_result = <lv_first> MOD <lv_second>. WHEN '*'. lv_result = <lv_first> * <lv_second>. WHEN '-'. lv_result = <lv_first> - <lv_second>. WHEN '+'. lv_result = <lv_first> + <lv_second>. ENDCASE. CATCH cx_sy_zerodivide INTO DATA(lo_error). RAISE division_by_zero. ENDTRY. DELETE it_string_tab FROM lv_from TO lv_to. INSERT |{ lv_result }| INTO it_string_tab INDEX lv_from. ENDIF. ENDDO. ENDDO. IF lines( it_string_tab ) EQ 1. rv_result = it_string_tab[ 1 ]. ELSE. RAISE unknown_error. ENDIF. ENDMETHOD. ENDCLASS. **--------------------------------------------------------------------* ** Start of Program **--------------------------------------------------------------------* * Macro to set one radiobutton as default (can be used only once) DEFINE calc_default. SELECTION-SCREEN: BEGIN OF LINE. PARAMETERS: rb_calc&1 RADIOBUTTON GROUP cal DEFAULT 'X' USER-COMMAND calc&1. SELECTION-SCREEN: COMMENT 3(14) co_calc&1. PARAMETERS: pa_calc&1 TYPE string DEFAULT &2. SELECTION-SCREEN: END OF LINE. END-OF-DEFINITION. * Macro to create radiobutton without deafult DEFINE calc. SELECTION-SCREEN: BEGIN OF LINE. PARAMETERS: rb_calc&1 RADIOBUTTON GROUP cal. SELECTION-SCREEN: COMMENT 3(14) co_calc&1. PARAMETERS: pa_calc&1 TYPE string DEFAULT &2. SELECTION-SCREEN: END OF LINE. END-OF-DEFINITION. SELECTION-SCREEN BEGIN OF BLOCK b01 WITH FRAME TITLE gt_b01. * Creating all paramater on SELECTION SCREEN using macro calc_default 1 '10 + 300 / 100 * 10 - 50'. calc 2 '+10 + 300 / 100 * 10 - 50'. calc 3 '10 + 300 / 100 * 10 - 50+'. calc 4 '10 + 300 / 100 * * 10 - 50'. calc 5 '10 + 300 / 0 * 10 - 50'. SELECTION-SCREEN END OF BLOCK b01. INITIALIZATION. * Filling all comments and title co_calc1 = '1. Calculation'. co_calc2 = '2. Calculation'. co_calc3 = '3. Calculation'. co_calc4 = '4. Calculation'. co_calc5 = '5. Calculation'. gt_b01 = 'Calculations'. AT SELECTION-SCREEN OUTPUT. DATA: gv_editable TYPE rsscr_name. * Check which radiobutton is selected PERFORM get_field_selected_radiobutton CHANGING gv_editable. * Leave only selected line editable LOOP AT SCREEN. IF screen-name(7) EQ 'PA_CALC' AND screen-name NE gv_editable. screen-input = 0. MODIFY SCREEN. ENDIF. ENDLOOP. START-OF-SELECTION. DATA: gv_editable TYPE rsscr_name. * Check which radiobutton is selected PERFORM get_field_selected_radiobutton CHANGING gv_editable. * Get the Formular of selected radiobutton ASSIGN (gv_editable) TO FIELD-SYMBOL(<gv_calc>). IF <gv_calc> IS ASSIGNED. * Do the calculation zcl_stkoes_calc=>do_calculation( EXPORTING iv_formula = <gv_calc> RECEIVING rv_result = DATA(gv_result) EXCEPTIONS division_by_zero = 1 " Division By Zero missing_number_at_beginning = 2 " Missing Number at the beginning missing_number_at_end = 3 " Missing Number at the end two_operator_not_allowed = 4 " Two Operator are not allowed unknown_error = 5 " Unknown Error OTHERS = 6 ). * Handle Errors IF sy-subrc NE 0. MESSAGE |{ SWITCH #( sy-subrc WHEN 1 THEN |Division by zero| WHEN 2 THEN |Missing number at the beginning| WHEN 3 THEN |Missing number at the end| WHEN 4 THEN |Two operator is not allowed| WHEN 5 THEN |Unknown Error| ELSE |Other Error| ) }| TYPE 'S' DISPLAY LIKE 'E'. RETURN. ENDIF. ENDIF. END-OF-SELECTION. WRITE: gv_result. *&---------------------------------------------------------------------* *& Form GET_FIELD_SELECTED_RADIOBUTTON *&---------------------------------------------------------------------* * <--CV_EDITABLE PARAMETER of selected radiobutton *----------------------------------------------------------------------* FORM get_field_selected_radiobutton CHANGING cv_editable TYPE rsscr_name. DO. DATA(lv_fieldname) = |RB_CALC{ CONV numc1( sy-index ) }|. ASSIGN (lv_fieldname) TO FIELD-SYMBOL(<lv_fieldvalue>). IF sy-subrc NE 0. EXIT. ELSEIF <lv_fieldvalue> EQ abap_true. cv_editable = |PA_CALC{ CONV numc1( sy-index ) }|. EXIT. ENDIF. ENDDO. ENDFORM.
Download above program in text record. Calculator Version 1
To involve Macros for the Radio Buttons, you can download one more adaptation of a similar program with less difficult Choice screen. Calculator Version 2
Output of Version 2 of the SAP ABAP Calculator looks like below.
I trust, this article would rouse you to involve the New ABAP Linguistic structure in the entirety of your current and future turns of events. We really want to embrace the change and acknowledge it in our everyday undertakings. Change is Progress.
YOU MAY BE INTERESTED IN
OData in SAP ABAP: Streamlining Data Exchange and Integration