Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions Utility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import java.util.regex.Pattern;

public class Utility {

private static final String EMAIL_REGEX = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEX);


public static boolean isValidEmail(String email) {
return EMAIL_PATTERN.matcher(email).matches();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Add a null check in isValidEmail to safely handle null inputs, avoiding potential NullPointerExceptions. [possible bug]

Suggested change
return EMAIL_PATTERN.matcher(email).matches();
if (email == null) {
return false;
}
return EMAIL_PATTERN.matcher(email).matches();

}


public static int add(int a, int b) {
return a + b;
}


public static int divide(int a, int b) {
if (b == 0) {
System.out.println("Warning: Division by zero. Returning 0 instead.");
return 0;
}
return a / b;
}


public static double calculateDiscountedPrice(double price, double discount) {
double discountedPrice = price - (price * discount / 100);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Validate the discount parameter range before calculating the discounted price. [possible bug]

Suggested change
double discountedPrice = price - (price * discount / 100);
if (discount < 0 || discount > 100) { LOGGER.warning("Invalid discount value: " + discount); return price; }
double discountedPrice = price - (price * discount / 100);

return discountedPrice;
}

public static String reverseString(String input) {
if (input == null) {
System.out.println("Warning: Input is null. Returning null.");
return null;
Comment on lines +34 to +36
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Change the null check in reverseString to throw an exception instead of printing a warning and returning null, ensuring errors are handled explicitly. [business logic]

Suggested change
if (input == null) {
System.out.println("Warning: Input is null. Returning null.");
return null;
if (input == null) {
throw new IllegalArgumentException("Input cannot be null.");
}

}
return new StringBuilder(input).reverse().toString();
}

public static long factorial(int n) {
if (n < 0) {
System.out.println("Warning: Factorial of negative number. Returning -1.");
return -1;
Comment on lines +42 to +44
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Throw an exception for negative factorial inputs instead of printing a warning and returning a sentinel value. [business logic]

Suggested change
if (n < 0) {
System.out.println("Warning: Factorial of negative number. Returning -1.");
return -1;
if (n < 0) {
throw new IllegalArgumentException("Factorial is undefined for negative numbers.");
}

}
long result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
Comment on lines +47 to +48
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Insert an overflow check in the factorial loop to prevent incorrect results for large inputs. [possible bug]

Suggested change
for (int i = 1; i <= n; i++) {
result *= i;
for (int i = 1; i <= n; i++) {
if (Long.MAX_VALUE / result < i) {
LOGGER.warning("Overflow detected in factorial.");
return -1;
}
result *= i;
}

}
return result;
}

public static int findMax(int[] numbers) {
if (numbers == null || numbers.length == 0) {
System.out.println("Warning: Input array is null or empty. Returning Integer.MIN_VALUE.");
return Integer.MIN_VALUE;
Comment on lines +54 to +56
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Replace the System.out.println warning in findMax with an exception for null or empty arrays to avoid returning misleading sentinel values. [business logic]

Suggested change
if (numbers == null || numbers.length == 0) {
System.out.println("Warning: Input array is null or empty. Returning Integer.MIN_VALUE.");
return Integer.MIN_VALUE;
if (numbers == null || numbers.length == 0) {
throw new IllegalArgumentException("Input array cannot be null or empty.");
}

}

int max = numbers[0];
for (int number : numbers) {
if (number > max) {
max = number;
}
}
return max;
}
}