* _______ _______ _______ _______ _______ _______ ______ _______ _______ ______ _______ * | _ | _ | _ | | | _ | || _ | _ | _ | | _ | * | |_| | |_| | |_| | _ | | |_| | _ | |_| | |_| | | || | |_| | * | | | | |_| | | | | | | | | |_||_| | * | | _ || | ___| _| | |_| | | _ || __ | | * | _ | |_| | _ | | | |_| _ | | _ | |_| | | | | _ | * |__| |__|_______|__| |__|___| |_______|__| |__|______||__| |__|_______|___| |_|__| |__| * www.abapcadabra.com *------------------------------------------------------------------------------------------- * program : ZABAPCADABRA_OO_BY_EXAMPLE_01 * title : Tutorial example: * functional area : Development * environment : 4.7 * program Function : To get the main idea on Object Oriented programming, an example with * real-life objects is set up on AbapcadabrA.com. Cars and carparks are * brought to life in this example report. * Documentation : Search for "Cars in carparks" on AbapcadabrA.com * http://abapcadabra.com/index.php/abap-objects/by-example/536-class-instance * Previous version : This is the initial version * Developer name : Wim Maasdam * Development date : 21-06-2015 * Version : 0.1 *--------------------------------------------------------------------- * Change list: * Date Description * 21-06-2015 Initial release * *--------------------------------------------------------------------- REPORT ZABAPCADABRA_OO_BY_EXAMPLE_01. CLASS lcl_timeline DEFINITION. PUBLIC SECTION. CLASS-DATA: gv_timestamp type timestamp. CLASS-METHODS: read_clock RETURNING VALUE(timestamp) type timestamp. ENDCLASS. CLASS lcl_timeline IMPLEMENTATION. METHOD read_clock. data: lv_seconds type DATATYPE-INTEGER2. * Read a fictional clock, the first call it is set to the current * date and time, for further calls a random number of seconds is * added. if gv_timestamp is initial. GET TIME STAMP FIELD gv_timestamp. else. * Determine a random number of seconds between 10 and 1200 CALL FUNCTION 'RANDOM_I2' EXPORTING RND_MIN = 10 RND_MAX = 1200 IMPORTING rnd_value = lv_seconds. gv_timestamp = cl_abap_tstmp=>ADD( exporting TSTMP = gv_timestamp SECS = lv_seconds ). endif. timestamp = gv_timestamp. ENDMETHOD. ENDCLASS. CLASS lcl_car DEFINITION. PUBLIC SECTION. TYPES: begin of ty_car, object type ref to lcl_car, end of ty_car. CLASS-DATA: gt_cars type STANDARD TABLE OF ty_car. DATA: gv_licenceplate type string. CLASS-METHODS: register importing licenceplate type string returning value(car) type ref to lcl_car, display. ENDCLASS. CLASS lcl_car IMPLEMENTATION. method register. data: lw_car type ty_car. * First look up the licenceplate - has this car been registered before ? read table gt_cars with key object->gv_licenceplate = licenceplate into lw_car. if sy-subrc <> 0. * The car is registered for the first time create object lw_car-object. lw_car-object->gv_licenceplate = licenceplate. append lw_car to gt_cars. endif. car = lw_car-object. endmethod. method display. data: lw_car type ty_car. loop at gt_cars into lw_car. write: / lw_car-object->gv_licenceplate. endloop. endmethod. ENDCLASS. CLASS lcl_carpark DEFINITION. PUBLIC SECTION. TYPES: begin of ty_visiting_car, car type ref to lcl_car, gv_timestamp_in type timestamp, gv_timestamp_out type timestamp, end of ty_visiting_car. DATA: gv_name type string, gt_visits type standard table of ty_visiting_car. METHODS: constructor importing carparkname type string, drive_in importing licenceplate type string, drive_out importing licenceplate type string, display. ENDCLASS. CLASS lcl_carpark IMPLEMENTATION. method constructor. gv_name = carparkname. endmethod. method drive_in. data: lw_visiting_car type ty_visiting_car. clear lw_visiting_car. lw_visiting_car-car = lcl_car=>register( licenceplate ). * GET TIME STAMP FIELD lw_visiting_car-gv_timestamp_in. lw_visiting_car-gv_timestamp_in = lcl_timeline=>read_clock( ). append lw_visiting_car to gt_visits. endmethod. method drive_out. field-symbols: type ty_visiting_car. read table gt_visits assigning with key car->gv_licenceplate = licenceplate gv_timestamp_out = 0. if sy-subrc = 0. * GET TIME STAMP FIELD -gv_timestamp_out. -gv_timestamp_out = lcl_timeline=>read_clock( ). endif. endmethod. method display. data: lw_visiting_car type ty_visiting_car, lw_seconds TYPE TZNTSTMPL. skip. write: / '==>', gv_name. skip. loop at gt_visits into lw_visiting_car. write: /(15) lw_visiting_car-car->gv_licenceplate, lw_visiting_car-gv_timestamp_in using edit mask '____/__/__ __:__:__'. if not lw_visiting_car-gv_timestamp_out is initial. write: lw_visiting_car-gv_timestamp_out using edit mask '____/__/__ __:__:__'. * Calculate the parking duration: lw_seconds = cl_abap_tstmp=>SUBTRACT( exporting TSTMP1 = lw_visiting_car-gv_timestamp_out TSTMP2 = lw_visiting_car-gv_timestamp_in ). write: lw_seconds DECIMALS 0 EXPONENT 0, 'seconds'. else. write: 'Still parked'. endif. endloop. endmethod. ENDCLASS. start-of-selection. data: lo_north type ref to lcl_carpark, lo_south type ref to lcl_carpark. * Create the carparks: create object lo_north exporting carparkname = 'Class-city Carpark - North'. create object lo_south exporting carparkname = 'Class-city Carpark - South'. * Bring a few cars to the parks: lo_north->drive_in( '89-KVV-6' ). lo_north->drive_in( '52-RK-DF' ). lo_south->drive_in( 'R1243AL' ). lo_south->drive_in( 'R1243BL' ). lo_south->drive_in( 'R1243CL' ). lo_north->drive_in( 'R1243DL' ). * A bit more parking activity: lo_north->drive_out( '52-RK-DF' ). lo_south->drive_in( '52-RK-DF' ). lo_south->drive_out( 'R1243AL' ). lo_south->drive_out( 'R1243CL' ). lo_south->drive_out( 'R1243BL' ). lo_south->drive_out( '52-RK-DF' ). lo_north->drive_in( '52-RK-DF' ). write: / 'Registered cars'. write: / '==============='. lcl_car=>display( ). skip. write: / 'Park details'. write: / '============'. lo_north->display( ). lo_south->display( ).