This is my current code, and as you can see, a big part of it is reoccurring code with just a few adjustments to it, for example the input, output name and the link. Is there any way I can bunch the two similar blocks of code together, but still get the same result.
I am going to make a few more similar to the purity fist and the backbiter's things in the future.
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();
}
}
There are a few differences between two if
statements:
if
clause ("pf" or "bb") num1 >= num2
AND userinput.Contains("pf")
or num1 >= num2
OR userinput.Contains("bb")
) Not sure why there exist the fourth difference, and why the cases are so strangely different, but I guess you know. Anyway, you can make a function that takes these differences as parameters, something like this: DoWork(bool containsSpecific, string urlPathEnding, string itemTitle, bool useOrInsteadOfAnd)
. And just call this function twice:
DoWork(userinput.Contains("pf"), "Purity%20Fist", "Genuine Purity Fist", false);
DoWork(userinput.Contains("bb"), "Backbiter%27s%20Billycock", "Backbiter's Billycock", true);
Last parameter is used at the end of the if (success == true)
block (btw, you should remove the == true
part, because it's redundant), but outside the if (num1 >= num2) .. else ..
part:
if (success == true)
{
if (num1 >= num2)
...
else
...
if ((!useOrInsteadOfAnd && num1 >= num2 && containsSpecific) ||
(useOrInsteadOfAnd && (num1 >= num2 || containsSpecific)))
{
Console.ReadKey();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.