コード例 #1
0
 static void OnProcessExit(object sender, EventArgs e, Garage <Vehicle> gar)
 {
     Console.Write("Saving");
     gar.SaveVehicles();
     dotdotdot();
 }
コード例 #2
0
        /// <summary>
        /// Here you add all the different menu option and what happends when you choose them.
        /// </summary>
        /// <param name="gar">The current garage you want the changes to apply to.</param>

        static void AddToDictionary(Garage <Vehicle> gar)
        {
            //The main menu have index 0 in the list.
            Menues.Add(new Dictionary <string, Action <string> >());


            //The sub menu "Add vehicle" have index 1 in the list.
            Menues.Add(new Dictionary <string, Action <string> >());

            //The sub menu "Remove vehicle" have index 2 in the list.
            Menues.Add(new Dictionary <string, Action <string> >());

            //The sub menu "Load garage" have index 3 in the list.
            Menues.Add(new Dictionary <string, Action <string> >());


            //The sub menu "Search for vehicle" have index 4 in the list.
            Menues.Add(new Dictionary <string, Action <string> >());



            //Shows all the vehicles in the active garage.
            Menues[0].Add("Show all vehicles.", (x) => {
                Console.Clear();
                Console.WriteLine(gar.ListAllVehicles());
                Console.ReadKey();
                Console.Clear();
            });


            //Shows all the vehicle types and the amount of each in the garage.
            Menues[0].Add("Show all vehicle types.", (x) => {
                Console.Clear();
                Console.WriteLine(gar.ListAllTypes());
                Console.ReadKey();
                Console.Clear();
            });


            //Redirecting to sub menu "Add vehicle"
            Menues[0].Add("Add vehicle.", (x) => { Console.Clear();
                                                   menuIndex = 1;
                                                   index     = 0;
                                                   Console.WriteLine("What kind of vehicle would you like to add? \n "); });

            //Creating and then redirecting to sub menu "Remove vehicle"
            Menues[0].Add("Remove vehicle.", (x) => {
                #region Remove Vehicle
                string allvehicles = gar.ListAllVehicles();
                if (allvehicles != "")
                {
                    #region Creating sub menu "Remove vehicle"

                    Menues[2] = new Dictionary <string, Action <string> >();

                    foreach (var item in allvehicles.Split('\n'))
                    {
                        string thisVehicle;
                        if (item != "")
                        {
                            thisVehicle = item.Split('=')[1].Trim();
                            Menues[2].Add("Vehicle: " + thisVehicle, (y) => { Console.Clear(); gar.RemoveVehicle(thisVehicle); menuIndex = 0; index = 0; Console.WriteLine("Your vehicle was removed!"); Console.ReadKey(); });
                        }
                    }
                    #endregion

                    menuIndex = 2;
                    index     = 0;
                    Console.Clear();
                }
                else
                {
                    Console.WriteLine("There is no vehicles in the garage!");
                    Console.ReadKey();
                    Console.Clear();
                }

                #endregion
            });


            //Redirecting to sub menu 4.
            Menues[0].Add("Search for vehicle", (x) => {
                #region Search for vehicle
                menuIndex = 4;
                index     = 0;
                Console.Clear();



                #endregion
            });



            //Creates a textfile corresponding to your new garage and adds it to the textfile containing all the existing garages.
            Menues[0].Add("Create new Garage:", (x) => {
                #region Create Garage

                string FileName = @"C:\Users\elev\Documents\Visual Studio 2013\Garages.txt";


                string[] Garages = System.IO.File.ReadAllLines(FileName);
                bool Continue    = true;

                Console.Clear();
                do
                {
                    Console.WriteLine("What name do you want to have on the new garage?");
                    string newgarage = Console.ReadLine();
                    Continue         = true;
                    int spots        = 0;
                    do
                    {
                        try
                        {
                            Console.WriteLine("How many spots do you want in your garage?");
                            spots = int.Parse(Console.ReadLine());
                            if (spots > 0)
                            {
                                Continue = false;
                            }
                            else
                            {
                                Console.WriteLine("You can not have a garage with no spots!");
                                Console.ReadKey();
                                Console.Clear();
                            }
                        }
                        catch (Exception)
                        {
                            Console.WriteLine("Wrong input! Try again!");
                        }
                    } while (Continue);

                    if (!Garages.Contains(newgarage))
                    {
                        Continue = false;
                        Console.Clear();
                        FileStream appendWrite = File.Open(FileName, FileMode.Append);
                        StreamWriter write     = new StreamWriter(appendWrite);
                        write.WriteLine(newgarage + ":" + spots);
                        write.Close();
                        appendWrite.Close();
                        FileStream temp = new FileStream(@"C:\Users\elev\Documents\Visual Studio 2013\" + newgarage + ".txt", FileMode.Create);
                        temp.Close();

                        Console.WriteLine("You need to restart your application before your new garage is avalible.\nIt is currently in construction!");
                        Console.ReadKey();
                    }
                    else
                    {
                        Console.WriteLine("It allready exists. Try again.");
                        Console.ReadKey();
                        Console.Clear();
                    }
                } while (Continue);



                Console.Clear();

                #endregion
            });


            //Creates and redirects to the sub menu "Load Garage"
            Menues[0].Add("Load Garage", (x) => {
                #region Load Garage

                string FileName  = @"C:\Users\elev\Documents\Visual Studio 2013\Garages.txt";
                string[] Garages = System.IO.File.ReadAllLines(FileName);

                #region Creating sub menu "Load Garage"
                Menues[3] = new Dictionary <string, Action <string> >();

                foreach (var item in Garages)
                {
                    string garageName = item.Split(':')[0];
                    int garageSpots   = int.Parse(item.Split(':')[1]);


                    if (item != "" && garageName != gar.GarageName)
                    {
                        Menues[3].Add("Garage: " + garageName, (y) =>
                        {
                            Console.Clear();
                            menuIndex = 0;
                            index     = 0;
                            gar.SaveVehicles();
                            gar.removeAllVehicles();
                            LoadVehicles(gar, @"C:\Users\elev\Documents\Visual Studio 2013\" + garageName + ".txt");

                            gar.changeMaxParkingSpots(garageSpots);
                            gar.GarageName = garageName;


                            Console.Write("Loading");
                            dotdotdot();
                            Console.Clear();
                        });
                    }
                }

                #endregion

                menuIndex = 3;
                index     = 0;
                Console.Clear();
                Console.WriteLine("Coose what garage to load. \n");


                #endregion
            });


            Menues[0].Add("Exit application\n\n", (x) => { Environment.Exit(0); });
            //Creates sub menu "Add vehicle"
            #region Creatig sub menu "Add vehicle"

            Menues[1].Add("Car", (x) => {
                #region Menu Car

                int NumberOfWheels = 0;
                string Color;
                string RegNumber;
                int NumberOfWindows = 0;
                bool SunRoof;

                Console.Clear();

                bool input = true;
                do
                {
                    try
                    {
                        Console.WriteLine("Number of Wheels: ");
                        NumberOfWheels = int.Parse(Console.ReadLine());
                        input          = false;
                    }
                    catch (Exception)
                    {
                        Console.Clear();
                        Console.WriteLine("Wrong input. Try again!");
                    }
                } while (input);

                Console.WriteLine("Color: ");
                Color = Console.ReadLine();
                Console.WriteLine("RegNum: ");
                RegNumber = Console.ReadLine();



                input = true;
                do
                {
                    try
                    {
                        Console.WriteLine("NumberOfWindows: ");
                        NumberOfWindows = int.Parse(Console.ReadLine());
                        input           = false;
                    }
                    catch (Exception)
                    {
                        Console.Clear();
                        Console.WriteLine("Wrong input. Try again!");
                    }
                } while (input);

                Console.WriteLine("Does it have a sunroof? (y/n)");
                string sunroof = Console.ReadLine();
                if (sunroof.ToLower() == "y")
                {
                    SunRoof = true;
                }

                else
                {
                    SunRoof = false;
                    Console.WriteLine("no sunroof");
                }


                if (gar.AddVehicle(new Car(NumberOfWheels, Color, RegNumber, NumberOfWindows, SunRoof)))
                {
                    Console.WriteLine("Your car has been added.");
                }
                else
                {
                    Console.WriteLine("That Registration number allready exists!");
                }

                Console.ReadKey();
                Console.WriteLine("Press the any key.");
                Console.Clear();
                menuIndex = 0;
                index     = 0;



                #endregion
            });



            Menues[1].Add("Airplane", (x) => {
                #region Menu Airplane

                int NumberOfWheels = 0;
                string Color;
                string RegNumber;
                int NumberOfWings = 0;
                bool Pontoons;

                Console.Clear();

                bool input = true;
                do
                {
                    try
                    {
                        Console.WriteLine("Number of Wheels: ");
                        NumberOfWheels = int.Parse(Console.ReadLine());
                        input          = false;
                    }
                    catch (Exception)
                    {
                        Console.Clear();
                        Console.WriteLine("Wrong input. Try again!");
                    }
                } while (input);

                Console.WriteLine("Color: ");
                Color = Console.ReadLine();
                Console.WriteLine("RegNum: ");
                RegNumber = Console.ReadLine();



                input = true;
                do
                {
                    try
                    {
                        Console.WriteLine("NumberOfWings: ");
                        NumberOfWings = int.Parse(Console.ReadLine());
                        input         = false;
                    }
                    catch (Exception)
                    {
                        Console.Clear();
                        Console.WriteLine("Wrong input. Try again!");
                    }
                } while (input);

                Console.WriteLine("Does it have poontons? (y/n)");
                string pontoons = Console.ReadLine();
                if (pontoons.ToLower() == "y")
                {
                    Pontoons = true;
                }

                else
                {
                    Pontoons = false;
                    Console.WriteLine("No pontoons");
                }



                if (gar.AddVehicle(new Airplane(NumberOfWheels, Color, RegNumber, NumberOfWings, Pontoons)))
                {
                    Console.WriteLine("Your airplane has been added.");
                }
                else
                {
                    Console.WriteLine("That Registration number allready exists!");
                }

                Console.ReadKey();
                Console.WriteLine("Press the any key.");
                Console.Clear();
                menuIndex = 0;
                index     = 0;

                #endregion
            });



            Menues[1].Add("Motorcycle", (x) => {
                #region Menu Motorcycle

                int NumberOfWheels = 0;
                string Color;
                string RegNumber;
                string Name;
                int Year = 0;

                Console.Clear();


                Console.WriteLine("Name: ");
                Name = Console.ReadLine();

                bool input = true;
                do
                {
                    try
                    {
                        Console.WriteLine("Number of Wheels: ");
                        NumberOfWheels = int.Parse(Console.ReadLine());
                        input          = false;
                    }
                    catch (Exception)
                    {
                        Console.Clear();
                        Console.WriteLine("Wrong input. Try again!");
                    }
                } while (input);

                Console.WriteLine("Color: ");
                Color = Console.ReadLine();
                Console.WriteLine("RegNum: ");
                RegNumber = Console.ReadLine();



                input = true;
                do
                {
                    try
                    {
                        Console.WriteLine("Year: ");
                        Year  = int.Parse(Console.ReadLine());
                        input = false;
                    }
                    catch (Exception)
                    {
                        Console.Clear();
                        Console.WriteLine("Wrong input. Try again!");
                    }
                } while (input);



                if (gar.AddVehicle(new Motorcycle(NumberOfWheels, Color, RegNumber, Name, Year)))
                {
                    Console.WriteLine("Your motorcycle has been added.");
                }
                else
                {
                    Console.WriteLine("That Registration number allready exists!");
                }

                Console.ReadKey();
                Console.WriteLine("Press the any key.");
                Console.Clear();
                menuIndex = 0;
                index     = 0;



                #endregion
            });



            Menues[1].Add("Buss", (x) => {
                #region Menu Buss

                int NumberOfWheels = 0;
                string Color;
                string RegNumber;
                int Seats = 0;
                string Route;

                Console.Clear();



                bool input = true;
                do
                {
                    try
                    {
                        Console.WriteLine("Number of Wheels: ");
                        NumberOfWheels = int.Parse(Console.ReadLine());
                        input          = false;
                    }
                    catch (Exception)
                    {
                        Console.Clear();
                        Console.WriteLine("Wrong input. Try again!");
                    }
                } while (input);

                Console.WriteLine("Color: ");
                Color = Console.ReadLine();
                Console.WriteLine("RegNum: ");
                RegNumber = Console.ReadLine();



                input = true;
                do
                {
                    try
                    {
                        Console.WriteLine("Number of seats: ");
                        Seats = int.Parse(Console.ReadLine());
                        input = false;
                    }
                    catch (Exception)
                    {
                        Console.Clear();
                        Console.WriteLine("Wrong input. Try again!");
                    }
                } while (input);

                Console.WriteLine("Route: ");
                Route = Console.ReadLine();



                if (gar.AddVehicle(new Buss(NumberOfWheels, Color, RegNumber, Seats, Route)))
                {
                    Console.WriteLine("Your buss has been added.");
                }
                else
                {
                    Console.WriteLine("That Registration number allready exists!");
                }

                Console.ReadKey();
                Console.WriteLine("Press the any key.");
                Console.Clear();
                menuIndex = 0;
                index     = 0;


                #endregion
            });



            Menues[1].Add("Boat\n\n\n", (x) => {
                #region Menu Boat

                int NumberOfWheels = 0;
                string Color;
                string RegNumber;
                int Speed        = 0;
                int NumberOfBeds = 0;

                Console.Clear();

                bool input = true;
                do
                {
                    try
                    {
                        Console.WriteLine("Number of Wheels: ");
                        NumberOfWheels = int.Parse(Console.ReadLine());
                        input          = false;
                    }
                    catch (Exception)
                    {
                        Console.Clear();
                        Console.WriteLine("Wrong input. Try again!");
                    }
                } while (input);

                Console.WriteLine("Color: ");
                Color = Console.ReadLine();
                Console.WriteLine("RegNum: ");
                RegNumber = Console.ReadLine();



                input = true;
                do
                {
                    try
                    {
                        Console.WriteLine("Speed: ");
                        Speed = int.Parse(Console.ReadLine());
                        input = false;
                    }
                    catch (Exception)
                    {
                        Console.Clear();
                        Console.WriteLine("Wrong input. Try again!");
                    }
                } while (input);

                input = true;
                do
                {
                    try
                    {
                        Console.WriteLine("Number of beds in the boat: ");
                        NumberOfBeds = int.Parse(Console.ReadLine());
                        input        = false;
                    }
                    catch (Exception)
                    {
                        Console.Clear();
                        Console.WriteLine("Wrong input. Try again!");
                    }
                } while (input);


                if (gar.AddVehicle(new Boat(NumberOfWheels, Color, RegNumber, Speed, NumberOfBeds)))
                {
                    Console.WriteLine("Your boat has been added.");
                }
                else
                {
                    Console.WriteLine("That Registration number allready exists!");
                }

                Console.ReadKey();
                Console.WriteLine("Press the any key.");
                Console.Clear();
                menuIndex = 0;
                index     = 0;


                #endregion
            });

            #endregion


            //Creates sub menu "Search vehicle"
            #region Creating sub menu "Search vehicle"

            Menues[4].Add("Search by RegNum", (y) =>
            {
                #region Search by RegNum

                Console.WriteLine("What RegNum would you like to search on?");

                string search = gar.SearchByRegNum(Console.ReadLine());

                if (search != "")
                {
                    Console.WriteLine(search);
                }
                else
                {
                    Console.WriteLine("There is no vehicle with that reg num in the garage.");
                }

                Console.ReadKey();
                Console.Clear();
                index = 0;

                #endregion
            });


            Menues[4].Add("Search by color", (y) =>
            {
                #region Search by color

                Console.WriteLine("What Color would you like to search on?");

                string search = gar.SearchByColor(Console.ReadLine());

                if (search != "")
                {
                    Console.WriteLine(search);
                }
                else
                {
                    Console.WriteLine("There is no vehicle with that color num in the garage.");
                }

                Console.ReadKey();
                Console.Clear();

                #endregion
            });



            Menues[4].Add("Search by number of wheels", (y) =>
            {
                #region Search by number of wheels
                Console.WriteLine("What number of wheels would you like to search on?");
                string search;
                try
                {
                    search = gar.SearchByNumberOfWheels(int.Parse(Console.ReadLine()));
                }
                catch (Exception)
                {
                    search = "";
                }


                if (search != "")
                {
                    Console.WriteLine(search);
                }
                else
                {
                    Console.WriteLine("There is no vehicle with that number of wheels in the garage.");
                }

                Console.ReadKey();
                Console.Clear();
                #endregion
            });

            #endregion
        }