Class CommonFunction
java.lang.Object
com.glowmart.shop_management.common.CommonFunction
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic DoublecalculateDiscountAmount(Double originalPrice, Double discountPercentage) Calculates the discount amount based on the original price and discount percentage.static StringcreateFile(org.springframework.web.multipart.MultipartFile file, ProductDto productDto) Creates and saves a file to the "uploads" directory.static booleanValidates an ID string.static booleanisValidName(String name) Validates a name string.static booleanisValidPriceAndDiscount(Double originalPrice, Double discountPercentage) Validates product price and discount percentage.
-
Constructor Details
-
CommonFunction
public CommonFunction()
-
-
Method Details
-
isValidName
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:
trueif the name is valid,falseotherwise
-
isValidId
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:
trueif the ID is valid,falseotherwise
-
isValidPriceAndDiscount
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 pricediscountPercentage- the discount percentage to apply- Returns:
trueif both values are valid,falseotherwise
-
calculateDiscountAmount
Calculates the discount amount based on the original price and discount percentage.- Parameters:
originalPrice- the original product pricediscountPercentage- 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 saveproductDto- 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
-