Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014aUaQeLnwbb1zTpN7kHeat
3.5 KiB
Please implement an expense claim validator for us. The goal is to validate an employee expense claim before it is posted to accounting. The check reports the first rule that fails as a class-based exception. Each exception carries the value that caused the failure and a short message. There are no open questions.
The interface {{P}}IF_EXPENSE_CLAIM exists in package $TMP and is active. It contains types only and has no methods. TY_AMOUNT is a packed number with length 8 and 2 decimals. TY_CURRENCY is a character field with length 3. TY_COST_CENTER is a character field with length 10. TY_CLAIM is a structure with components AMOUNT of type TY_AMOUNT, CURRENCY of type TY_CURRENCY, CLAIM_DATE of type D, COST_CENTER of type TY_COST_CENTER, and HAS_RECEIPT of type ABAP_BOOL. The class {{P}}CX_CLAIM_ERROR exists in package $TMP and is active. It is a direct subclass of CX_STATIC_CHECK. Its constructor has importing parameter IV_MESSAGE of type string. The method GET_MESSAGE returns this text of type string.
Please create the class {{P}}EXPENSE_VALIDATOR in package $TMP. The class is public and final and has a public constructor without parameters. The class has exactly one public method: VALIDATE importing is_claim of type {{P}}IF_EXPENSE_CLAIM=>TY_CLAIM and raising {{P}}CX_CLAIM_ERROR. Also create these exception classes in package $TMP. Each one inherits from {{P}}CX_CLAIM_ERROR, is final, and has a public constructor. {{P}}CX_CLAIM_AMOUNT has constructor parameter IV_AMOUNT of type TY_AMOUNT of {{P}}IF_EXPENSE_CLAIM. It has public read-only attribute MV_AMOUNT of the same type. Its message is Amount must be positive. {{P}}CX_CLAIM_CURRENCY has constructor parameter IV_CURRENCY of type TY_CURRENCY. It has public read-only attribute MV_CURRENCY of the same type. Its message is Currency is not supported. {{P}}CX_CLAIM_DATE has constructor parameter IV_CLAIM_DATE of type D. It has public read-only attribute MV_CLAIM_DATE of type D. Its message is Claim date is in the future. {{P}}CX_CLAIM_RECEIPT has constructor parameter IV_AMOUNT of type TY_AMOUNT. It has public read-only attribute MV_AMOUNT of the same type. Its message is Receipt is required. {{P}}CX_CLAIM_COSTCENTER has constructor parameter IV_AMOUNT of type TY_AMOUNT. It has public read-only attribute MV_AMOUNT of the same type. Its message is Cost center is required. GET_MESSAGE returns the message of the exception. Do not add other public methods or attributes.
Check the claim in this sequence. Raise the exception of the first rule that fails. If no rule fails, the method ends without an exception. AMOUNT must be greater than zero. If not, raise {{P}}CX_CLAIM_AMOUNT with IV_AMOUNT = AMOUNT. Then CURRENCY must be one of EUR, USD, GBP, CHF. If not, raise {{P}}CX_CLAIM_CURRENCY with IV_CURRENCY = CURRENCY. Then CLAIM_DATE must not be later than the current date. If it is later, raise {{P}}CX_CLAIM_DATE with IV_CLAIM_DATE = CLAIM_DATE. Then if AMOUNT is 100.00 or more, HAS_RECEIPT must be true. If not, raise {{P}}CX_CLAIM_RECEIPT with IV_AMOUNT = AMOUNT. Then if AMOUNT is 1000.00 or more, COST_CENTER must not be initial. If it is initial, raise {{P}}CX_CLAIM_COSTCENTER with IV_AMOUNT = AMOUNT.
Use release target 8.16. Follow Clean ABAP. Keep method length below 40 statements. Do not use global variables. Do not write comments that restate the code. Do not change {{P}}IF_EXPENSE_CLAIM and {{P}}CX_CLAIM_ERROR. All objects must be active and have no syntax error. The hidden tests must pass. Write your own ABAP Unit tests for {{P}}EXPENSE_VALIDATOR.