Aliendreamer

traveling company

Jul 12th, 2017
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _03NestedDictionariesExcercisesTravelCompany
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<string> destination = new List<string>();
  14.             List<int> groupCount = new List<int>();
  15.  
  16.             Dictionary<string, Dictionary<string, int>> TravelingMethods = new Dictionary<string, Dictionary<string, int>>();
  17.  
  18.             string entryInfo = Console.ReadLine();
  19.             while (entryInfo != "ready")
  20.             {
  21.                 string[] input = entryInfo.Split(':').ToArray();
  22.  
  23.                 string town = input[0];
  24.                 string[] transportoptions = input[1].Split('-', ',').ToArray();
  25.  
  26.                 for (int i = 0; i < transportoptions.Length; i += 2)
  27.                 {
  28.                     string transport = transportoptions[i];
  29.                     int amountofTravelers = int.Parse(transportoptions[i + 1]);
  30.  
  31.                     if(!TravelingMethods.ContainsKey(town))
  32.                     {
  33.                         TravelingMethods.Add(town, new Dictionary<string, int>());
  34.                         TravelingMethods[town].Add(transport, amountofTravelers);
  35.                     }
  36.                     else  if (TravelingMethods[town].ContainsKey(transport))
  37.                     {
  38.                         TravelingMethods[town][transport] = amountofTravelers;
  39.                     }
  40.                     else
  41.                     {
  42.                                          
  43.                         TravelingMethods[town].Add(transport, amountofTravelers);
  44.                     }
  45.  
  46.  
  47.                 }
  48.                 entryInfo = Console.ReadLine();
  49.  
  50.             }
  51.             entryInfo = Console.ReadLine();
  52.  
  53.             while(entryInfo!="travel time!")
  54.             {
  55.                 string[] travelingDestinations = entryInfo.Split(' ').ToArray();
  56.  
  57.                 string destinationTown = travelingDestinations[0];
  58.                 int totalgroupnumbers = int.Parse(travelingDestinations[1]);
  59.                 destination.Add(destinationTown);
  60.                 groupCount.Add(totalgroupnumbers);
  61.                
  62.  
  63.                 entryInfo = Console.ReadLine();
  64.  
  65.             }
  66.            
  67.             for(int k=0;k<destination.Count;k++)
  68.             {
  69.                 if (TravelingMethods.ContainsKey(destination[k]))
  70.                 {
  71.                     Dictionary<string, int> acomodations = TravelingMethods[destination[k]];
  72.                     string town = destination[k];
  73.                     int grouptotal = groupCount[k];
  74.                     int totalplaces = 0;
  75.                     foreach (int  num in acomodations.Values)
  76.                     {
  77.                         totalplaces += num;
  78.                     }
  79.                     if(totalplaces-grouptotal>=0)
  80.                     {
  81.                         Console.WriteLine($"{town} -> all {grouptotal} accommodated");
  82.                     }
  83.                     else
  84.                     {
  85.                         Console.WriteLine("{0} -> all except {1} accommodated",town,Math.Abs(totalplaces-grouptotal));
  86.                     }
  87.  
  88.  
  89.  
  90.                 }
  91.      
  92.             }
  93.  
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment