Beispiel #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;
                }
            }
        }
Beispiel #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();
        }
Beispiel #3
0
        private static void AddBoatsFromFileToHarbour(IEnumerable <string> fileText, HarbourSpace[] harbour)
        {
            // File:
            // index; Id; Weight; MaxSpeed; Type; DaysStaying; DaySinceArrival; Special
            // 0      1   2       3         4     5            6                7


            foreach (var line in fileText)
            {
                int      index;
                string[] boatData = line.Split(";");

                switch (boatData[4])
                {
                case "Roddbåt":
                    index = int.Parse(boatData[0]);
                    harbour[index].ParkedBoats.Add
                        (new RowingBoat(boatData[1], int.Parse(boatData[2]), int.Parse(boatData[3]),
                                        int.Parse(boatData[5]), int.Parse(boatData[6]), int.Parse(boatData[7])));
                    break;

                case "Motorbåt":
                    index = int.Parse(boatData[0]);
                    harbour[index].ParkedBoats.Add
                        (new MotorBoat(boatData[1], int.Parse(boatData[2]), int.Parse(boatData[3]),
                                       int.Parse(boatData[5]), int.Parse(boatData[6]), int.Parse(boatData[7])));
                    break;

                case "Segelbåt":
                    index = int.Parse(boatData[0]);

                    if (harbour[index].ParkedBoats.Count == 0)     // När andra halvan av segelbåten kommmer från foreach är den redan tillagd på den platsen annars hade det blivit två kopior av samma båt
                    {
                        SailingBoat sailingBoat = new SailingBoat(boatData[1], int.Parse(boatData[2]), int.Parse(boatData[3]),
                                                                  int.Parse(boatData[5]), int.Parse(boatData[6]), int.Parse(boatData[7]));

                        harbour[index].ParkedBoats.Add(sailingBoat);
                        harbour[index + 1].ParkedBoats.Add(sailingBoat);     // samma båt på två platser
                    }
                    break;

                case "Katamaran":
                    index = int.Parse(boatData[0]);

                    if (harbour[index].ParkedBoats.Count == 0)     // När resten av lastfartyget kommmer från foreach är det redan tillagt, annars hade det blivit kopior
                    {
                        Catamaran catamaran = new Catamaran(boatData[1], int.Parse(boatData[2]), int.Parse(boatData[3]),
                                                            int.Parse(boatData[5]), int.Parse(boatData[6]), int.Parse(boatData[7]));

                        harbour[index].ParkedBoats.Add(catamaran);
                        harbour[index + 1].ParkedBoats.Add(catamaran);
                        harbour[index + 2].ParkedBoats.Add(catamaran);
                    }
                    break;

                case "Lastfartyg":
                    index = int.Parse(boatData[0]);

                    if (harbour[index].ParkedBoats.Count == 0)     // När resten av lastfartyget kommmer från foreach är det redan tillagt, annars hade det blivit kopior
                    {
                        CargoShip cargoship = new CargoShip(boatData[1], int.Parse(boatData[2]), int.Parse(boatData[3]),
                                                            int.Parse(boatData[5]), int.Parse(boatData[6]), int.Parse(boatData[7]));

                        harbour[index].ParkedBoats.Add(cargoship);
                        harbour[index + 1].ParkedBoats.Add(cargoship);
                        harbour[index + 2].ParkedBoats.Add(cargoship);
                        harbour[index + 3].ParkedBoats.Add(cargoship);
                    }
                    break;

                default:
                    break;
                }
            }
        }