Skip to content

Instantly share code, notes, and snippets.

/.cs

Created March 24, 2017 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/01ab55e06bccec33e9a2addc9d829a17 to your computer and use it in GitHub Desktop.
Save anonymous/01ab55e06bccec33e9a2addc9d829a17 to your computer and use it in GitHub Desktop.
Pizza Coding Challenge
//Models
namespace PizzaCodingChallenge.Model
{
public class PizzaModel
{
public string Name { get; set; }
public int Price { get; set; }
public List<string> Ingredients { get; set; }
}
}
//Models_for_outputJSON
namespace PizzaCodingChallenge.Model
{
public class JSONOutput
{
public JSONOutput()
{
Answer = new List<Model.Answer>();
}
[JsonProperty("personal_info")]
public PersonalInfo PersonalInfo { get; set; }
[JsonProperty("answer")]
public List<Answer> Answer { get; set; }
}
}
namespace PizzaCodingChallenge.Model
{
public class PersonalInfo
{
[JsonProperty("full_name")]
public string FullName { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("code_link")]
public string CodeLink { get; set; }
}
}
namespace PizzaCodingChallenge.Model
{
public class Answer
{
[JsonProperty("group")]
public Group Groups { get; set; }
}
}
namespace PizzaCodingChallenge.Model
{
public class Group
{
public Group()
{
Pizzas = new List<PizzaModel>();
}
[JsonProperty("percentage")]
public string Percentage { get; set; }
[JsonProperty("pizza")]
public List<PizzaModel> Pizzas { get; set; }
}
}
//Service //Methods
namespace PizzaCodingChallenge.Services
{
public class PizzaService
{
public static string SerializeJSON(JSONOutput j)
{
string pathProject = Directory.GetParent(Directory.GetParent(Environment.CurrentDirectory).FullName).FullName;
string finalPath = Path.Combine(pathProject, "JSON", "outputJSON.json");
var jsonString = JsonConvert.SerializeObject(j);
File.WriteAllText(finalPath, jsonString);
return jsonString;
}
public static List<PizzaModel> DeserializeJSON()
{
string pathProject = Directory.GetParent(Directory.GetParent(Environment.CurrentDirectory).FullName).FullName;
string finalPath = Path.Combine(pathProject, "JSON", "inputJson.json");
var jsonString = File.ReadAllText(finalPath);
var jsonObj = JsonConvert.DeserializeObject<dynamic>(jsonString);
var data = ((JArray)jsonObj.pizzas).Children();
List<PizzaModel> pizzas = new List<PizzaModel>();
for(int i=0; i<data.Count(); i++)
{
var name = data.ElementAt(i).First();
var price = data.ElementAt(i).Last();
if (data.ElementAt(i).First.First.HasValues)
{
List<string> ingList = new List<string>();
foreach (JValue ing in data.ElementAt(i).First.First.First.First)
{
ingList.Add(ing.Value.ToString());
}
pizzas.Add(new PizzaModel
{
Name = (name as JProperty).Name,
Price = (int)(price as JProperty).Value,
Ingredients = ingList
});
}
}
return pizzas;
}
public static List<PizzaModel> PizzasWithMeat(List<PizzaModel> pizzas)
{
List<PizzaModel> pizzasWithMeat = new List<PizzaModel>();
pizzasWithMeat = pizzas.Where(x => x.Ingredients.Where(y=>y.ToLower().IndexOf("meat") >= 0).Count() > 0 ).ToList();
return pizzasWithMeat;
}
public static List<PizzaModel> PizzasWithCheese(List<PizzaModel> pizzas)
{
List<PizzaModel> pizzasWithCheese = new List<Model.PizzaModel>();
pizzasWithCheese = pizzas.Where(x => x.Ingredients.Where(y => y.ToLower().IndexOf("cheese") >=0).Count() > 1).ToList();
return pizzasWithCheese;
}
public static List<PizzaModel> PizzasWithMeatAndOlives(List<PizzaModel> pizzas)
{
List<PizzaModel> pizzasWithMeatAndOlives = new List<PizzaModel>();
pizzasWithMeatAndOlives = PizzasWithMeat(pizzas);
pizzasWithMeatAndOlives = pizzasWithMeatAndOlives.Where(x => x.Ingredients.Where(y => y.ToLower().IndexOf("cheese") >= 0).Count() > 1).ToList();
return pizzasWithMeatAndOlives;
}
public static List<PizzaModel> PizzasWithMozzarellaAndMushrooms(List<PizzaModel> pizzas)
{
List<PizzaModel> pizzasWithMM = new List<PizzaModel>();
pizzasWithMM = pizzas.Where(x => x.Ingredients.Where(y => y.ToLower().IndexOf("mushrooms") >= 0).Count() > 0).ToList();
pizzasWithMM = pizzasWithMM.Where(x => x.Ingredients.Where(y => y.ToLower().IndexOf("mozzarella") >= 0).Count() > 0).ToList();
return pizzasWithMM;
}
public static void Post()
{
string pathProject = Directory.GetParent(Directory.GetParent(Environment.CurrentDirectory).FullName).FullName;
string finalPath = Path.Combine(pathProject, "JSON", "outputJson.json");
var jsonString = File.ReadAllText(finalPath);
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(jsonString);
WebRequest request = WebRequest.Create("http://coding-challenge.renderedtext.com/submit");
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
WebResponse response = request.GetResponse();
stream = response.GetResponseStream();
StreamReader sr = new StreamReader(stream);
Console.WriteLine(sr.ReadToEnd());
}
}
}
//Main_Program
namespace PizzaCodingChallenge
{
class Program
{
static void Main(string[] args)
{
List<PizzaModel> pizzas = new List<PizzaModel>();
pizzas = PizzaService.DeserializeJSON();
var pizzasWihtMeat = PizzaService.PizzasWithMeat(pizzas);
var pizzasWithCheese = PizzaService.PizzasWithCheese(pizzas);
var pizzasWithMeatAndOlives = PizzaService.PizzasWithMeatAndOlives(pizzas);
var pizzasWithMozzarellaAndMushrooms = PizzaService.PizzasWithMozzarellaAndMushrooms(pizzas);
double pizzasCount = Double.Parse(pizzas.Count.ToString());
double meatCount = Double.Parse(pizzasWihtMeat.Count.ToString());
double cheeseCount = Double.Parse(pizzasWithCheese.Count.ToString());
double meatOliveCount = Double.Parse(pizzasWithMeatAndOlives.Count.ToString());
double mAndM = Double.Parse(pizzasWithMozzarellaAndMushrooms.Count.ToString());
double numberMeat = (meatCount / pizzasCount) * 100;
double numberCheese = (cheeseCount / pizzasCount) * 100;
double numberMeatOlive = (meatOliveCount / pizzasCount) * 100;
double numberMAndM = (mAndM / pizzasCount) * 100;
JSONOutput json = new JSONOutput();
PersonalInfo p = new PersonalInfo();
p.FullName = "Stefan Djokic";
p.Email = "stefandjokic@elfak.rs";
p.CodeLink = "http://www.google.com";
json.PersonalInfo = p;
Group g1 = new Group();
Group g2 = new Group();
Group g3 = new Group();
Group g4 = new Group();
List<Answer> a = new List<Answer>();
json.Answer = a;
json.Answer.Add(new Answer { Groups = g1 });
json.Answer.Add(new Answer { Groups = g2 });
json.Answer.Add(new Answer { Groups = g3 });
json.Answer.Add(new Answer { Groups = g4 });
g1.Pizzas = pizzasWihtMeat;
g2.Pizzas = pizzasWithCheese;
g3.Pizzas = pizzasWithMeatAndOlives;
g4.Pizzas = pizzasWithMozzarellaAndMushrooms;
g1.Percentage = numberMeat.ToString() + "%";
g2.Percentage = numberCheese.ToString() + "%";
g3.Percentage = numberMeatOlive.ToString() + "%";
g4.Percentage = numberMAndM.ToString() + "%";
string v = PizzaService.SerializeJSON(json);
try
{
PizzaService.Post();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
Console.Read(); //To see Response from Server
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment