CLASS {{p}}oil_settlement DEFINITION PUBLIC FINAL CREATE PUBLIC. PUBLIC SECTION. TYPES ty_restaurant TYPE c LENGTH 10. TYPES: BEGIN OF ty_collection, restaurant TYPE ty_restaurant, collected_on TYPE d, litres TYPE i, END OF ty_collection. TYPES tt_collections TYPE STANDARD TABLE OF ty_collection WITH EMPTY KEY. TYPES: BEGIN OF ty_settlement, restaurant TYPE ty_restaurant, total_litres TYPE i, round_count TYPE i, last_collection TYPE d, END OF ty_settlement. TYPES tt_settlements TYPE SORTED TABLE OF ty_settlement WITH UNIQUE KEY restaurant. METHODS settle IMPORTING it_collections TYPE tt_collections RETURNING VALUE(rt_settlements) TYPE tt_settlements. PRIVATE SECTION. TYPES: BEGIN OF ty_buffer, restaurant TYPE ty_restaurant, total_litres TYPE i, round_count TYPE i, last_collection TYPE d, END OF ty_buffer. TYPES tt_buffer TYPE HASHED TABLE OF ty_buffer WITH UNIQUE KEY restaurant. METHODS normalize IMPORTING iv_restaurant TYPE ty_restaurant RETURNING VALUE(rv_restaurant) TYPE ty_restaurant. ENDCLASS. CLASS {{p}}oil_settlement IMPLEMENTATION. METHOD settle. DATA lt_buffer TYPE tt_buffer. LOOP AT it_collections INTO DATA(ls_collection). DATA(lv_restaurant) = normalize( ls_collection-restaurant ). IF lv_restaurant IS INITIAL OR ls_collection-litres <= 0. CONTINUE. ENDIF. ASSIGN lt_buffer[ restaurant = lv_restaurant ] TO FIELD-SYMBOL(). IF sy-subrc <> 0. INSERT VALUE #( restaurant = lv_restaurant ) INTO TABLE lt_buffer ASSIGNING . ENDIF. -total_litres = -total_litres + ls_collection-litres. -round_count = -round_count + 1. IF ls_collection-collected_on > -last_collection. -last_collection = ls_collection-collected_on. ENDIF. ENDLOOP. rt_settlements = CORRESPONDING #( lt_buffer ). ENDMETHOD. METHOD normalize. rv_restaurant = to_upper( replace( val = iv_restaurant sub = ` ` with = `` occ = 0 ) ). ENDMETHOD. ENDCLASS.