11package com .iluwatar ;
22
3+ import java .util .logging .Level ;
4+ import java .util .logging .Logger ;
5+
36/**
47 * BusinessTransaction class handles the logic of processing customer transactions.
58 * It works with the Framework to acquire and release locks for resources.
69 */
710public class BusinessTransaction {
811
12+ private static final Logger logger = Logger .getLogger (BusinessTransaction .class .getName ());
913 private final Framework framework ;
1014
1115 /**
@@ -24,10 +28,11 @@ public BusinessTransaction(Framework framework) {
2428 * @param resource the resource to be locked during the transaction
2529 * @param customerId the ID of the customer being processed
2630 * @param customerData the data related to the customer being processed
31+ * @return true if the transaction was processed successfully, false otherwise
2732 */
28- public void processCustomer (Resource resource , String customerId , String customerData ) {
29- // Print a message indicating which customer is being processed
30- System . out . println ( "Processing customer " + customerId + " with data: " + customerData );
33+ public boolean processCustomer (Resource resource , String customerId , String customerData ) {
34+ // Log a message indicating which customer is being processed
35+ logger . log ( Level . INFO , "Processing customer {0} with data: {1}" , new Object []{ customerId , customerData } );
3136
3237 // Try to acquire the lock for the resource
3338 if (framework .tryLockResource (resource )) {
@@ -36,11 +41,18 @@ public void processCustomer(Resource resource, String customerId, String custome
3641 Thread .sleep (500 );
3742 } catch (InterruptedException e ) {
3843 Thread .currentThread ().interrupt (); // Handle interruption
44+ return false ; // Return false if the thread was interrupted
3945 }
46+
4047 // Release the lock after processing is done
4148 framework .notifyReleaseLock (resource );
49+
50+ // Log success and return true to indicate the transaction was completed
51+ logger .log (Level .INFO , "Customer {0} processed successfully." , customerId );
52+ return true ;
4253 } else {
43- System .out .println ("Failed to acquire lock for resource: " + resource .getId ());
54+ logger .log (Level .WARNING , "Failed to acquire lock for resource: {0}" , resource .getId ());
55+ return false ; // Return false if the lock was not acquired
4456 }
4557 }
4658}
0 commit comments