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:
kanwarpreet25 2019-07-28 18:12:03 +05:30 committed by Ilkka Seppälä
parent c6ecf58687
commit f7e22a1cf6
3 changed files with 72 additions and 55 deletions

View File

@ -60,7 +60,8 @@ public class App {
/**
* Program entry point.
*
* @param args command line args
* @param args
* command line args
*/
public static void main(String[] args) {
App app = new App();
@ -150,56 +151,11 @@ public class App {
option = s.nextInt();
if (option == 1) {
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.");
}
processOption1(eventManager, s);
} else if (option == 2) {
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());
}
processOption2(eventManager, s);
} else if (option == 3) {
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();
}
processOption3(eventManager, s);
} else if (option == 4) {
eventManager.shutdown();
}
@ -208,4 +164,61 @@ public class App {
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.");
}
}
}

View File

@ -39,6 +39,8 @@ public class EventManager implements ThreadCompleteListener {
private int currentlyRunningSyncEvent = -1;
private Random rand;
private Map<Integer, Event> eventPool;
private static final String DOES_NOT_EXIST = " does not exist.";
/**
* EventManager constructor.
@ -112,7 +114,7 @@ public class EventManager implements ThreadCompleteListener {
*/
public void start(int eventId) throws EventDoesNotExistException {
if (!eventPool.containsKey(eventId)) {
throw new EventDoesNotExistException(eventId + " does not exist.");
throw new EventDoesNotExistException(eventId + DOES_NOT_EXIST);
}
eventPool.get(eventId).start();
@ -126,7 +128,7 @@ public class EventManager implements ThreadCompleteListener {
*/
public void cancel(int eventId) throws EventDoesNotExistException {
if (!eventPool.containsKey(eventId)) {
throw new EventDoesNotExistException(eventId + " does not exist.");
throw new EventDoesNotExistException(eventId + DOES_NOT_EXIST);
}
if (eventId == currentlyRunningSyncEvent) {
@ -145,7 +147,7 @@ public class EventManager implements ThreadCompleteListener {
*/
public void status(int eventId) throws EventDoesNotExistException {
if (!eventPool.containsKey(eventId)) {
throw new EventDoesNotExistException(eventId + " does not exist.");
throw new EventDoesNotExistException(eventId + DOES_NOT_EXIST);
}
eventPool.get(eventId).status();

View File

@ -44,6 +44,8 @@ public class Account {
private final int accountNo;
private final String owner;
private BigDecimal money;
private static final String MSG = "Some external api for only realtime execution could be called here.";
/**
* Instantiates a new Account.
@ -126,7 +128,7 @@ public class Account {
depositMoney(money);
AccountAggregate.putAccount(this);
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);
AccountAggregate.putAccount(this);
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) {
AccountAggregate.putAccount(this);
if (accountCreateEvent.isRealTime()) {
LOGGER.info("Some external api for only realtime execution could be called here.");
LOGGER.info(MSG);
}
}