Is there a better way to write this method in Java - Random Generator
I have this method that takes in an array of spinner values. It takes the values and adds them to a prefix with 9 random digits.
So far, I have quite a lot of prefixes that go along with each spinner value.
Every value is checking if it's greater than 0, looping over the amount of values then adding the prefix + 9 digits. e.g R1 spinner value = 10, then an array gets spit out with R1+9DIGITS 10 times
There are so many if statements, and I'm sure there would be a better way. You an see below its extremely repetitive. Any help or ideas would be appreciated. Thanks!
Spinner<Integer>[] spinners = new Spinner[]{
r1Spinner, r2Spinner, r3Spinner, r5Spinner, rd2Spinner, rd8Spinner,m1Spinner,gSpinner, srSpinner, lcSpinner, singleURNSpinner,
customPrefixSpinner, prefixSpinner};
public static String[] randomPrefixGenerator(int[] values){
int R1 = values[0];
int R2 = values[1];
int R3 = values[2];
int R5 = values[3];
int RD2 = values[4];
int RD8 = values[5];
int M1 = values[6];
int G = values[7];
int SR = values[8];
int LC = values[9];
String[] urnR1 = new String[R1];
String[] urnR2 = new String[R2];
String[] urnR3 = new String[R3];
String[] urnR5 = new String[R5];
String[] urnRD2 = new String[RD2];
String[] urnRD8 = new String[RD8];
String[] urnG = new String[G];
String[] urnM1 = new String[M1];
String[] urnLC = new String[LC];
String[] urnSR = new String[SR];
if (R1 >= 0){
for (int i = 0; i < R1 ; i++) {
urnR1[i] = "R1" + random9digits();
}
}
if (R2 >= 0){
for (int i = 0; i < R2 ; i++) {
urnR2[i] = "R2" + random9digits();
}
}
if (R3 >= 0){
for (int i = 0; i < R3 ; i++) {
urnR3[i] = "R3" + random9digits();
}
}
if (R5 >= 0){
for (int i = 0; i < R5 ; i++) {
urnR5[i] = "R5" + random9digits();
}
}
if (RD2 >= 0){
for (int i = 0; i < RD2 ; i++) {
urnRD2[i] = "RD2" + random9digits();
}
}
if (RD8 >= 0){
for (int i = 0; i < RD8 ; i++) {
urnRD8[i] = "RD8" + random9digits();
}
}
if (M1 >= 0){
for (int i = 0; i < M1 ; i++) {
urnM1[i] = "M1" + random9digits();
}
}
if (G >= 0){
for (int i = 0; i < G ; i++) {
urnG[i] = "G" + random9digits();
}
}
if (LC >= 0){
for (int i = 0; i < LC ; i++) {
urnLC[i] = "LC" + random9digits();
}
}
if (SR >= 0){
for (int i = 0; i < SR ; i++) {
urnSR[i] = "SR" + random9digits();
}
}
return Arrays.stream(mergeArray(urnR1,urnR2, urnR3,urnR5, urnRD2, urnRD8, urnG, urnM1, urnLC, urnSR)).toArray(String[]::new);
}
from Recent Questions - Stack Overflow https://ift.tt/3FJJmgc
https://ift.tt/eA8V8J
Comments
Post a Comment