Created
June 23, 2019 04:24
-
-
Save andrewloable/6e26f2a9b9ebb4dd4eef7533f3cd119f to your computer and use it in GitHub Desktop.
Olo - Pizza Exercise - c# .net code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
namespace test_pizza | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string url = @"http://files.olo.com/pizzas.json"; | |
int maxRecords = 20; | |
using (WebClient wc = new WebClient()) | |
{ | |
var json = wc.DownloadString(url); | |
var orders = JsonConvert.DeserializeObject<orders[]>(json); | |
var result = orders.GroupBy(r => r.concatToppings) | |
.Select(r => new | |
{ | |
toppings = r.Key, | |
count = r.Count() | |
}) | |
.OrderByDescending(r => r.count).ToList(); | |
Console.WriteLine("{0,-10} \t| {0,-50} \t| {2,-10}", "Rank", "Toppings", "Order Count"); | |
for(int i=0; i<maxRecords; i++) | |
{ | |
string disp = string.Format("{0,-10} \t| {1,-50} \t| {2,-10}", (i + 1), result[i].toppings, result[i].count); | |
Console.WriteLine(disp); | |
} | |
} | |
Console.WriteLine("Press enter to continue..."); | |
Console.ReadLine(); | |
} | |
} | |
public class orders | |
{ | |
public string[] toppings { get; set; } | |
public string concatToppings | |
{ | |
get | |
{ | |
var sorted = toppings.OrderBy(r => r); | |
return string.Join(", ", toppings); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment