Created with <3 with dartpad.dev.
Last active
January 29, 2024 08:38
-
-
Save ANNASBlackHat/8288eecc31dde5f5684d9d512bfeb871 to your computer and use it in GitHub Desktop.
Generate Unique Sales Id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
class Generator { | |
static String generateSalesId() { | |
List<String> words = [ | |
"A", | |
"P", | |
"L", | |
"I", | |
"K", | |
"U", | |
"N", | |
"Q", | |
"D", | |
"E" | |
]; | |
String time = DateTime.now().millisecondsSinceEpoch.toString(); | |
List<String> timeChars = time.split(""); | |
List<int> changes = []; | |
int max = timeChars.length; | |
int min = 1; | |
for (int i = 0; i < timeChars.length ~/ 2; i++) { | |
int index = Random().nextInt(max - min + 1) + min; | |
if (!changes.contains(index)) changes.add(index); | |
} | |
StringBuffer value = StringBuffer(); | |
for (int i = 0; i < timeChars.length; i++) { | |
if (timeChars[i] != '') { | |
if (changes.contains(i)) { | |
value.write(words[int.parse(timeChars[i])]); | |
} else { | |
value.write(timeChars[i]); | |
} | |
} | |
} | |
return value.toString(); | |
} | |
} | |
void main() { | |
print('SalesId: ${Generator.generateSalesId()}'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment