Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace PragueParking1._0
- {
- class Program // ALLA MOTORCYKLAR HAR PREFIXET MCC PÅ SINA REGPLÅTAR, ALLA BILAR HAR PREFIXET CAR
- {
- static string[] ParkingLot = new string[100];
- static string[] templot = new string[100];
- static Random rnd = new Random();
- static void Main(string[] args)
- {
- // ImportSampleData();
- while(true) //loop
- {
- DisplayMenu();
- Console.WriteLine("\n" + Messages.Commands +"\n");
- string input = Console.ReadLine();
- CommandOperator(input);
- Console.Clear();
- }
- }
- static void ImportSampleData()
- {
- for(int i = 4; i < 15; i++)
- ParkingLot[i] = GenerateRegPlate("car");
- }
- static void PrintAllParkedCars()
- {
- foreach(string car in ParkingLot)
- {
- if(car != null)
- {
- Console.WriteLine(car);
- }
- }
- }
- static void DisplayMenu()
- {
- Console.Write("Meny: \n");
- Console.WriteLine("Använd /hjälp för instruktioner");
- }
- static void CommandOperator(string input)
- {
- switch(input.ToLower())
- {
- case "parkera":
- StartAPark();
- break;
- case "flytta":
- StartMoveCar();
- break;
- case "lediga":
- PrintOpenSpots();
- Console.ReadKey();
- break;
- case "dev":
- PrintTakenSpots();
- PrintCars();
- Console.WriteLine("XD: " + ParkingLot[9]);
- Console.ReadKey();
- break;
- case "/hjälp":
- Console.WriteLine("\n"+Messages.Commands);
- Console.ReadKey();
- break;
- case "exit":
- Console.Write("Exiting...");
- System.Threading.Thread.Sleep(450);
- Environment.Exit(0);
- break;
- default:
- Console.WriteLine(Messages.UnrecognizedCommand);
- break;
- }
- }
- static void StartAPark()
- {
- Console.Clear();
- Console.WriteLine("Vad ska du parkera för något? {MC}/{BIL}?");
- string vechiletype = Console.ReadLine();
- string regplate = "";
- switch(vechiletype.ToLower())
- {
- case "mc":
- Console.Write("Ange ditt registreringsnummer: M");
- regplate = "M" + Console.ReadLine();
- break;
- case "bil":
- Console.Write("Ange ditt registreringsnummer: C");
- regplate = "C" + Console.ReadLine();
- break;
- default:
- return;
- }
- if (IsRegPlateTaken(regplate))
- {
- Console.WriteLine(Messages.InvalidRegplate);
- Console.ReadKey();
- StartAPark();
- }
- Console.WriteLine("Vilken ruta skulle du vilja parkera på?");
- int input;
- int.TryParse(Console.ReadLine(), out input);
- if(!CheckIfSpotIsTaken(input))
- {
- if(!IsRegPlateTaken(regplate))
- {
- Park(regplate, input);
- Console.WriteLine("Parkering Lyckad!");
- Console.ReadKey();
- }
- else
- {
- Console.WriteLine(Messages.InvalidRegplate);
- }
- }
- else
- {
- Console.WriteLine(Messages.SpotTaken);
- string Alt = Console.ReadLine();
- switch(Alt.ToLower())
- {
- case "ja":
- int spot = AllocateSpot();
- Park(regplate, spot);
- Console.WriteLine("Parkerat på ruta " + spot);
- Console.ReadKey();
- break;
- default:
- break;
- }
- }
- }
- static void StartMoveCar()
- {
- Console.Clear();
- Console.WriteLine("Vilken Plats står du nu på?");
- int plats = 0;
- int.TryParse(Console.ReadLine(), out plats);
- Console.WriteLine("Vilken plats vill du flytta den till?");
- int fplats = 0;
- int.TryParse(Console.ReadLine(), out fplats);
- if (CheckIfSpotIsTaken(fplats))
- {
- Console.WriteLine("Platsen är redan Upptagen");
- Console.ReadKey();
- return;
- }
- MoveCar(plats, fplats);
- Console.WriteLine($"Vi har nu flyttat din bil från plats {plats} till {fplats}");
- }
- static void PrintTakenSpots()
- {
- for(int i = 0; i < ParkingLot.Length; i++)
- {
- if(ParkingLot[i] != null)
- {
- Console.WriteLine($"{i + 1} ");
- }
- }
- }
- static void PrintOpenSpots()
- {
- for (int i = 0; i < ParkingLot.Length; i++)
- {
- if (ParkingLot[i] == null)
- {
- Console.Write($"Plats: {i + 1} ||");
- }
- }
- }
- static void PrintCars()
- {
- foreach(string car in ParkingLot)
- {
- if(car != null)
- Console.WriteLine(car);
- }
- }
- static void Park(string regplate, int spot)
- {
- if(spot == 0)
- {
- ParkingLot[0] = regplate;
- }
- else
- ParkingLot[spot-1] = regplate;
- }
- static bool IsRegPlateTaken(string regplate)
- {
- ParkingLot.CopyTo(templot, 0);
- Array.Sort(templot);
- if (Array.BinarySearch(templot, regplate) < 0)
- {
- return false;
- }
- else { return true; };
- }
- static int GetCarIndex(string regplate)
- {
- ParkingLot.CopyTo(templot, 0);
- Array.Sort(templot);
- int pos = Array.BinarySearch(templot, regplate);
- if (pos < 0)
- {
- return -1;
- }
- else
- return pos;
- }
- static void MoveCar(int index, int targetdest)
- {
- if(!CheckIfSpotIsTaken(targetdest-1))
- {
- ParkingLot[targetdest-1] = ParkingLot[index-1];
- ParkingLot[index-1] = null;
- }
- }
- static bool CheckIfSpotIsTaken(int index)
- {
- if (ParkingLot[index - 1] == null)
- {
- Console.WriteLine($"Checking if spot {index - 1} is taken, at spot {index - 1} the car is {ParkingLot[index - 1]}");
- return false;
- }
- else return true;
- }
- static int AllocateSpot()
- {
- for (int i = 0; i < ParkingLot.Length; i++)
- {
- if (ParkingLot[i] == null)
- return i;
- }
- return -1;
- }
- static string GenerateRegPlate(string type)
- {
- string regplate = "";
- switch(type)
- {
- case "mc":
- regplate = "MCC";
- for (int i = 0; i < 3; i++)
- {
- regplate += rnd.Next(0, 9);
- }
- break;
- case "car":
- regplate = "CAR";
- for (int i = 0; i < 3; i++)
- {
- regplate += rnd.Next(0, 9);
- }
- break;
- }
- return regplate;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement