CLASS ztco_calculator DEFINITION FINAL.
PUBLIC SECTION.
TYPES: BEGIN OF tco_and_surcharges,
tco TYPE ztco,
surcharges_logistics TYPE zsurcharges_logistics,
process_costs TYPE zprocess_costs,
END OF tco_and_surcharges.
METHODS constructor
IMPORTING
purchase_order_item TYPE ekpo.
METHODS calculate_tco_with_surcharges
RETURNING VALUE(result) TYPE tco_and_surcharges.
PROTECTED SECTION.
DATA: purchase_order_item TYPE ekpo.
METHODS get_purchasing_price
RETURNING VALUE(result) TYPE zpurchase_price.
METHODS get_surcharges_logistics
RETURNING VALUE(result) TYPE zsurcharges_logistics.
METHODS get_process_costs
RETURNING VALUE(result) TYPE zprocess_costs.
ENDCLASS.
CLASS ztco_calculator IMPLEMENTATION.
METHOD constructor.
me->purchase_order_item = purchase_order_item.
ENDMETHOD.
METHOD calculate_tco_with_surcharges.
result-surcharges_logistics = get_surcharges_logistics( ).
result-process_costs = get_process_costs( ).
result-tco = get_purchasing_price( ) +
result-surcharges_logistics +
result-process_costs.
ENDMETHOD.
ENDCLASS.
get_surcharges_logistics
and get_process_costs
(process-costs were some kind of surcharges) in an interface, i could stub them in the unit-test.
INTERFACE ztco_surcharge_calculator.
METHODS get_surcharges_logistics
RETURNING VALUE(result) TYPE zsurcharges_logistics.
METHODS get_process_costs
RETURNING VALUE(result) TYPE zprocess_costs.
ENDINTERFACE.
CLASS ztco_calculator DEFINITION FINAL.
PUBLIC SECTION.
METHODS constructor
IMPORTING
surcharge_calculator TYPE REF TO ztco_surcharge_calculator
purchase_order_item TYPE ekpo.
METHODS calculate_tco_with_surcharges
RETURNING VALUE(result) TYPE tco_and_surcharges.
PROTECTED SECTION.
DATA: surcharge_calculator TYPE REF TO ztco_surcharge_calculator,
purchase_order_item TYPE ekpo.
METHODS get_purchasing_price
RETURNING VALUE(result) TYPE zpurchase_price.
ENDCLASS.
CLASS ztco_calculator IMPLEMENTATION.
METHOD constructor.
me->surcharge_calculator = surcharge_calculator.
me->purchase_order_item = purchase_order_item.
ENDMETHOD.
METHOD calculate_tco_with_surcharges.
result-surcharges_logistics = surcharge_calculator->get_surcharges_logistics( ).
result-process_costs = surcharge_calculator->get_process_costs( ).
result-tco = get_purchasing_price( ) +
result-surcharges_logistics +
result-process_costs.
ENDMETHOD.
ENDCLASS.
ztco_surcharge_calculator
. We had the following three classes:ztco_calculator
. Instead of we just used the interface ztco_surcharge_calculator
. This works with a technique called polymorphism.ztco_calculator
could be created.
DATA: sucharge_calculator TYPE REF TO ztco_surcharge_calculator,
material_type TYPE mtart.
SELECT SINGLE mtart INTO material_type FROM mara WHERE matnr = purchase_order_item-matnr.
CASE material_type.
WHEN 'EOIL'.
" engine oil
surcharge_calculator = NEW ztco_surcharges_engine_oil( ).
WHEN 'BEAR'.
" ball bearing
surcharge_calculator = NEW ztco_surcharges_ball_bearing( ).
OTHERS.
surcharge_calculator = NEW ztco_surcharges_default( ).
ENDCASE.
DATA(tco_calculator) = NEW ztco_calculator(
surcharge_calculator = surcharge_calculator
purchase_order_item = purchase_order_item ).
cl_demo_output=>display( tco_calculator->calculate_tco_with_surcharges( ) ).
ztco_calculator
only needs two interface methods. You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
4 | |
3 | |
2 | |
2 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |