Search code examples
javachaining

multiple method execution with same input processed


warm greetings.

I am having a scenario that i have 10 methods i.e. process1, process2..., process10 according to input i need to execute few methods out of 10 in specific order. 1)1,3,5,7 2)2,4,6,8 3)1,5,9,10

whatever order i am executing, output of 1st method should be used as the input of second method and so on.

i searched a lot in internet but didn't found anything suitable. I started doing below code. as of now i am using concat method of java to achieve this.

public class Perform {

    static String op = "";
    
    public static void main(String[] args) {
                process(list);
    }
    
    public static void process(List<String> list)
    {

        for(String str: list)
        {
            if(str.startsWith("Samsung"))
            {
process1("test").concat(process2(op).concat(process3(op)).concat(process4(op)));

            }
        }
    }
    
    public static String process1(String str)
    {
        System.out.println("process1 start");
        System.out.println("str1:"+str);
        System.out.println("process1 end");
        return str+"1";
    }
    public static String process2(String str)
    {
        System.out.println("process2 start");
        System.out.println("str2:"+str);
        System.out.println("process2 end");
        return str+"2";
    }
    public static String process3(String str)
    {
        System.out.println("process3 start");
        System.out.println("str3:"+str);
        System.out.println("process3 end");
        return str+"3";
    }
    public static String process4(String str)
    {
        System.out.println("process4 start");
        System.out.println("str4:"+str);
        System.out.println("process4 end");
        return str+"4";
    }
}

output :

process1 start

str1:test

process1 end

process2 start

str2:

op :

process2 end

process3 start

str3:

process3 end

process4 start

str4:

process4 end

op:test1234

the answer is expected and processing all 4 methods but the issue is : it is not printing the value of "str" which is the parameter. why ?

if i am keeping the code

op = process1("test");
op = process2(op);
op = process3(op);
op = process4(op);

in place of

process1("test").concat(process2(op).concat(process3(op)).concat(process4(op)));

then it is printing the value of str which is an parameter.

Please answer the question from below code as well as suggest me some good code for this scenario.


Solution

  • I have found the solution. I developed below code to solve it. Please let me know if anybody is having a better solution for this.

    public class Perform {
    
        private static List<Integer> SamsungMethodExecution = Arrays.asList(1,3,5,7);
        private static List<Integer> NokiaMethodExecution = Arrays.asList(2,4,6,8);
        private static List<Integer> LgMethodExecution = Arrays.asList(1,5,9,10);
        
        
        public static void main(String[] args) {
            List<String> list = new ArrayList<>();
            
            list.add("Lgmsg");
            list.add("Samsungmsg");
            list.add("Nokiamsg");
            list.add("Lgmsg");
            list.add("Samsungmsg");
            list.add("Nokiamsg");
            list.add("Lgmsg");
            list.add("Samsungmsg");
            list.add("Nokiamsg");
    //      
            process(list);
        }
        
        public static void process(List<String> list)
        {
            for(String str: list)
            {
                if(str.startsWith("Samsung"))
                {
                    System.out.println("SAMSUNG======================================");
                    Process.process(SamsungMethodExecution, str.replace("Samsung", ""));
                    System.out.println("SAMSUNG======================================");
                    System.out.println();
                }else if(str.startsWith("Nokia"))
                {
                    System.out.println("NOKIA========================================");
                    Process.process(NokiaMethodExecution, str.replace("Nokia", ""));
                    System.out.println("NOKIA========================================");
                    System.out.println();
                }else
                {
                    System.out.println("LG===========================================");
                    Process.process(LgMethodExecution, str.replace("Lg", ""));
                    System.out.println("LG===========================================");
                    System.out.println();
                }
            }
        }
    
    public class Process {
        
        public static String process(List<Integer> processList, String msg) {
            String output = "";
            for(int i: processList)
            {
                switch(i)
                {
                    case 1:
                        output = Process.process1(msg); break;
                    case 2:
                        output = Process.process2(msg); break;
                    case 3:
                        output = Process.process3(msg); break;
                    case 4:
                        output = Process.process4(msg); break;
                    case 5:
                        output = Process.process5(msg); break;
                    case 6:
                        output = Process.process6(msg); break;
                    case 7:
                        output = Process.process7(msg); break;
                    case 8:
                        output = Process.process8(msg); break;
                    case 9:
                        output = Process.process9(msg); break;
                    case 10:
                        output = Process.process10(msg); break;
                    default :
                        System.out.println("Sorry, no specific match found");
                }
            }
            return output;
        }
    
        public static String process1(String msg)
        {
            System.out.println("process1: "+msg);
            return msg;
        }
        public static String process2(String msg)
        {
            System.out.println("process2: "+msg);
            return msg;
        }
        public static String process3(String msg)
        {
            System.out.println("process3: "+msg);
            return msg;
        }
        public static String process4(String msg)
        {
            System.out.println("process4: "+msg);
            return msg;
        }
        public static String process5(String msg)
        {
            System.out.println("process5: "+msg);
            return msg;
        }
        public static String process6(String msg)
        {
            System.out.println("process6: "+msg);
            return msg;
        }
        public static String process7(String msg)
        {
            System.out.println("process7: "+msg);
            return msg;
        }
        public static String process8(String msg)
        {
            System.out.println("process8: "+msg);
            return msg;
        }
        public static String process9(String msg)
        {
            System.out.println("process9: "+msg);
            return msg;
        }
        public static String process10(String msg)
        {
            System.out.println("process10: "+msg);
            return msg;
        }
    }
    

    Two classes one is main class and another is Process class which is having a actual logic.