Ejemplo n.º 1
0
        private static void CreateNewBoats(List <Boat> boats, int newBoats)
        {
            for (int i = 0; i < newBoats; i++)
            {
                int boatType = Utils.r.Next(4 + 1);

                switch (boatType)
                {
                case 0:
                    RowingBoat.CreateRowingBoat(boats);
                    break;

                case 1:
                    MotorBoat.CreateMotorBoat(boats);
                    break;

                case 2:
                    SailingBoat.CreateSailingBoat(boats);
                    break;

                case 3:
                    Catamaran.CreateCatamaran(boats);
                    break;

                case 4:
                    CargoShip.CreateCargoShip(boats);
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            var fileText = File.ReadLines("BoatsInHarbour.txt", System.Text.Encoding.UTF7);

            //Console.WriteLine(Utils.PrintTextFromFile(fileText));

            HarbourSpace[] harbour = new HarbourSpace[64];

            for (int i = 0; i < harbour.Length; i++)
            {
                harbour[i] = new HarbourSpace(i);
            }

            AddBoatsFromFileToHarbour(fileText, harbour);

            Console.WriteLine("Båtar i hamn efter uppstart\n");
            Console.WriteLine(PrintHarbour2(harbour));
            Console.WriteLine();

            bool goToNextDay = true;

            while (goToNextDay)
            {
                List <Boat> boatsInHarbour = GenerateBoatsInHarbourList(harbour);
                AddDayToDaysSinceArrival(boatsInHarbour);

                bool boatRemoved = true;

                while (boatRemoved)
                {
                    boatRemoved = RemoveBoats(harbour);
                }

                Console.WriteLine("Båtar i hamn efter dagens avfärder");
                Console.WriteLine(PrintHarbour2(harbour));
                Console.WriteLine();

                int rejectedRowingBoats  = 0;
                int rejectedMotorBoats   = 0;
                int rejectedSailingBoats = 0;
                int rejectedCatamarans   = 0;
                int rejectedCargoShips   = 0;

                List <Boat> arrivingBoats         = new List <Boat>();
                int         NumberOfArrivingBoats = 5;

                CreateNewBoats(arrivingBoats, NumberOfArrivingBoats); // Tar bor tillfälligt, för att kunna styra vilka båtar som läggs till

                // Skapar båtar för test, ta bort sedan
                //arrivingBoats.Add(new MotorBoat("M-" + Boat.GenerateID(), 10, 2, 3, 0, 4));
                //arrivingBoats.Add(new RowingBoat("R-" + Boat.GenerateID(), 10, 2, 1, 0, 4));
                //arrivingBoats.Add(new SailingBoat("S-" + Boat.GenerateID(), 10, 2, 4, 0, 4));
                //arrivingBoats.Add(new CargoShip("L-" + Boat.GenerateID(), 10, 2, 6, 0, 4));
                //arrivingBoats.Add(new RowingBoat("R-" + Boat.GenerateID(), 10, 2, 1, 0, 4));
                //arrivingBoats.Add(new Catamaran("K-" + Boat.GenerateID(), 10, 2, 1, 0, 4));

                Console.WriteLine("Anländande båtar");
                foreach (var boat in arrivingBoats)  //Kontroll, ta bort sedan
                {
                    Console.WriteLine(boat.ToString());
                }
                Console.WriteLine();

                foreach (var boat in arrivingBoats)
                {
                    int  harbourPosition;
                    bool spaceFound;

                    if (boat is RowingBoat)
                    {
                        (harbourPosition, spaceFound) = RowingBoat.FindRowingboatSpace(harbour);

                        if (spaceFound)
                        {
                            harbour[harbourPosition].ParkedBoats.Add(boat);
                        }
                        else
                        {
                            rejectedRowingBoats++;
                        }
                    }

                    else if (boat is MotorBoat)
                    {
                        (harbourPosition, spaceFound) = MotorBoat.FindMotorBoatSpace(harbour);

                        if (spaceFound)
                        {
                            harbour[harbourPosition].ParkedBoats.Add(boat);
                        }
                        else
                        {
                            rejectedMotorBoats++;
                        }
                    }

                    else if (boat is SailingBoat)
                    {
                        (harbourPosition, spaceFound) = SailingBoat.FindSailingBoatSpace(harbour);

                        if (spaceFound)
                        {
                            harbour[harbourPosition].ParkedBoats.Add(boat);
                            harbour[harbourPosition + 1].ParkedBoats.Add(boat);
                        }

                        if (spaceFound == false)
                        {
                            rejectedSailingBoats++;
                        }
                    }

                    else if (boat is Catamaran)
                    {
                        (harbourPosition, spaceFound) = Catamaran.FindCatamaranSpace(harbour);

                        if (spaceFound)
                        {
                            harbour[harbourPosition].ParkedBoats.Add(boat);
                            harbour[harbourPosition + 1].ParkedBoats.Add(boat);
                            harbour[harbourPosition + 2].ParkedBoats.Add(boat);
                        }

                        if (spaceFound == false)
                        {
                            rejectedCatamarans++;
                        }
                    }

                    else if (boat is CargoShip)
                    {
                        (harbourPosition, spaceFound) = CargoShip.FindCargoShipSpace(harbour);

                        if (spaceFound)
                        {
                            harbour[harbourPosition].ParkedBoats.Add(boat);
                            harbour[harbourPosition + 1].ParkedBoats.Add(boat);
                            harbour[harbourPosition + 2].ParkedBoats.Add(boat);
                            harbour[harbourPosition + 3].ParkedBoats.Add(boat);
                        }

                        if (spaceFound == false)
                        {
                            rejectedCargoShips++;
                        }
                    }
                }

                Console.WriteLine("Båtar i hamn\n------------\n");
                Console.WriteLine(PrintHarbour2(harbour));
                Console.WriteLine();

                boatsInHarbour = GenerateBoatsInHarbourList(harbour);

                Console.WriteLine(GnerateSummaryOfBoats(boatsInHarbour));

                int    sumOfWeight     = GenerateSumOfWeight(boatsInHarbour);
                double averageSpeed    = GenerateAverageSpeed(boatsInHarbour);
                int    availableSpaces = CountAvailableSpaces(harbour);

                Console.WriteLine(PrintStatistics(sumOfWeight, averageSpeed, availableSpaces,
                                                  rejectedRowingBoats, rejectedMotorBoats, rejectedSailingBoats, rejectedCatamarans, rejectedCargoShips));

                Console.WriteLine();
                Console.WriteLine();

                Console.Write("Tryck \"Q\" för att avsluta eller valfri annan tangent för att gå till nästa dag ");

                ConsoleKey input = Console.ReadKey().Key;

                goToNextDay = input != ConsoleKey.Q;

                Console.WriteLine();
                Console.WriteLine();
            }


            StreamWriter sw = new StreamWriter("BoatsInHarbour.txt", false, System.Text.Encoding.UTF7);

            SaveToFile(sw, harbour);
            sw.Close();
        }