如何使重复出现的代码程序更有效?

时间:2015-11-21 15:30:27

标签: c#

这是我当前的代码,正如您所看到的,其中很大一部分是重复发生的代码,只需对其进行一些调整,例如输入,输出名称和链接。有什么方法可以将两个相似的代码块组合在一起,但仍然得到相同的结果。

我将在未来做一些类似纯度拳头和后退者的事情。

        if (userinput.Contains("pf")||userinput.Contains("all"))
        {

            //PURITY FIST
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://steamcommunity.com/market/priceoverview/?currency=3&appid=440&market_hash_name=Genuine%20Purity%20Fist");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader stream = new StreamReader(response.GetResponseStream());
            var final_response = stream.ReadToEnd();


            // Converts the unicode to string correctValue.
            string correctValue = "Euro";
            StringBuilder sb = new StringBuilder(final_response);
            if (sb.ToString().Contains("\\u20ac"))
            {
                sb.Replace("\\u20ac", correctValue);
            }

            dynamic items = JObject.Parse(sb.ToString());

            bool success = items.success;
            string lowest = items.lowest_price;
            string volume = items.volume;
            string median = items.median_price;

            // Create a test object of RootObject class
            RootObject r = new RootObject(success, lowest, volume, median);

            // Calculation example
            double num1 = double.Parse(r.FixComma(r.lowest_price, correctValue), CultureInfo.InvariantCulture);
            double num2 = double.Parse(r.FixComma(r.median_price, correctValue), CultureInfo.InvariantCulture);



            Console.WriteLine("Genuine Purity Fist");

            if (success == true)
            {
                if (num1 >= num2)
                {
                    Console.WriteLine(r.median_price);
                    Console.WriteLine();
                    if (userinput.Contains("pf"))
                    {
                        Console.ReadKey();
                    }
                }

                else
                {
                    Console.WriteLine(r.lowest_price);
                    Console.WriteLine();
                }
            }
        }

    if (userinput.Contains("bb")||userinput.Contains("all"))
    {
        //BACKBITER'S BILLYCOCK
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://steamcommunity.com/market/priceoverview/?currency=3&appid=440&market_hash_name=Genuine%20Backbiter%27s%20Billycock");
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader stream = new StreamReader(response.GetResponseStream());
        var final_response = stream.ReadToEnd();


        // Converts the unicode to string correctValue.
        string correctValue = "Euro";
        StringBuilder sb = new StringBuilder(final_response);
        if (sb.ToString().Contains("\\u20ac"))
        {
            sb.Replace("\\u20ac", correctValue);
        }

        dynamic items = JObject.Parse(sb.ToString());

        bool success = items.success;
        string lowest = items.lowest_price;
        string volume = items.volume;
        string median = items.median_price;

        // Create a test object of RootObject class
        RootObject r = new RootObject(success, lowest, volume, median);

        // Calculation example
        double num1 = double.Parse(r.FixComma(r.lowest_price, correctValue), CultureInfo.InvariantCulture);
        double num2 = double.Parse(r.FixComma(r.median_price, correctValue), CultureInfo.InvariantCulture);



        Console.WriteLine("Backbiter's Billycock");

        if (success == true)
        {
            if (num1 >= num2)
            {
                Console.WriteLine(r.median_price);
                Console.WriteLine();
                Console.ReadKey();
            }

            else
            {
                Console.WriteLine(r.lowest_price);
                Console.WriteLine();
                if (userinput.Contains("bb"))
                {
                    Console.ReadKey();
                }
            }
        }
    }

            else
            {
                Console.WriteLine("Item not found!");
                Main(args);
            }
        }
    }
}





public class RootObject
{
    public bool success { get; set; }
    public string lowest_price { get; set; }
    public string volume { get; set; }
    public string median_price { get; set; }

    public RootObject(bool success, string lowest_price, string volume, string median_price)
    {
        this.success = success;
        this.lowest_price = lowest_price;
        this.volume = volume;
        this.median_price = median_price;
    }

    public string FixComma(string value, string currency)
    {
        string correctValue = ".";
        string correctValue2 = "";
        StringBuilder sb = new StringBuilder(value);
        if (sb.ToString().Contains(","))
        {
            sb.Replace(",", correctValue);
        }
        if (sb.ToString().Contains(currency))
        {
            sb.Replace(currency, correctValue2);
        }
        return sb.ToString();
    }
}

1 个答案:

答案 0 :(得分:0)

两个if语句之间存在一些差异:

  1. if子句的字符串(" pf"或" bb")
  2. URL的字符串(以&#34结尾; Purity%20Fist"或" Backbiter%27s%20Billycock")
  3. Console.WriteLine的字符串("真正的纯净拳头"或者#34; Backbiter&#B; Billycock")
  4. Console.ReadKey的条件(num1 >= num2userinput.Contains("pf")num1 >= num2userinput.Contains("bb")
  5. 不确定为什么存在第四个差异,为什么案件如此奇怪地不同,但我想你知道。无论如何,你可以创建一个将这些差异作为参数的函数,如下所示:DoWork(bool containsSpecific, string urlPathEnding, string itemTitle, bool useOrInsteadOfAnd)。只需调用此函数两次:

    DoWork(userinput.Contains("pf"), "Purity%20Fist", "Genuine Purity Fist", false);
    DoWork(userinput.Contains("bb"), "Backbiter%27s%20Billycock", "Backbiter's Billycock", true);
    

    最后一个参数在if (success == true)块的末尾使用(顺便说一下,您应该删除== true部分,因为它是多余的),但在if (num1 >= num2) .. else ..部分之外:

        if (success == true)
        {
            if (num1 >= num2)
               ...
            else
               ...
    
            if ((!useOrInsteadOfAnd && num1 >= num2 && containsSpecific) ||
               (useOrInsteadOfAnd && (num1 >= num2 || containsSpecific)))
            {
                Console.ReadKey();
            }
        }