fork download
  1. public class LoyaltyProgramHandler {
  2. public void processTransaction(Id loyaltyProgramId, Decimal transactionAmount, String transactionType) {
  3. // Retrieve the loyalty program record
  4. Loyalty_Program__c loyaltyProgram = [SELECT Id, Promotion_Type__c, Is_Active__c
  5. FROM Loyalty_Program__c
  6. WHERE Id = :loyaltyProgramId
  7. LIMIT 1];
  8.  
  9. if (!loyaltyProgram.Is_Active__c) {
  10. throw new CustomException('Loyalty program is not active.');
  11. }
  12.  
  13. if (loyaltyProgram.Promotion_Type__c == 'Cumulative') {
  14. if (transactionType == 'Accrual') {
  15. creditPoints(loyaltyProgramId, transactionAmount);
  16. } else if (transactionType == 'Redemption') {
  17. redeemPoints(loyaltyProgramId, transactionAmount);
  18. } else {
  19. throw new CustomException('Invalid transaction type.');
  20. }
  21. }
  22. }
  23.  
  24. private void creditPoints(Id loyaltyProgramId, Decimal transactionAmount) {
  25. // Assuming 1 point for every 2 dollars spent
  26. Integer pointsEarned = (Integer)(transactionAmount / 2);
  27.  
  28. // Create a transaction journal record for accrual
  29. Transaction_Journal__c journal = new Transaction_Journal__c(
  30. Loyalty_Program__c = loyaltyProgramId,
  31. Points_Earned__c = pointsEarned,
  32. Transaction_Type__c = 'Accrual',
  33. Transaction_Date__c = System.now()
  34. );
  35.  
  36. insert journal;
  37.  
  38. // Update loyalty program points (pseudo-code)
  39. Loyalty_Program__c loyaltyProgram = [SELECT Id, Points__c FROM Loyalty_Program__c WHERE Id = :loyaltyProgramId];
  40. loyaltyProgram.Points__c += pointsEarned;
  41. update loyaltyProgram;
  42. }
  43.  
  44. private void redeemPoints(Id loyaltyProgramId, Decimal pointsToRedeem) {
  45. // Retrieve current points
  46. Loyalty_Program__c loyaltyProgram = [SELECT Id, Points__c FROM Loyalty_Program__c WHERE Id = :loyaltyProgramId];
  47.  
  48. if (pointsToRedeem > loyaltyProgram.Points__c) {
  49. throw new CustomException('Insufficient points for redemption.');
  50. }
  51.  
  52. // Create a transaction journal record for redemption
  53. Transaction_Journal__c journal = new Transaction_Journal__c(
  54. Loyalty_Program__c = loyaltyProgramId,
  55. Points_Redeemed__c = pointsToRedeem,
  56. Transaction_Type__c = 'Redemption',
  57. Transaction_Date__c = System.now()
  58. );
  59.  
  60. insert journal;
  61.  
  62. // Update loyalty program points
  63. loyaltyProgram.Points__c -= pointsToRedeem;
  64. update loyaltyProgram;
  65. }
  66.  
  67. // Custom exception class
  68. public class CustomException extends Exception {}
  69. }
  70.  
Success #stdin #stdout #stderr 0.02s 10532KB
stdin
Standard input is empty
stdout
Object: UndefinedObject error: did not understand #LoyaltyProgramHandler
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
UndefinedObject class(Object)>>doesNotUnderstand: #LoyaltyProgramHandler (SysExcept.st:1448)
UndefinedObject>>executeStatements (prog:1)
Object: nil error: did not understand #associationAt:ifAbsent:
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
UndefinedObject(Object)>>doesNotUnderstand: #associationAt:ifAbsent: (SysExcept.st:1448)
DeferredVariableBinding>>resolvePathFrom: (DeferBinding.st:115)
DeferredVariableBinding>>value (DeferBinding.st:69)
UndefinedObject>>executeStatements (prog:9)
stderr
./prog:2: parse error, expected '}'
./prog:9: expected expression