508 : Sonar qube critical Issue Fix (#854)
* 508 : Sonar qube critical Issue Fix Refactor this method to reduce its Cognitive Complexity from 30 to the 15 allowed. * 508: Sonar Qube Issue fxes Define a constant instead of duplicating this literal " does not exist." 3 times. * 508: sonar qube issue fixes Define a constant instead of duplicating this literal "Some external api for only realtime execution could be called here." 3 times.
This commit is contained in:
		
				
					committed by
					
						 Ilkka Seppälä
						Ilkka Seppälä
					
				
			
			
				
	
			
			
			
						parent
						
							c6ecf58687
						
					
				
				
					commit
					f7e22a1cf6
				
			| @@ -60,7 +60,8 @@ public class App { | |||||||
|   /** |   /** | ||||||
|    * Program entry point. |    * Program entry point. | ||||||
|    * |    * | ||||||
|    * @param args command line args |    * @param args | ||||||
|  |    *          command line args | ||||||
|    */ |    */ | ||||||
|   public static void main(String[] args) { |   public static void main(String[] args) { | ||||||
|     App app = new App(); |     App app = new App(); | ||||||
| @@ -150,56 +151,11 @@ public class App { | |||||||
|       option = s.nextInt(); |       option = s.nextInt(); | ||||||
|  |  | ||||||
|       if (option == 1) { |       if (option == 1) { | ||||||
|         s.nextLine(); |         processOption1(eventManager, s); | ||||||
|         LOGGER.info("Boil multiple eggs at once (A) or boil them one-by-one (S)?: "); |  | ||||||
|         String eventType = s.nextLine(); |  | ||||||
|         LOGGER.info("How long should this egg be boiled for (in seconds)?: "); |  | ||||||
|         int eventTime = s.nextInt(); |  | ||||||
|         if (eventType.equalsIgnoreCase("A")) { |  | ||||||
|           try { |  | ||||||
|             int eventId = eventManager.createAsync(eventTime); |  | ||||||
|             eventManager.start(eventId); |  | ||||||
|             LOGGER.info("Egg [{}] is being boiled.", eventId); |  | ||||||
|           } catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) { |  | ||||||
|             LOGGER.error(e.getMessage()); |  | ||||||
|           } |  | ||||||
|         } else if (eventType.equalsIgnoreCase("S")) { |  | ||||||
|           try { |  | ||||||
|             int eventId = eventManager.create(eventTime); |  | ||||||
|             eventManager.start(eventId); |  | ||||||
|             LOGGER.info("Egg [{}] is being boiled.", eventId); |  | ||||||
|           } catch (MaxNumOfEventsAllowedException | InvalidOperationException | LongRunningEventException |  | ||||||
|               | EventDoesNotExistException e) { |  | ||||||
|             LOGGER.error(e.getMessage()); |  | ||||||
|           } |  | ||||||
|         } else { |  | ||||||
|           LOGGER.info("Unknown event type."); |  | ||||||
|         } |  | ||||||
|       } else if (option == 2) { |       } else if (option == 2) { | ||||||
|         LOGGER.info("Which egg?: "); |         processOption2(eventManager, s); | ||||||
|         int eventId = s.nextInt(); |  | ||||||
|         try { |  | ||||||
|           eventManager.cancel(eventId); |  | ||||||
|           LOGGER.info("Egg [{}] is removed from boiler.", eventId); |  | ||||||
|         } catch (EventDoesNotExistException e) { |  | ||||||
|           LOGGER.error(e.getMessage()); |  | ||||||
|         } |  | ||||||
|       } else if (option == 3) { |       } else if (option == 3) { | ||||||
|         s.nextLine(); |         processOption3(eventManager, s); | ||||||
|         LOGGER.info("Just one egg (O) OR all of them (A) ?: "); |  | ||||||
|         String eggChoice = s.nextLine(); |  | ||||||
|  |  | ||||||
|         if (eggChoice.equalsIgnoreCase("O")) { |  | ||||||
|           LOGGER.info("Which egg?: "); |  | ||||||
|           int eventId = s.nextInt(); |  | ||||||
|           try { |  | ||||||
|             eventManager.status(eventId); |  | ||||||
|           } catch (EventDoesNotExistException e) { |  | ||||||
|             LOGGER.error(e.getMessage()); |  | ||||||
|           } |  | ||||||
|         } else if (eggChoice.equalsIgnoreCase("A")) { |  | ||||||
|           eventManager.statusOfAllEvents(); |  | ||||||
|         } |  | ||||||
|       } else if (option == 4) { |       } else if (option == 4) { | ||||||
|         eventManager.shutdown(); |         eventManager.shutdown(); | ||||||
|       } |       } | ||||||
| @@ -208,4 +164,61 @@ public class App { | |||||||
|     s.close(); |     s.close(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   private void processOption3(EventManager eventManager, Scanner s) { | ||||||
|  |     s.nextLine(); | ||||||
|  |     LOGGER.info("Just one egg (O) OR all of them (A) ?: "); | ||||||
|  |     String eggChoice = s.nextLine(); | ||||||
|  |  | ||||||
|  |     if (eggChoice.equalsIgnoreCase("O")) { | ||||||
|  |       LOGGER.info("Which egg?: "); | ||||||
|  |       int eventId = s.nextInt(); | ||||||
|  |       try { | ||||||
|  |         eventManager.status(eventId); | ||||||
|  |       } catch (EventDoesNotExistException e) { | ||||||
|  |         LOGGER.error(e.getMessage()); | ||||||
|  |       } | ||||||
|  |     } else if (eggChoice.equalsIgnoreCase("A")) { | ||||||
|  |       eventManager.statusOfAllEvents(); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   private void processOption2(EventManager eventManager, Scanner s) { | ||||||
|  |     LOGGER.info("Which egg?: "); | ||||||
|  |     int eventId = s.nextInt(); | ||||||
|  |     try { | ||||||
|  |       eventManager.cancel(eventId); | ||||||
|  |       LOGGER.info("Egg [{}] is removed from boiler.", eventId); | ||||||
|  |     } catch (EventDoesNotExistException e) { | ||||||
|  |       LOGGER.error(e.getMessage()); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   private void processOption1(EventManager eventManager, Scanner s) { | ||||||
|  |     s.nextLine(); | ||||||
|  |     LOGGER.info("Boil multiple eggs at once (A) or boil them one-by-one (S)?: "); | ||||||
|  |     String eventType = s.nextLine(); | ||||||
|  |     LOGGER.info("How long should this egg be boiled for (in seconds)?: "); | ||||||
|  |     int eventTime = s.nextInt(); | ||||||
|  |     if (eventType.equalsIgnoreCase("A")) { | ||||||
|  |       try { | ||||||
|  |         int eventId = eventManager.createAsync(eventTime); | ||||||
|  |         eventManager.start(eventId); | ||||||
|  |         LOGGER.info("Egg [{}] is being boiled.", eventId); | ||||||
|  |       } catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) { | ||||||
|  |         LOGGER.error(e.getMessage()); | ||||||
|  |       } | ||||||
|  |     } else if (eventType.equalsIgnoreCase("S")) { | ||||||
|  |       try { | ||||||
|  |         int eventId = eventManager.create(eventTime); | ||||||
|  |         eventManager.start(eventId); | ||||||
|  |         LOGGER.info("Egg [{}] is being boiled.", eventId); | ||||||
|  |       } catch (MaxNumOfEventsAllowedException | InvalidOperationException | LongRunningEventException | ||||||
|  |           | EventDoesNotExistException e) { | ||||||
|  |         LOGGER.error(e.getMessage()); | ||||||
|  |       } | ||||||
|  |     } else { | ||||||
|  |       LOGGER.info("Unknown event type."); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -39,6 +39,8 @@ public class EventManager implements ThreadCompleteListener { | |||||||
|   private int currentlyRunningSyncEvent = -1; |   private int currentlyRunningSyncEvent = -1; | ||||||
|   private Random rand; |   private Random rand; | ||||||
|   private Map<Integer, Event> eventPool; |   private Map<Integer, Event> eventPool; | ||||||
|  |    | ||||||
|  |   private static final String DOES_NOT_EXIST = " does not exist."; | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * EventManager constructor. |    * EventManager constructor. | ||||||
| @@ -112,7 +114,7 @@ public class EventManager implements ThreadCompleteListener { | |||||||
|    */ |    */ | ||||||
|   public void start(int eventId) throws EventDoesNotExistException { |   public void start(int eventId) throws EventDoesNotExistException { | ||||||
|     if (!eventPool.containsKey(eventId)) { |     if (!eventPool.containsKey(eventId)) { | ||||||
|       throw new EventDoesNotExistException(eventId + " does not exist."); |       throw new EventDoesNotExistException(eventId + DOES_NOT_EXIST); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     eventPool.get(eventId).start(); |     eventPool.get(eventId).start(); | ||||||
| @@ -126,7 +128,7 @@ public class EventManager implements ThreadCompleteListener { | |||||||
|    */ |    */ | ||||||
|   public void cancel(int eventId) throws EventDoesNotExistException { |   public void cancel(int eventId) throws EventDoesNotExistException { | ||||||
|     if (!eventPool.containsKey(eventId)) { |     if (!eventPool.containsKey(eventId)) { | ||||||
|       throw new EventDoesNotExistException(eventId + " does not exist."); |       throw new EventDoesNotExistException(eventId + DOES_NOT_EXIST); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (eventId == currentlyRunningSyncEvent) { |     if (eventId == currentlyRunningSyncEvent) { | ||||||
| @@ -145,7 +147,7 @@ public class EventManager implements ThreadCompleteListener { | |||||||
|    */ |    */ | ||||||
|   public void status(int eventId) throws EventDoesNotExistException { |   public void status(int eventId) throws EventDoesNotExistException { | ||||||
|     if (!eventPool.containsKey(eventId)) { |     if (!eventPool.containsKey(eventId)) { | ||||||
|       throw new EventDoesNotExistException(eventId + " does not exist."); |       throw new EventDoesNotExistException(eventId + DOES_NOT_EXIST); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     eventPool.get(eventId).status(); |     eventPool.get(eventId).status(); | ||||||
|   | |||||||
| @@ -44,6 +44,8 @@ public class Account { | |||||||
|   private final int accountNo; |   private final int accountNo; | ||||||
|   private final String owner; |   private final String owner; | ||||||
|   private BigDecimal money; |   private BigDecimal money; | ||||||
|  |    | ||||||
|  |   private static final String MSG = "Some external api for only realtime execution could be called here."; | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|    * Instantiates a new Account. |    * Instantiates a new Account. | ||||||
| @@ -126,7 +128,7 @@ public class Account { | |||||||
|     depositMoney(money); |     depositMoney(money); | ||||||
|     AccountAggregate.putAccount(this); |     AccountAggregate.putAccount(this); | ||||||
|     if (realTime) { |     if (realTime) { | ||||||
|       LOGGER.info("Some external api for only realtime execution could be called here."); |       LOGGER.info(MSG); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -138,7 +140,7 @@ public class Account { | |||||||
|     withdrawMoney(money); |     withdrawMoney(money); | ||||||
|     AccountAggregate.putAccount(this); |     AccountAggregate.putAccount(this); | ||||||
|     if (realTime) { |     if (realTime) { | ||||||
|       LOGGER.info("Some external api for only realtime execution could be called here."); |       LOGGER.info(MSG); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -160,7 +162,7 @@ public class Account { | |||||||
|   public void handleEvent(AccountCreateEvent accountCreateEvent) { |   public void handleEvent(AccountCreateEvent accountCreateEvent) { | ||||||
|     AccountAggregate.putAccount(this); |     AccountAggregate.putAccount(this); | ||||||
|     if (accountCreateEvent.isRealTime()) { |     if (accountCreateEvent.isRealTime()) { | ||||||
|       LOGGER.info("Some external api for only realtime execution could be called here."); |       LOGGER.info(MSG); | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user