Here's a Java program that generates gift code vouchers using the specified pattern and character set:
package com.cloudtuned;
import java.util.Random;
public class GiftVoucherCodeGenerator {
// Define the pattern for the gift code
private static final String PATTERN = "####-####-####-####";
// Define the character set for the gift code
private static final String CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void main(String[] args) {
// Generate the gift code
String giftCode = generateGiftVoucherCode(PATTERN, CHARSET);
// Display the generated gift code
System.out.println("Generated Gift Code: " + giftCode);
}
public static String generateGiftVoucherCode(String pattern, String charset) {
StringBuilder sb = new StringBuilder();
Random random = new Random();
// Generate characters for each position in the pattern
for (int i = 0; i < pattern.length(); i++) {
char ch = pattern.charAt(i);
if (ch == '#') {
// If the character is '#', select a random character from the charset
int index = random.nextInt(charset.length());
sb.append(charset.charAt(index));
} else {
// If the character is not '#', use it as-is
sb.append(ch);
}
}
return sb.toString();
}
}
This program defines a GiftVoucherCodeGenerator
class with a generateGiftVoucherCode
method that takes a pattern and a character set as input and generates a gift code based on those parameters. The main method demonstrates how to use this method to generate a gift code with the specified pattern and character set and print it to the console.
Output:
/Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA.app/com.cloudtuned.GiftVoucherCodeGenerator
Generated Gift Code: 9NJ2-HN4X-TSZF-HCYE
Process finished with exit code 0
A simple unit test to check the code is correctly generating gift codes:
package com.cloudtuned;
import static org.junit.Assert.*;
import org.junit.Test;
public class GiftVoucherCodeGeneratorTest {
private static final String PATTERN = "####-####-####-####";
private static final String CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
@Test
public void testGenerateGiftCode() {
String giftCode = GiftVoucherCodeGenerator.generateGiftVoucherCode(PATTERN, CHARSET);
assertNotNull(giftCode);
assertEquals(19, giftCode.length());
// Check if the generated gift code matches the pattern
assertTrue(giftCode.matches("[0-9A-Z]{4}-[0-9A-Z]{4}-[0-9A-Z]{4}-[0-9A-Z]{4}"));
// Check if all characters in the gift code are present in the charset
for (char ch : giftCode.toCharArray()) {
// Skip the hyphen character
if (ch == '-') {
continue;
}
assertTrue(CHARSET.indexOf(ch) != -1);
}
}
}
Have ideas for enhancing the GiftVoucherCodeGenerator
program? Feel free to copy the code and implement your improvements! Here are a few ideas to get you started:
- Error Handling: Implement error handling to handle edge cases, such as invalid patterns or charsets.
- Parameterization: Allow users to specify custom patterns and charsets as input parameters to the
generateGiftVoucherCode
method. - More Parameterization: Allow users to define how many gift vouchers the program should generate.
- Efficiency: Explore ways to optimize the generation process for larger patterns or charsets.
- Unit Tests: Add additional unit tests to ensure the correctness and robustness of the code.
- Documentation: Improve code documentation to provide clearer explanations of the program's functionality and usage.
Feel free to experiment, innovate, and share your insights! Your contributions are valuable in making this program even better.
Feel free to share your improvements, suggestions, or any other feedback in the comments below! Your input is greatly appreciated. Happy coding! ๐