Advertisement
grubcho

Travel company

Jul 14th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 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. //*Write a program, which categorizes information about a travel company.
  7. Companies have various vehicles planned for different cities. Every city has prepared several types of vehicles. Each vehicle type has a different capacity.
  8. Until you receive the command “ready”, you will receive all the cities the company offers holiday packages for, and their respective vehicle types + capacities in the format:
  9. • “{city}:{type}-{capacity},{type}-{capacity}…”
  10. An example city with its transportation options would look like this:
  11. • “Athens:bus-40,airplane-300,train-150”
  12. If a city is entered a second time, add all transport which hasn’t already been added and replace existing transports’ capacities with the new ones.
  13. After the “ready” command, you will start receiving groups for various cities in the format: “{city} {peopleCount}” until you receive “travel time!”.
  14. After that, calculate whether the group will have enough seats to accommodate the passengers and print the status per these conditions:
  15. If the group’s size is smaller than or equal to the combines seats in all the vehicles, print:
  16. • “{city} -> all {peopleCount} accommodated”
  17. If the group’s size is larger than the combines seats in all the vehicles, print:
  18. • “{city} -> all except {peopleCount - transportCapacities} accommodated”*//
  19.  
  20. namespace Travel_company
  21. {
  22. class Program
  23. {
  24. static void Main(string[] args)
  25. {
  26. Dictionary<string, Dictionary<string, int>> dict = new Dictionary<string, Dictionary<string, int>>();
  27. Dictionary<string, int> groups = new Dictionary<string, int>();
  28. string input = Console.ReadLine();
  29.  
  30. while (input != "ready")
  31. {
  32. string[] toInput = input.Split(':').ToArray();
  33. string city = toInput[0];
  34. string[] token = toInput[1].Split(',').ToArray();
  35.  
  36. if (!dict.ContainsKey(city))
  37. {
  38. dict.Add(city, new Dictionary<string, int>());
  39. }
  40. for (int i = 0; i < token.Length; i++)
  41. {
  42. string[] toAdd = token[i].Split('-').ToArray();
  43. string vehicle = toAdd[0];
  44. int capacity = int.Parse(toAdd[1]);
  45. if (!dict[city].ContainsKey(vehicle))
  46. {
  47. dict[city].Add(vehicle, capacity);
  48. }
  49. dict[city][vehicle] = capacity;
  50.  
  51. }
  52. input = Console.ReadLine();
  53. }
  54. string ready = Console.ReadLine();
  55.  
  56. while (ready != "travel time!")
  57. {
  58. string[] travel = ready.Split(' ').ToArray();
  59. string travelCity = travel[0];
  60. int travelCount = int.Parse(travel[1]);
  61. if (!groups.ContainsKey(travelCity))
  62. {
  63. groups.Add(travelCity, travelCount);
  64. }
  65. groups[travelCity] = travelCount;
  66. ready = Console.ReadLine();
  67. }
  68. foreach (KeyValuePair<string, int> group in groups)
  69. {
  70. int peopleCount = group.Value;
  71.  
  72. var totalCapacityInEachCity = dict[group.Key].Values.Sum();
  73.  
  74. if (peopleCount > 0 && peopleCount <= totalCapacityInEachCity)
  75. {
  76. Console.WriteLine($"{group.Key} -> all {peopleCount} accommodated");
  77. }
  78. else if (totalCapacityInEachCity > 0 && peopleCount > totalCapacityInEachCity)
  79. {
  80. Console.WriteLine($"{group.Key} -> all except {peopleCount - totalCapacityInEachCity} accommodated");
  81. }
  82. }
  83. }
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement