Add clock exercise (#25)

* Create instructions.md

* Clock exercise

* Allow any class attribute and type names

* Formatting
This commit is contained in:
Marc Bernard
2021-11-30 00:35:00 -05:00
committed by GitHub
parent 3120bd6548
commit 8631b96f54
8 changed files with 435 additions and 36 deletions

View File

@@ -84,19 +84,7 @@
"check_syntax": true,
"check_text_elements": true,
"check_transformation_exists": true,
"class_attribute_names": {
"exclude": [],
"severity": "Error",
"patternKind": "required",
"ignoreNames": [],
"ignorePatterns": [],
"ignoreExceptions": true,
"ignoreLocal": true,
"ignoreInterfaces": false,
"statics": "^G._.+$",
"instance": "^M._.+$",
"constants": ""
},
"class_attribute_names": false,
"cloud_types": true,
"colon_missing_space": true,
"commented_code": {
@@ -516,11 +504,7 @@
"tabl_enhancement_category": true,
"try_without_catch": true,
"type_form_parameters": true,
"types_naming": {
"exclude": [],
"severity": "Error",
"pattern": "^TY_.+$"
},
"types_naming": false,
"uncaught_exception": true,
"unknown_types": true,
"unreachable_code": true,

View File

@@ -84,19 +84,7 @@
"check_syntax": true,
"check_text_elements": true,
"check_transformation_exists": true,
"class_attribute_names": {
"exclude": [],
"severity": "Error",
"patternKind": "required",
"ignoreNames": [],
"ignorePatterns": [],
"ignoreExceptions": true,
"ignoreLocal": true,
"ignoreInterfaces": false,
"statics": "^G._.+$",
"instance": "^M._.+$",
"constants": ""
},
"class_attribute_names": false,
"cloud_types": true,
"colon_missing_space": true,
"commented_code": {
@@ -516,11 +504,7 @@
"tabl_enhancement_category": true,
"try_without_catch": true,
"type_form_parameters": true,
"types_naming": {
"exclude": [],
"severity": "Error",
"pattern": "^TY_.+$"
},
"types_naming": false,
"uncaught_exception": true,
"unknown_types": true,
"unreachable_code": true,

View File

@@ -60,6 +60,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "clock",
"name": "Clock",
"uuid": "ac4e5fb1-1e7c-461d-b2df-1abfd88d8c8a",
"practices": [],
"prerequisites": [],
"difficulty": 1
}
]
},

View File

@@ -0,0 +1,7 @@
# Instructions
Implement a clock that handles times without dates.
You should be able to add and subtract minutes to it.
Two clocks that represent the same time should be equal to each other.

View File

@@ -0,0 +1,10 @@
{
"blurb": "Clock",
"authors": ["mbtools"],
"contributors": [],
"files": {
"solution": ["zcl_clock.clas.abap"],
"test": ["zcl_clock.clas.testclasses.abap"],
"example": [".meta/zcl_clock.clas.abap"]
}
}

View File

@@ -0,0 +1,54 @@
CLASS zcl_clock DEFINITION
PUBLIC
CREATE PUBLIC.
PUBLIC SECTION.
METHODS constructor
IMPORTING
!hours TYPE i
!minutes TYPE i DEFAULT 0.
METHODS get
RETURNING
VALUE(result) TYPE string.
METHODS add
IMPORTING
!minutes TYPE i.
METHODS sub
IMPORTING
!minutes TYPE i.
PRIVATE SECTION.
DATA clock TYPE t.
ENDCLASS.
CLASS zcl_clock IMPLEMENTATION.
METHOD add.
clock = clock + minutes * 60.
ENDMETHOD.
METHOD constructor.
" Clock is number of seconds
clock = hours * 60 * 60 + minutes * 60.
ENDMETHOD.
METHOD get.
" hh:mm:ss
result = |{ clock TIME = ISO }|.
" return only hh:mm
result = result(5).
ENDMETHOD.
METHOD sub.
clock = clock - minutes * 60.
ENDMETHOD.
ENDCLASS.

View File

@@ -0,0 +1,49 @@
CLASS zcl_clock DEFINITION
PUBLIC
CREATE PUBLIC.
PUBLIC SECTION.
METHODS constructor
IMPORTING
!hours TYPE i
!minutes TYPE i DEFAULT 0.
METHODS get
RETURNING
VALUE(result) TYPE string.
METHODS add
IMPORTING
!minutes TYPE i.
METHODS sub
IMPORTING
!minutes TYPE i.
PRIVATE SECTION.
* add solution here
ENDCLASS.
CLASS zcl_clock IMPLEMENTATION.
METHOD add.
* add solution here
ENDMETHOD.
METHOD constructor.
* add solution here
ENDMETHOD.
METHOD get.
* add solution here
ENDMETHOD.
METHOD sub.
* add solution here
ENDMETHOD.
ENDCLASS.

View File

@@ -0,0 +1,303 @@
CLASS ltcl_clock DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATION SHORT FINAL.
PRIVATE SECTION.
DATA cut TYPE REF TO zcl_clock.
METHODS:
on_the_hour FOR TESTING,
past_the_hour FOR TESTING,
adding_a_few_minutes FOR TESTING,
adding_zero_minutes FOR TESTING,
adding_over_an_hour FOR TESTING,
adding_more_than_two_hours_w_c FOR TESTING,
adding_more_than_two_days FOR TESTING,
wrap_around_at_midnight FOR TESTING,
subtract_minutes FOR TESTING,
subtract_more_than_two_hours FOR TESTING,
subtract_more_than_two_hours_w FOR TESTING,
subtract_more_than_two_days FOR TESTING,
wrap_around_backwards FOR TESTING,
wrap_around_day FOR TESTING,
wrap_around_day_backwards FOR TESTING,
equivalent_clocks FOR TESTING,
inequivalent_clocks FOR TESTING,
equivalent_clocks_1 FOR TESTING,
equivalent_clocks_2 FOR TESTING,
equivalent_clocks_3 FOR TESTING,
equivalent_clocks_4 FOR TESTING,
equivalent_clocks_5 FOR TESTING,
equivalent_clocks_6 FOR TESTING,
hours_rollover FOR TESTING,
minutes_rollover FOR TESTING,
hours_and_minutes_rollover FOR TESTING,
negative_hours_rollover FOR TESTING,
negative_minutes_rollover FOR TESTING,
negative_hours_and_minutes FOR TESTING.
ENDCLASS.
CLASS ltcl_clock IMPLEMENTATION.
METHOD on_the_hour.
cut = NEW zcl_clock( 8 ).
cl_abap_unit_assert=>assert_equals(
exp = '08:00'
act = cut->get( ) ).
ENDMETHOD.
METHOD past_the_hour.
cut = NEW zcl_clock( hours = 11
minutes = 9 ).
cl_abap_unit_assert=>assert_equals(
exp = '11:09'
act = cut->get( ) ).
ENDMETHOD.
METHOD adding_a_few_minutes.
cut = NEW zcl_clock( 10 ).
cut->add( 3 ).
cl_abap_unit_assert=>assert_equals(
exp = '10:03'
act = cut->get( ) ).
ENDMETHOD.
METHOD adding_zero_minutes.
cut = NEW zcl_clock( hours = 6
minutes = 41 ).
cut->add( 0 ).
cl_abap_unit_assert=>assert_equals(
exp = '06:41'
act = cut->get( ) ).
ENDMETHOD.
METHOD adding_over_an_hour.
cut = NEW zcl_clock( 10 ).
cut->add( 61 ).
cl_abap_unit_assert=>assert_equals(
exp = '11:01'
act = cut->get( ) ).
ENDMETHOD.
METHOD adding_more_than_two_hours_w_c.
cut = NEW zcl_clock( hours = 0
minutes = 45 ).
cut->add( 160 ).
cl_abap_unit_assert=>assert_equals(
exp = '03:25'
act = cut->get( ) ).
ENDMETHOD.
METHOD adding_more_than_two_days.
cut = NEW zcl_clock( hours = 1
minutes = 1 ).
cut->add( 3500 ).
cl_abap_unit_assert=>assert_equals(
exp = '11:21'
act = cut->get( ) ).
ENDMETHOD.
METHOD wrap_around_at_midnight.
cut = NEW zcl_clock( hours = 23
minutes = 30 ).
cut->add( 60 ).
cl_abap_unit_assert=>assert_equals(
exp = '00:30'
act = cut->get( ) ).
ENDMETHOD.
METHOD subtract_minutes.
cut = NEW zcl_clock( 10 ).
cut->sub( 90 ).
cl_abap_unit_assert=>assert_equals(
exp = '08:30'
act = cut->get( ) ).
ENDMETHOD.
METHOD subtract_more_than_two_hours.
cut = NEW zcl_clock( hours = 6
minutes = 15 ).
cut->sub( 160 ).
cl_abap_unit_assert=>assert_equals(
exp = '03:35'
act = cut->get( ) ).
ENDMETHOD.
METHOD subtract_more_than_two_hours_w.
cut = NEW zcl_clock( hours = 6
minutes = 15 ).
cut->add( -160 ).
cl_abap_unit_assert=>assert_equals(
exp = '03:35'
act = cut->get( ) ).
ENDMETHOD.
METHOD subtract_more_than_two_days.
cut = NEW zcl_clock( hours = 2
minutes = 20 ).
cut->sub( 3000 ).
cl_abap_unit_assert=>assert_equals(
exp = '00:20'
act = cut->get( ) ).
ENDMETHOD.
METHOD wrap_around_backwards.
cut = NEW zcl_clock( hours = 0
minutes = 30 ).
cut->sub( 60 ).
cl_abap_unit_assert=>assert_equals(
exp = '23:30'
act = cut->get( ) ).
ENDMETHOD.
METHOD wrap_around_day.
cut = NEW zcl_clock( hours = 5
minutes = 32 ).
cut->add( 25 * 60 ).
cl_abap_unit_assert=>assert_equals(
exp = '06:32'
act = cut->get( ) ).
ENDMETHOD.
METHOD wrap_around_day_backwards.
cut = NEW zcl_clock( hours = 5
minutes = 32 ).
cut->sub( 25 * 60 ).
cl_abap_unit_assert=>assert_equals(
exp = '04:32'
act = cut->get( ) ).
ENDMETHOD.
METHOD equivalent_clocks.
cl_abap_unit_assert=>assert_equals(
exp = NEW zcl_clock( hours = 15
minutes = 37 )->get( )
act = NEW zcl_clock( hours = 15
minutes = 37 )->get( ) ).
ENDMETHOD.
METHOD inequivalent_clocks.
cl_abap_unit_assert=>assert_differs(
exp = NEW zcl_clock( hours = 1
minutes = 1 )->get( )
act = NEW zcl_clock( hours = 18
minutes = 32 )->get( ) ).
ENDMETHOD.
METHOD equivalent_clocks_1.
cl_abap_unit_assert=>assert_equals(
exp = NEW zcl_clock( hours = 3
minutes = 11 )->get( )
act = NEW zcl_clock( hours = 99
minutes = 11 )->get( ) ).
ENDMETHOD.
METHOD equivalent_clocks_2.
cl_abap_unit_assert=>assert_equals(
exp = NEW zcl_clock( hours = 22
minutes = 40 )->get( )
act = NEW zcl_clock( hours = -2
minutes = 40 )->get( ) ).
ENDMETHOD.
METHOD equivalent_clocks_3.
cl_abap_unit_assert=>assert_equals(
exp = NEW zcl_clock( hours = 17
minutes = 3 )->get( )
act = NEW zcl_clock( hours = -31
minutes = 3 )->get( ) ).
ENDMETHOD.
METHOD equivalent_clocks_4.
cl_abap_unit_assert=>assert_equals(
exp = NEW zcl_clock( hours = 2
minutes = 2 )->get( )
act = NEW zcl_clock( hours = 2
minutes = 4322 )->get( ) ).
ENDMETHOD.
METHOD equivalent_clocks_5.
cl_abap_unit_assert=>assert_equals(
exp = NEW zcl_clock( hours = 2
minutes = 40 )->get( )
act = NEW zcl_clock( hours = 3
minutes = -20 )->get( ) ).
ENDMETHOD.
METHOD equivalent_clocks_6.
cl_abap_unit_assert=>assert_equals(
exp = NEW zcl_clock( hours = 7
minutes = 32 )->get( )
act = NEW zcl_clock( hours = -12
minutes = -268 )->get( ) ).
ENDMETHOD.
METHOD hours_rollover.
cl_abap_unit_assert=>assert_equals(
exp = '04:00'
act = NEW zcl_clock( 100 )->get( ) ).
ENDMETHOD.
METHOD minutes_rollover.
cl_abap_unit_assert=>assert_equals(
exp = '04:43'
act = NEW zcl_clock( hours = 0
minutes = 1723 )->get( ) ).
ENDMETHOD.
METHOD hours_and_minutes_rollover.
cl_abap_unit_assert=>assert_equals(
exp = '00:00'
act = NEW zcl_clock( hours = 72
minutes = 8640 )->get( ) ).
ENDMETHOD.
METHOD negative_hours_rollover.
cl_abap_unit_assert=>assert_equals(
exp = '05:00'
act = NEW zcl_clock( -91 )->get( ) ).
ENDMETHOD.
METHOD negative_minutes_rollover.
cl_abap_unit_assert=>assert_equals(
exp = '16:40'
act = NEW zcl_clock( hours = 1
minutes = -4820 )->get( ) ).
ENDMETHOD.
METHOD negative_hours_and_minutes.
cl_abap_unit_assert=>assert_equals(
exp = '22:10'
act = NEW zcl_clock( hours = -121
minutes = -5810 )->get( ) ).
ENDMETHOD.
ENDCLASS.