Skip to content

Commit

Permalink
Refactoring utils class
Browse files Browse the repository at this point in the history
  • Loading branch information
kaizerpwn committed May 12, 2024
1 parent 2485c9c commit d310f63
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,12 @@ private void processUserInput(String userInput) {
return;
}

if (!Utils.checkProductQuantity(product.get(), quantity)) {
if (Utils.checkProductQuantity(product.get(), quantity)) {
System.out.println("ERROR: Quantity available for that product is '" + product.get().getAvailableQuantity() + "', not '" + quantity + "'.");
return;
}

if (!Utils.checkProductAvailability(product.get())) {
if (Utils.checkProductAvailability(product.get())) {
System.out.println("ERROR: That product is not currently available, it is available from the date " + product.get().getAvailableFrom() + " to "+ product.get().getAvailableUntil() + ".");
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ public Optional<OrderDto> createNewOrder(@Valid OrderDto orderDto) {

if (productItem.isPresent()) {
if(orderItemDto.getQuantity() != 0) {
if (!Utils.checkProductQuantity(productItem.get(), orderItemDto.getQuantity())) {
if (Utils.checkProductQuantity(productItem.get(), orderItemDto.getQuantity())) {
throw new RuntimeException("Quantity available for product "+ productItem.get().getProductId() +" is '" + productItem.get().getAvailableQuantity() + "', not '" + orderItemDto.getQuantity() + "'.");
}
if (!Utils.checkProductAvailability(productItem.get())) {
if (Utils.checkProductAvailability(productItem.get())) {
throw new RuntimeException("Product "+ productItem.get().getProductId() +" is not currently available, it is available from the date " + productItem.get().getAvailableFrom() + " to "+ productItem.get().getAvailableUntil() + ".");
}

Expand Down
16 changes: 6 additions & 10 deletions app/src/main/java/com/ibrahimokic/ordermanagement/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@ public static boolean checkIfUserIdIsDifferent(User user, Order retrievedOrderFr
}

public static boolean checkIfAddressIsDifferent(Address address, AddressDto addressDto) {
if (!Objects.equals(address.getStreet(), addressDto.getStreet()) ||
!Objects.equals(address.getCity(), addressDto.getCity()) ||
!Objects.equals(address.getZip(), addressDto.getZip()) ||
!Objects.equals(address.getCountry(), addressDto.getCountry())) {
return true;
}
return false;
return !Objects.equals(address.getStreet(), addressDto.getStreet()) ||
!Objects.equals(address.getCity(), addressDto.getCity()) ||
!Objects.equals(address.getZip(), addressDto.getZip()) ||
!Objects.equals(address.getCountry(), addressDto.getCountry());
}

public static boolean checkIfOrderItemsAreDifferent(List<OrderItem> retrievedOrderItems, List<OrderItem> updatedOrderItems) {
Expand Down Expand Up @@ -82,12 +79,11 @@ public static boolean checkProductAvailability(Product product) {
LocalDate availableFrom = product.getAvailableFrom();
LocalDate availableUntil = product.getAvailableUntil();

return currentDate.isAfter(availableFrom) && currentDate.isBefore(availableUntil);
return !currentDate.isAfter(availableFrom) || !currentDate.isBefore(availableUntil);
}

public static boolean checkProductQuantity(Product product, int quantity) {
if (product.getAvailableQuantity() < quantity) return false;
return true;
return product.getAvailableQuantity() < quantity;
}

public static int getValidInput(Scanner scanner, int max) {
Expand Down

0 comments on commit d310f63

Please sign in to comment.