Advertisement
Pazzobg

ProgrammingFundamentals-Lambda&LINQ-SrabskoUnleashed_3

Jun 8th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.83 KB | None | 0 0
  1. namespace _10.SrybskoUnleashed
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public class SrybskoUnleashed
  8.     {
  9.         public static void Main()
  10.         {
  11.             var venuesDict = new Dictionary<string, Dictionary<string, long>>();
  12.  
  13.             string[] input = Console.ReadLine().Trim().Split(
  14.                 new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  15.  
  16.             while (input[0] != "End")
  17.             {
  18.                 bool foundAt = false;
  19.                 for (int i = 0; i < input.Length; i++)
  20.                 {
  21.                     if (input[i].Contains("@"))
  22.                     {
  23.                         foundAt = true;
  24.                         break;
  25.                     }
  26.                 }
  27.  
  28.                 if (!foundAt)
  29.                 {
  30.                     input = Console.ReadLine().Trim().Split(
  31.                         new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  32.                     continue;
  33.                 }
  34.  
  35.                 int indexSign = 0;
  36.                 //Check wether there's an '@'
  37.                 indexSign = DefineIndexPossition(input, indexSign);
  38.  
  39.                 //Check if the Singer Name is valid
  40.                 if (indexSign <= 1 || indexSign >= 5)
  41.                 {
  42.                     input = Console.ReadLine().Trim().Split(
  43.                         new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  44.                     continue;
  45.                 }
  46.                 if (input.Length - indexSign >= 5 || input.Length - indexSign < 2)
  47.                 {
  48.                     input = Console.ReadLine().Trim().Split(
  49.                         new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  50.                     continue;
  51.                 }
  52.  
  53.                 long outValue = 0;
  54.                 bool lastIsNumber = long.TryParse(input[input.Length - 1], out outValue);
  55.                 bool forelastIsNumber = long.TryParse(input[input.Length - 2], out outValue);
  56.                 if (!lastIsNumber || !forelastIsNumber)
  57.                 {
  58.                     input = Console.ReadLine().Trim().Split(
  59.                         new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  60.                     continue;
  61.                 }
  62.  
  63.                 if (input.Length >= 4)
  64.                 {
  65.                     string artist = GetArtist(input);
  66.  
  67.                     if (artist.Split(' ').Length > 3)
  68.                     {
  69.                         input = Console.ReadLine().Split(' ');
  70.                         continue;
  71.                     }
  72.  
  73.                     string venue = GetVenue(input);
  74.  
  75.                     if (venue.Split(' ').Length > 3)
  76.                     {
  77.                         input = Console.ReadLine().Split(' ');
  78.                         continue;
  79.                     }
  80.  
  81.                     string ticketsSold = input[input.Length - 1];
  82.                     string ticketPrice = input[input.Length - 2];
  83.                     long totalTicketRevenue = long.Parse(ticketPrice) * long.Parse(ticketsSold);
  84.  
  85.                     if (!venuesDict.ContainsKey(venue))
  86.                     {
  87.                         venuesDict[venue] = new Dictionary<string, long>();
  88.                     }
  89.  
  90.                     if (!venuesDict[venue].ContainsKey(artist))
  91.                     {
  92.                         venuesDict[venue][artist] = 0;
  93.                     }
  94.  
  95.                     venuesDict[venue][artist] += totalTicketRevenue;
  96.  
  97.                     input = Console.ReadLine().Trim().Split(
  98.                         new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  99.                 }
  100.                 else
  101.                 {
  102.                     input = Console.ReadLine().Trim().Split(
  103.                         new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  104.                 }
  105.             }
  106.  
  107.             foreach (var kvp in venuesDict)
  108.             {
  109.                 string venue = kvp.Key;
  110.                 var artistsAndProfit = kvp.Value;
  111.  
  112.                 Console.WriteLine(venue);
  113.  
  114.                 foreach (var kvpInner in artistsAndProfit.OrderByDescending(x => x.Value))
  115.                 {
  116.                     string artist = kvpInner.Key;
  117.                     long profit = kvpInner.Value;
  118.  
  119.                     Console.WriteLine($"#  {artist} -> {profit}");
  120.                 }
  121.             }
  122.         }
  123.  
  124.         private static int DefineIndexPossition(string[] input, int indexSign)
  125.         {
  126.             foreach (var a in input)
  127.             {
  128.                 indexSign++;
  129.                 if (a.Contains('@')) break;
  130.             }
  131.  
  132.             return indexSign;
  133.         }
  134.  
  135.         public static string GetArtist(string[] input)
  136.         {
  137.             string name = string.Empty;
  138.  
  139.             for (int i = 0; i < input.Length; i++)
  140.             {
  141.                 if (!input[i].Contains("@"))
  142.                 {
  143.                     name += input[i];
  144.                     name += " ";
  145.                 }
  146.                 else
  147.                 {
  148.                     break;
  149.                 }
  150.             }
  151.  
  152.             return name.TrimEnd();
  153.         }
  154.  
  155.         public static string GetVenue(string[] input)
  156.         {
  157.             string venueName = string.Empty;
  158.             bool venueStarted = false;
  159.  
  160.             for (int i = 0; i < input.Length - 2; i++)
  161.             {
  162.                 if (input[i].Contains("@") && !venueStarted)
  163.                 {
  164.                     venueName += input[i];
  165.                     venueName += " ";
  166.                     venueStarted = true;
  167.                 }
  168.                 else if (venueStarted)
  169.                 {
  170.                     venueName += input[i];
  171.                     venueName += " ";
  172.                 }
  173.             }
  174.  
  175.             return venueName.Trim(new[] { '@', ' ' });
  176.         }
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement