Home > Enterprise >  Is there a better way to write this method in Java - Random Generator
Is there a better way to write this method in Java - Random Generator

Time:11-26

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);
    }

CodePudding user response:

I don't know what the merge arrays function does. But i think you can optmizie your code a bit using the code below:

public static String[] randomPrefixGenerator(int[] values) {
        
        // we can use a map to retrieve your prefixes by their index
        Map<Integer, String> prefixNameByIndex = Map.of(
                0, "R1",
                1, "R2",
                2, "R3",
                3, "R5",
                4, "RD2",
                5, "RD8",
                6, "M1",
                7, "G",
                8, "SR",
                9, "LC"
        );
        
        // this stream goes through value 0 to 9
        return IntStream.rangeClosed(0, 9)
                // we need objects for the flatmap to work
                .boxed()
                // flatmap so we get a singe list of strings
                .flatMap(generatePrefixNamesForIndex(values, prefixNameByIndex))
                .collect(Collectors.toList())
                // the to array call as last
                .toArray(String[]::new);
        
}

private static Function<Integer, Stream<? extends String>> generatePrefixNamesForIndex(int[] values, Map<Integer, String> prefixNameByIndex) {
        return index -> {
            // we can retrieve the prefix from the map by its index
            var prefixValue = prefixNameByIndex.get(index);
            // retrieve te number of iterations from parameters given in the method call
            return IntStream.range(0, values[index])
                    .mapToObj(i -> prefixValue   random9digits());
        };
}

CodePudding user response:

Updated

I ended up thinking of a solution.


private static String[] urnCounter(String[] urns){
        String[] urnsProcessed = new String[Integer.parseInt(urns[0])];

            if (Integer.parseInt(urns[0]) >= 0){
                for (int j = 0; j < Integer.parseInt(urns[0]); j  ) {
                    urnsProcessed[j] = urns[1]   random9Digits();
                }
            }
        return urnsProcessed;
    }
    public static String[] randomURNGenerator1(int[] values){

        String[] urnR1 = urnCounter(new String[]{String.valueOf(values[0]),"R1"});
        String[] urnR2 = urnCounter(new String[]{String.valueOf(values[1]),"R2"});
        String[] urnR3 = urnCounter(new String[]{String.valueOf(values[2]),"R3"});
        String[] urnR5 = urnCounter(new String[]{String.valueOf(values[3]),"R5"});
        String[] urnRD2 = urnCounter(new String[]{String.valueOf(values[4]),"RD2"});
        String[] urnRD8 = urnCounter(new String[]{String.valueOf(values[5]),"RD8"});
        String[] urnG = urnCounter(new String[]{String.valueOf(values[7]),"G"});
        String[] urnM1 = urnCounter(new String[]{String.valueOf(values[6]),"M1"});
        String[] urnLC = urnCounter(new String[]{String.valueOf(values[9]),"LC"});
        String[] urnSR = urnCounter(new String[]{String.valueOf(values[8]),"SR"});

        return Arrays.stream(mergeArray(urnR1,urnR2, urnR3,urnR5, urnRD2, urnRD8, urnG, urnM1, urnLC, urnSR)).toArray(String[]::new);
    }
    // function to merge two arrays
    @SafeVarargs
    public static <T> Object[] mergeArray(T[]... arr) {
        Object[] array = Stream.of().flatMap(Stream::of).toArray();
        for(T[] passedObject: arr){
            array = Stream.of(array, passedObject).flatMap(Stream::of).toArray(String[]::new);
        }
        return array;
    }

  • Related