多个相同输入的方法执行已处理。

huangapple go评论27阅读模式
英文:

multiple method execution with same input processed

问题

热忱的问候。

我有一个场景,我有10个方法,即process1、process2...,process10,根据输入,我需要按特定顺序执行这些方法中的几个。

  1. 1,3,5,7
  2. 2,4,6,8
  3. 1,5,9,10

无论我执行什么顺序,第一个方法的输出都应该作为第二个方法的输入,依此类推。

我在互联网上搜索了很多,但没有找到合适的东西。我开始编写下面的代码。目前我正在使用Java的concat方法来实现这一点。

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

输出:

process1 start
str1:test
process1 end
process2 start
str2:
op :
process2 end
process3 start
str3:
process3 end
process4 start
str4:
process4 end
op:test1234

问题是:期望的答案正在处理所有4个方法,但问题是:它没有打印参数“str”的值。为什么?

如果我将代码中的:

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

替换为:

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

那么它会打印参数“str”的值。

请回答下面的代码问题,同时建议我一些适用于这种情况的好代码。

英文:

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 = &quot;&quot;;
public static void main(String[] args) {
process(list);
}
public static void process(List&lt;String&gt; list)
{
for(String str: list)
{
if(str.startsWith(&quot;Samsung&quot;))
{
process1(&quot;test&quot;).concat(process2(op).concat(process3(op)).concat(process4(op)));
}
}
}
public static String process1(String str)
{
System.out.println(&quot;process1 start&quot;);
System.out.println(&quot;str1:&quot;+str);
System.out.println(&quot;process1 end&quot;);
return str+&quot;1&quot;;
}
public static String process2(String str)
{
System.out.println(&quot;process2 start&quot;);
System.out.println(&quot;str2:&quot;+str);
System.out.println(&quot;process2 end&quot;);
return str+&quot;2&quot;;
}
public static String process3(String str)
{
System.out.println(&quot;process3 start&quot;);
System.out.println(&quot;str3:&quot;+str);
System.out.println(&quot;process3 end&quot;);
return str+&quot;3&quot;;
}
public static String process4(String str)
{
System.out.println(&quot;process4 start&quot;);
System.out.println(&quot;str4:&quot;+str);
System.out.println(&quot;process4 end&quot;);
return str+&quot;4&quot;;
}
}

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(&quot;test&quot;);
op = process2(op);
op = process3(op);
op = process4(op);

in place of

process1(&quot;test&quot;).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.

答案1

得分: 0

我已找到解决方案。我开发了以下代码来解决这个问题。如果有人有更好的解决方案,请告诉我。

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("对不起,找不到特定的匹配");
            }
        }
        return output;
    }

    // 各个 processX 方法的定义
}

两个类,一个是主类(Perform),另一个是实际逻辑的过程类(Process)。

英文:

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

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

huangapple
  • 本文由 发表于 2020年9月16日 20:29:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63920107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定