Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.55 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 PragueParking1._0
  8. {
  9. class Program // ALLA MOTORCYKLAR HAR PREFIXET MCC PÅ SINA REGPLÅTAR, ALLA BILAR HAR PREFIXET CAR
  10. {
  11. static string[] ParkingLot = new string[100];
  12. static string[] templot = new string[100];
  13.  
  14. static Random rnd = new Random();
  15. static void Main(string[] args)
  16. {
  17. // ImportSampleData();
  18.  
  19.  
  20. while(true) //loop
  21. {
  22. DisplayMenu();
  23. Console.WriteLine("\n" + Messages.Commands +"\n");
  24. string input = Console.ReadLine();
  25. CommandOperator(input);
  26.  
  27. Console.Clear();
  28. }
  29.  
  30. }
  31.  
  32. static void ImportSampleData()
  33. {
  34. for(int i = 4; i < 15; i++)
  35. ParkingLot[i] = GenerateRegPlate("car");
  36. }
  37.  
  38. static void PrintAllParkedCars()
  39. {
  40. foreach(string car in ParkingLot)
  41. {
  42. if(car != null)
  43. {
  44. Console.WriteLine(car);
  45. }
  46. }
  47. }
  48.  
  49.  
  50. static void DisplayMenu()
  51. {
  52. Console.Write("Meny: \n");
  53. Console.WriteLine("Använd /hjälp för instruktioner");
  54. }
  55.  
  56. static void CommandOperator(string input)
  57. {
  58.  
  59. switch(input.ToLower())
  60. {
  61. case "parkera":
  62. StartAPark();
  63. break;
  64. case "flytta":
  65. StartMoveCar();
  66. break;
  67. case "lediga":
  68. PrintOpenSpots();
  69. Console.ReadKey();
  70. break;
  71. case "dev":
  72. PrintTakenSpots();
  73. PrintCars();
  74. Console.WriteLine("XD: " + ParkingLot[9]);
  75. Console.ReadKey();
  76. break;
  77. case "/hjälp":
  78. Console.WriteLine("\n"+Messages.Commands);
  79. Console.ReadKey();
  80. break;
  81. case "exit":
  82. Console.Write("Exiting...");
  83. System.Threading.Thread.Sleep(450);
  84. Environment.Exit(0);
  85. break;
  86. default:
  87. Console.WriteLine(Messages.UnrecognizedCommand);
  88. break;
  89. }
  90. }
  91.  
  92.  
  93.  
  94. static void StartAPark()
  95. {
  96. Console.Clear();
  97. Console.WriteLine("Vad ska du parkera för något? {MC}/{BIL}?");
  98. string vechiletype = Console.ReadLine();
  99. string regplate = "";
  100. switch(vechiletype.ToLower())
  101. {
  102. case "mc":
  103. Console.Write("Ange ditt registreringsnummer: M");
  104. regplate = "M" + Console.ReadLine();
  105. break;
  106. case "bil":
  107. Console.Write("Ange ditt registreringsnummer: C");
  108. regplate = "C" + Console.ReadLine();
  109. break;
  110. default:
  111. return;
  112. }
  113. if (IsRegPlateTaken(regplate))
  114. {
  115. Console.WriteLine(Messages.InvalidRegplate);
  116. Console.ReadKey();
  117. StartAPark();
  118. }
  119. Console.WriteLine("Vilken ruta skulle du vilja parkera på?");
  120. int input;
  121. int.TryParse(Console.ReadLine(), out input);
  122. if(!CheckIfSpotIsTaken(input))
  123. {
  124. if(!IsRegPlateTaken(regplate))
  125. {
  126. Park(regplate, input);
  127. Console.WriteLine("Parkering Lyckad!");
  128. Console.ReadKey();
  129. }
  130. else
  131. {
  132. Console.WriteLine(Messages.InvalidRegplate);
  133. }
  134. }
  135. else
  136. {
  137. Console.WriteLine(Messages.SpotTaken);
  138. string Alt = Console.ReadLine();
  139. switch(Alt.ToLower())
  140. {
  141. case "ja":
  142. int spot = AllocateSpot();
  143. Park(regplate, spot);
  144. Console.WriteLine("Parkerat på ruta " + spot);
  145. Console.ReadKey();
  146. break;
  147. default:
  148. break;
  149. }
  150. }
  151.  
  152.  
  153. }
  154.  
  155. static void StartMoveCar()
  156. {
  157. Console.Clear();
  158. Console.WriteLine("Vilken Plats står du nu på?");
  159. int plats = 0;
  160. int.TryParse(Console.ReadLine(), out plats);
  161. Console.WriteLine("Vilken plats vill du flytta den till?");
  162. int fplats = 0;
  163. int.TryParse(Console.ReadLine(), out fplats);
  164. if (CheckIfSpotIsTaken(fplats))
  165. {
  166. Console.WriteLine("Platsen är redan Upptagen");
  167. Console.ReadKey();
  168. return;
  169. }
  170. MoveCar(plats, fplats);
  171. Console.WriteLine($"Vi har nu flyttat din bil från plats {plats} till {fplats}");
  172. }
  173.  
  174. static void PrintTakenSpots()
  175. {
  176. for(int i = 0; i < ParkingLot.Length; i++)
  177. {
  178. if(ParkingLot[i] != null)
  179. {
  180. Console.WriteLine($"{i + 1} ");
  181. }
  182. }
  183. }
  184.  
  185. static void PrintOpenSpots()
  186. {
  187. for (int i = 0; i < ParkingLot.Length; i++)
  188. {
  189. if (ParkingLot[i] == null)
  190. {
  191. Console.Write($"Plats: {i + 1} ||");
  192. }
  193. }
  194. }
  195.  
  196. static void PrintCars()
  197. {
  198. foreach(string car in ParkingLot)
  199. {
  200. if(car != null)
  201. Console.WriteLine(car);
  202. }
  203. }
  204.  
  205. static void Park(string regplate, int spot)
  206. {
  207. if(spot == 0)
  208. {
  209. ParkingLot[0] = regplate;
  210. }
  211. else
  212. ParkingLot[spot-1] = regplate;
  213. }
  214.  
  215. static bool IsRegPlateTaken(string regplate)
  216. {
  217.  
  218. ParkingLot.CopyTo(templot, 0);
  219. Array.Sort(templot);
  220. if (Array.BinarySearch(templot, regplate) < 0)
  221. {
  222. return false;
  223. }
  224. else { return true; };
  225.  
  226. }
  227.  
  228.  
  229. static int GetCarIndex(string regplate)
  230. {
  231. ParkingLot.CopyTo(templot, 0);
  232. Array.Sort(templot);
  233. int pos = Array.BinarySearch(templot, regplate);
  234. if (pos < 0)
  235. {
  236. return -1;
  237. }
  238. else
  239. return pos;
  240. }
  241.  
  242. static void MoveCar(int index, int targetdest)
  243. {
  244. if(!CheckIfSpotIsTaken(targetdest-1))
  245. {
  246. ParkingLot[targetdest-1] = ParkingLot[index-1];
  247. ParkingLot[index-1] = null;
  248. }
  249. }
  250.  
  251. static bool CheckIfSpotIsTaken(int index)
  252. {
  253. if (ParkingLot[index - 1] == null)
  254. {
  255. Console.WriteLine($"Checking if spot {index - 1} is taken, at spot {index - 1} the car is {ParkingLot[index - 1]}");
  256. return false;
  257. }
  258. else return true;
  259. }
  260.  
  261. static int AllocateSpot()
  262. {
  263. for (int i = 0; i < ParkingLot.Length; i++)
  264. {
  265. if (ParkingLot[i] == null)
  266. return i;
  267. }
  268. return -1;
  269. }
  270.  
  271.  
  272. static string GenerateRegPlate(string type)
  273. {
  274. string regplate = "";
  275.  
  276. switch(type)
  277. {
  278. case "mc":
  279. regplate = "MCC";
  280. for (int i = 0; i < 3; i++)
  281. {
  282. regplate += rnd.Next(0, 9);
  283. }
  284. break;
  285.  
  286. case "car":
  287. regplate = "CAR";
  288. for (int i = 0; i < 3; i++)
  289. {
  290. regplate += rnd.Next(0, 9);
  291. }
  292. break;
  293. }
  294.  
  295. return regplate;
  296. }
  297. }
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement