} //end of AddVehicle method //changes vehicle's parking spot by reg nr public static void ChangeParkingSpot(int oldPosition, int newPosition, string regnr) { //Console.WriteLine("parking spot will be changed"); //gets list of ParkingSpots from JSON file List <ParkingSpot> parkingSpotsList = GetParkingSpotsList(); ParkingSpot[] parkingSpotsArray = parkingSpotsList.ToArray(); //Console.WriteLine("MC is gonna saved"); string sAttr = ConfigurationManager.AppSettings.Get("NrOfMCPerPSpot"); int mcSize = Int32.Parse(sAttr); if (parkingSpotsArray[oldPosition].nrOfVehicle == 2) //when there are 2 vehicles in the parking spot { //separating regnr from joined regnr string joinedRegNr = parkingSpotsArray[oldPosition].vh.RegNr; int pos = joinedRegNr.IndexOf(regnr); //gets position of the regnr string newStr = joinedRegNr.Remove(pos, regnr.Length); //removes the regnr from the string string remainingRegnr = newStr.Trim(new Char[] { ' ', ',' }); //removes komma och white space from remaining reg Console.WriteLine("position of the regnr in the joined regnr" + pos); //0 //separating CheckedInDate from joined dates string joinedDate = parkingSpotsArray[oldPosition].dateCheckedIn; string[] dates = joinedDate.Split(','); //Console.WriteLine("First date: " + dates[0]); //Console.WriteLine("Second date: " + dates[1]); string dateTobeMoved; string dateToStay; if (pos == 0) //first date belongs to the vehicle to be removed { dateTobeMoved = dates[0]; dateToStay = dates[1]; } else { dateTobeMoved = dates[1]; dateToStay = dates[0]; } //moves the vehicle into new parking spot if (parkingSpotsArray[newPosition].nrOfVehicle >= 1 && parkingSpotsArray[newPosition].nrOfVehicle < mcSize) //there is mc in the new ps { parkingSpotsArray[newPosition].vh.RegNr += "," + regnr; int nrOfMC = parkingSpotsArray[newPosition].nrOfVehicle; parkingSpotsArray[newPosition].nrOfVehicle = nrOfMC + 1; if (parkingSpotsArray[newPosition].nrOfVehicle == mcSize) { parkingSpotsArray[newPosition].status = "taken"; } else { parkingSpotsArray[newPosition].status = "halffull"; } parkingSpotsArray[newPosition].dateCheckedIn += "," + dateTobeMoved; } else //new ps is empty { MC mc = new MC(regnr); parkingSpotsArray[newPosition].vh = mc; if (parkingSpotsArray[newPosition].nrOfVehicle >= mcSize) { parkingSpotsArray[newPosition].status = "taken"; } else { parkingSpotsArray[newPosition].status = "halffull"; } parkingSpotsArray[newPosition].dateCheckedIn = dateTobeMoved; parkingSpotsArray[newPosition].nrOfVehicle = 1; } //free the old parking spot parkingSpotsArray[oldPosition].vh.RegNr = remainingRegnr; parkingSpotsArray[oldPosition].status = "halffull"; parkingSpotsArray[oldPosition].dateCheckedIn = dateToStay; parkingSpotsArray[oldPosition].nrOfVehicle = 1; } else if (parkingSpotsArray[oldPosition].nrOfVehicle == 1 && String.Equals(parkingSpotsArray[oldPosition].vh.Type, "mc")) //when 1 mc need to be moved { //moves the vehicle into new parking spot if (parkingSpotsArray[newPosition].nrOfVehicle >= 1 && parkingSpotsArray[newPosition].nrOfVehicle < mcSize) //there is mc in the new ps { parkingSpotsArray[newPosition].vh.RegNr += "," + regnr; int nrOfMC = parkingSpotsArray[newPosition].nrOfVehicle; parkingSpotsArray[newPosition].nrOfVehicle = nrOfMC + 1; if (parkingSpotsArray[newPosition].nrOfVehicle == mcSize) { parkingSpotsArray[newPosition].status = "taken"; } else { parkingSpotsArray[newPosition].status = "halffull"; } parkingSpotsArray[newPosition].dateCheckedIn += "," + parkingSpotsArray[oldPosition].dateCheckedIn; } else //new ps is empty { MC mc = new MC(regnr); parkingSpotsArray[newPosition].vh = mc; if (parkingSpotsArray[newPosition].nrOfVehicle >= mcSize) { parkingSpotsArray[newPosition].status = "taken"; } else { parkingSpotsArray[newPosition].status = "halffull"; } parkingSpotsArray[newPosition].dateCheckedIn = parkingSpotsArray[oldPosition].dateCheckedIn; parkingSpotsArray[newPosition].nrOfVehicle = 1; } parkingSpotsArray[oldPosition].vh.RegNr = "empty"; parkingSpotsArray[oldPosition].vh.Type = "empty"; parkingSpotsArray[oldPosition].status = "empty"; parkingSpotsArray[oldPosition].dateCheckedIn = "empty"; parkingSpotsArray[oldPosition].nrOfVehicle = 0; parkingSpotsArray[oldPosition].vh.Size = 0; } else //when there is only 1 car need to be moved { //moves the vehicle into new parking spot Car car = new Car(regnr); parkingSpotsArray[newPosition].vh = car; parkingSpotsArray[newPosition].status = "taken"; parkingSpotsArray[newPosition].dateCheckedIn = parkingSpotsArray[oldPosition].dateCheckedIn; parkingSpotsArray[newPosition].nrOfVehicle = 1; //free the old parking spot parkingSpotsArray[oldPosition].vh.RegNr = "empty"; parkingSpotsArray[oldPosition].vh.Type = "empty"; parkingSpotsArray[oldPosition].status = "empty"; parkingSpotsArray[oldPosition].dateCheckedIn = "empty"; parkingSpotsArray[oldPosition].nrOfVehicle = 0; parkingSpotsArray[oldPosition].vh.Size = 0; } Console.WriteLine("Your vehicle is moved into the new parkingspot"); SaveArrayInJsonFile(parkingSpotsArray); PrintParkingSpots(); }//end of ChangeParkingSpot method
static void Main(string[] args) { //Welcome text Console.WriteLine(); var table = new Table(); table.Border = TableBorder.None; table.AddColumn(new TableColumn("[bold dodgerblue2]Welcome to the Prague Parking![/]").Centered()); table.AddRow("[bold dodgerblue2]---------------------------------------------[/]"); table.Expand(); AnsiConsole.Render(table); Console.WriteLine(); Console.WriteLine(); PrintParkingSpots(); Console.WriteLine(""); Console.WriteLine(""); //Menu starts here bool isOpen = true; while (isOpen) { Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine(""); // Ask for the user's favorite fruit var menuItem = AnsiConsole.Prompt( new SelectionPrompt <string>() .Title("[bold paleturquoise1]Please choose from the menu below?[/]") .PageSize(5) .AddChoices(new[] { "1. Leave a vehicle for parking", "2. Change a vehicle's parkingspot", "3. Get your vehicle", "4. Search for a vehicle" })); Console.WriteLine(""); // Echo the fruit back to the terminal char charFirst = menuItem[0]; //Console.WriteLine("You have chosen: " + charFirst); switch (charFirst) { case '1': //Leaving vehicle to the garage Console.WriteLine(""); Console.WriteLine("You chose to leave your vehicle for parking"); Console.WriteLine(""); string regNr = "empty"; //checks if registration nr is valid bool isValidRegNr = false; while (!isValidRegNr) { string strRegNr = AnsiConsole.Ask <string>("[paleturquoise1]Please enter your vehicle's registration number: [/]"); bool isRegnrValid = IsInputRegnrValid(strRegNr); if (isRegnrValid) { regNr = strRegNr; isValidRegNr = true; } else { Console.WriteLine(""); Console.WriteLine("Registration number is not valid"); Console.WriteLine(""); } } Console.WriteLine(""); Console.WriteLine("Your reg number is: " + regNr); Console.WriteLine(""); //Getting vehicle's type string type = "empty"; type = AnsiConsole.Prompt( new SelectionPrompt <string>() .Title("[bold paleturquoise1]Please choose type of your vehicle?[/]") .PageSize(3) .AddChoices(new[] { "Car", "MC" })); Console.WriteLine(""); Console.WriteLine("You have chosen: " + type); Console.WriteLine(""); Vehicle vh = new Vehicle(); if (type == "MC") { MC motorcycle = new MC(regNr); vh = motorcycle; } else { Car car = new Car(regNr); vh = car; } AddVehicle(vh); break; case '2': Console.WriteLine("You chose to change parkingspot for your vehicle"); string regNrInput = "empty"; int parkingNrInput = 0; int position = -1; //checks if registration nr is valid bool isValidregNrInput = false; while (!isValidregNrInput) { string strRegNrInput = AnsiConsole.Ask <string>("[paleturquoise1]Please enter your vehicle's registration number: [/]"); bool isRegnrValid = IsInputRegnrValid(strRegNrInput); position = IsRegnrAvailable(strRegNrInput); if (isRegnrValid && position != -1) { regNrInput = strRegNrInput; isValidregNrInput = true; } else { Console.WriteLine(""); Console.WriteLine("Registration number is not found in the garage"); Console.WriteLine(""); } } //checks if new parking nr is valid bool isValidParkingNrInput = false; while (!isValidParkingNrInput) { Console.WriteLine(""); string strNr = AnsiConsole.Ask <string>("[paleturquoise1]Please enter parkingspot number where you want to move your vehicle: [/]"); int intNr = IsNewParkingNrValid(strNr, position); //returns position of a new parkingspot if (intNr != -1) { parkingNrInput = intNr; isValidParkingNrInput = true; } } //end of while Console.WriteLine(""); Console.WriteLine("Your reg number is: " + regNrInput); Console.WriteLine("Position of your vehicle is: " + position); Console.WriteLine("Your new ps nummer is valid: " + parkingNrInput); Console.WriteLine(""); ChangeParkingSpot(position, parkingNrInput, regNrInput); break; case '3': Console.WriteLine(""); Console.WriteLine("3. You chose to get your vehicle"); Console.WriteLine(""); string regNrInputG = "empty"; int positionG = -1; //checks if registration nr is valid bool isValidregNrInputG = false; while (!isValidregNrInputG) { string strRegNrInputG = AnsiConsole.Ask <string>("[paleturquoise1]Please enter your vehicle's registration number: [/]"); bool isRegnrValid = IsInputRegnrValid(strRegNrInputG); positionG = IsRegnrAvailable(strRegNrInputG); if (isRegnrValid && positionG != -1) { regNrInputG = strRegNrInputG; isValidregNrInputG = true; } else { Console.WriteLine(""); Console.WriteLine("Registration number is not found in the garage"); Console.WriteLine(""); } } //Console.WriteLine("Reg nr available. You can get your car" + regNrInputG); RemoveVehicle(regNrInputG, positionG); break; case '4': Console.WriteLine(""); Console.WriteLine("4. You chose to search your vehicle"); Console.WriteLine(""); //checks if registration nr is valid bool isValidregNrInputS = false; while (!isValidregNrInputS) { string strRegNrInputS = AnsiConsole.Ask <string>("[paleturquoise1]Please enter your vehicle's registration number: [/]"); bool isRegnrValidS = IsInputRegnrValid(strRegNrInputS); int positionS = IsRegnrAvailable(strRegNrInputS); if (isRegnrValidS && positionS != -1) { //regNrInputS = strRegNrInputS; isValidregNrInputS = true; int psNr = positionS + 1; Console.WriteLine("Your vehicle with registration number " + strRegNrInputS + " is in the parkingspot " + psNr); } else { Console.WriteLine(""); Console.WriteLine("Registration number is not found in the garage"); Console.WriteLine(""); } } break; default: Console.WriteLine("Please choose from the menu"); break; } } //end of while menu } //end of main