Übung Schrödinger: 08 Klassen global (ABAP)

Aus MattWiki

Zwei globale Klassen, die aus dem Programm mit den lokalen Klassen in Übung Schrödinger: 08 Klasse lokal (ABAP) verwendet werden:

ZCL_MATT_CBEAN

class ZCL_MATT_CBEAN definition
  public
  final
  create public .

public section.

  methods SET_VOLUME
    importing
      value(ID_VOLUME) type I .
  methods GET_VOLUME
    returning
      value(RD_VOLUME) type I .
  methods CONSTRUCTOR .
protected section.
private section.

  data GD_VOLUME type I value 350. "#EC NOTEXT .
ENDCLASS.



CLASS ZCL_MATT_CBEAN IMPLEMENTATION.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_MATT_CBEAN->CONSTRUCTOR
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD constructor.
  DATA: ld_randvol TYPE i,
        ld_newvol TYPE i.

  CALL FUNCTION 'QF05_RANDOM_INTEGER'
   EXPORTING
     ran_int_max         = 100
     ran_int_min         = 1
   IMPORTING
     ran_int             = ld_randvol
*   EXCEPTIONS
*     INVALID_INPUT       = 1
*     OTHERS              = 2
            .
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ELSE.
    ld_newvol = me->get_volume( ).
    ld_newvol = ld_newvol - 50 + ld_randvol.
    me->set_volume( ld_newvol ).
  ENDIF.
ENDMETHOD.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_MATT_CBEAN->GET_VOLUME
* +-------------------------------------------------------------------------------------------------+
* | [<-()] RD_VOLUME                      TYPE        I
* +--------------------------------------------------------------------------------------</SIGNATURE>
method GET_VOLUME.
  rd_volume = me->gd_volume.

endmethod.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_MATT_CBEAN->SET_VOLUME
* +-------------------------------------------------------------------------------------------------+
* | [--->] ID_VOLUME                      TYPE        I
* +--------------------------------------------------------------------------------------</SIGNATURE>
method SET_VOLUME.
  me->gd_volume = id_volume.
endmethod.
ENDCLASS.

ZCL_MATT_CBEAN

class ZCL_MATT_CBEANBOX definition
  public
  final
  create public .

public section.

  constants GC_MAX_VOLUME type I value 700000. "#EC NOTEXT

  events LOADED .

  methods INSERT_BEAN
    importing
      !IR_CBEAN type ref to ZCL_MATT_CBEAN .
protected section.
private section.

  data GD_VOLUME type I .
  data GD_BEANCOUNT type I .
ENDCLASS.



CLASS ZCL_MATT_CBEANBOX IMPLEMENTATION.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_MATT_CBEANBOX->INSERT_BEAN
* +-------------------------------------------------------------------------------------------------+
* | [--->] IR_CBEAN                       TYPE REF TO ZCL_MATT_CBEAN
* +--------------------------------------------------------------------------------------</SIGNATURE>
METHOD insert_bean.
  DATA: ld_totalvol LIKE me->gd_volume,
        ld_newvol LIKE me->gd_volume.

  " Neues Füllvolumen des Behälters berechnen
  ld_newvol = ir_cbean->get_volume( ).
  ld_totalvol = me->gd_volume + ld_newvol.

  " Prüfen, ob Behälter voll ist
  IF ld_totalvol > me->gc_max_volume.
    " Wenn Behälter voll ist, Event ausführen
    RAISE EVENT loaded.
  ELSE.
    " Wenn Behälter noch nicht voll, dann nächste Bohne einfüllen
    me->gd_volume = ld_totalvol.
    me->gd_beancount = me->gd_beancount + 1.
    WRITE: / 'Bohne: ', me->gd_beancount, ' Bohnenvolumen: ', ld_newvol, ' Füllstand (mm3): ', me->gd_volume.
  ENDIF.
ENDMETHOD.
ENDCLASS.