Class CommonFunction

java.lang.Object
com.glowmart.shop_management.common.CommonFunction

public class CommonFunction extends Object
A utility class that provides common validation and helper functions used across the application.

This class includes methods for validating user input (names, IDs, prices), calculating discounts, and handling file uploads.

  • Constructor Details

    • CommonFunction

      public CommonFunction()
  • Method Details

    • isValidName

      public static boolean isValidName(String name)
      Validates a name string.

      The name must not be null or empty, must not consist only of digits, and must contain only letters and spaces (no special characters).

      Parameters:
      name - the name string to validate
      Returns:
      true if the name is valid, false otherwise
    • isValidId

      public static boolean isValidId(String input)
      Validates an ID string.

      The ID must not be null or empty, must contain only digits, and must represent a positive number.

      Parameters:
      input - the ID string to validate
      Returns:
      true if the ID is valid, false otherwise
    • isValidPriceAndDiscount

      public static boolean isValidPriceAndDiscount(Double originalPrice, Double discountPercentage)
      Validates product price and discount percentage.

      The original price must be greater than zero, and the discount percentage must be between 0 and 100 inclusive.

      Parameters:
      originalPrice - the original product price
      discountPercentage - the discount percentage to apply
      Returns:
      true if both values are valid, false otherwise
    • calculateDiscountAmount

      public static Double calculateDiscountAmount(Double originalPrice, Double discountPercentage)
      Calculates the discount amount based on the original price and discount percentage.
      Parameters:
      originalPrice - the original product price
      discountPercentage - the discount percentage to apply
      Returns:
      the calculated discount amount
    • createFile

      public static String createFile(org.springframework.web.multipart.MultipartFile file, ProductDto productDto) throws IOException
      Creates and saves a file to the "uploads" directory.

      The file is saved with a unique name generated by combining a UUID with the original filename. If the "uploads" directory does not exist, it will be created automatically.

      Parameters:
      file - the multipart file to save
      productDto - the product DTO associated with the file (currently unused)
      Returns:
      the generated unique filename
      Throws:
      IOException - if an I/O error occurs while saving the file