示例#1
0
        private void AddRowingBoatsToHarbourCanvas(DockSpace harbourSpace, Canvas dockCanvas)
        {
            int addToTopAlign = 0;

            foreach (var boat in harbourSpace.ParkedBoats)
            {
                TextBox boatBox = new TextBox();

                boatBox.Width    = 45;
                boatBox.Height   = 12;
                boatBox.Text     = boat.IdNumber;
                boatBox.FontSize = 9;
                Canvas.SetLeft(boatBox, 37);
                Canvas.SetTop(boatBox, 26 + addToTopAlign + (harbourSpace.SpaceId * 26));

                dockCanvas.Children.Add(boatBox);
                addToTopAlign += 13;
            }
        }
示例#2
0
        public MainWindow()
        {
            InitializeComponent();

            if (File.Exists("BoatsInDock1.txt") == false)
            {
                FileStream fs1 = File.Create("BoatsInDock1.txt");
                fs1.Close();
            }

            var fileText = File.ReadLines("BoatsInDock1.txt", System.Text.Encoding.UTF7);


            DockSpace[] dock1 = new DockSpace[dock1Length];
            for (int i = 0; i < dock1.Length; i++)
            {
                dock1[i] = new DockSpace(i);
            }

            AddBoatsFromFileToDock(fileText, dock1);

            if (File.Exists("BoatsInDock2.txt") == false)
            {
                FileStream fs2 = File.Create("BoatsInDock2.txt");
                fs2.Close();
            }

            fileText = File.ReadLines("BoatsInDock2.txt", System.Text.Encoding.UTF7);

            DockSpace[] dock2 = new DockSpace[dock2Length];
            for (int i = 0; i < dock2.Length; i++)
            {
                dock2[i] = new DockSpace(i);
            }
            AddBoatsFromFileToDock(fileText, dock2);

            PrintHarbourTable(dock1, dock2);

            string dockName = "Kaj 1";

            dock1Canvas.Children.Clear();
            AddSpacesToDockCanvas(dockName, dock1.Length, dock1Canvas);
            PlaceBoatsInDockCanvas(dock1, dock1Canvas);

            dockName = "Kaj 2";
            dock2Canvas.Children.Clear();
            AddSpacesToDockCanvas(dockName, dock2.Length, dock2Canvas);
            PlaceBoatsInDockCanvas(dock2, dock2Canvas);


            List <Boat> boatsInDock1 = GenerateBoatsInHarbourList(dock1);
            List <Boat> boatsInDock2 = GenerateBoatsInHarbourList(dock2);

            var boatsInBothDocks = boatsInDock1
                                   .Concat(boatsInDock2);

            if (boatsInBothDocks.Count() > 0)
            {
                int    sumOfWeight          = GenerateSumOfWeight(boatsInBothDocks);
                double averageSpeed         = GenerateAverageSpeed(boatsInBothDocks);
                int    availableSpacesDock1 = CountAvailableSpaces(dock1);
                int    availableSpacesDock2 = CountAvailableSpaces(dock2);

                summaryListBox.Items.Clear();

                PrintSummaryOfBoats(boatsInBothDocks);
                summaryListBox.Items.Add("\n");

                PrintStatistics(sumOfWeight, averageSpeed, availableSpacesDock1, availableSpacesDock2);
            }

            StreamWriter sw1 = new StreamWriter("BoatsInDock1.txt", false, System.Text.Encoding.UTF7);

            SaveToFile(sw1, dock1);
            sw1.Close();

            StreamWriter sw2 = new StreamWriter("BoatsInDock2.txt", false, System.Text.Encoding.UTF7);

            SaveToFile(sw2, dock2);
            sw2.Close();
        }
示例#3
0
        private void NextDayButton_Click(object sender, RoutedEventArgs e)
        {
            var fileText = File.ReadLines("BoatsInDock1.txt", System.Text.Encoding.UTF7);

            DockSpace[] dock1 = new DockSpace[dock1Length]; // Hade velat använda längden på dock som skapades efter initialize
            for (int i = 0; i < dock1.Length; i++)
            {
                dock1[i] = new DockSpace(i);
            }

            AddBoatsFromFileToDock(fileText, dock1);

            fileText = File.ReadLines("BoatsInDock2.txt", System.Text.Encoding.UTF7);

            DockSpace[] dock2 = new DockSpace[dock2Length];
            for (int i = 0; i < dock2.Length; i++)
            {
                dock2[i] = new DockSpace(i);
            }

            AddBoatsFromFileToDock(fileText, dock2);

            List <Boat> boatsInDock1 = GenerateBoatsInHarbourList(dock1);
            List <Boat> boatsInDock2 = GenerateBoatsInHarbourList(dock2);

            AddDayToDaysSinceArrival(boatsInDock1);
            AddDayToDaysSinceArrival(boatsInDock2);

            bool boatRemoved = true;

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

            boatRemoved = true;
            while (boatRemoved)
            {
                boatRemoved = RemoveBoats(dock2);
            }

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

            List <Boat> arrivingBoats         = new List <Boat>();
            int         NumberOfArrivingBoats = 10;     // Det blir nästan aldrig fullt i hamnen om det kommer 5 båtar, ändrade till 10 för att vis att tabellen för avvisade båtar funkar

            AddNewBoats(arrivingBoats, NumberOfArrivingBoats);

            foreach (Boat boat in arrivingBoats)
            {
                bool boatParked;

                if (boat is RowingBoat)
                {
                    boatParked = RowingBoat.ParkRowingBoatInHarbour(boat, dock1, dock2);
                    if (boatParked == false)
                    {
                        rejectedRowingBoats++;
                    }
                }

                if (boat is MotorBoat)
                {
                    boatParked = MotorBoat.ParkMotorBoatInHarbour(boat, dock1, dock2);
                    if (boatParked == false)
                    {
                        rejectedMotorBoats++;
                    }
                }

                if (boat is SailingBoat)
                {
                    boatParked = SailingBoat.ParkSailingBoatInHarbour(boat, dock1, dock2);
                    if (boatParked == false)
                    {
                        rejectedSailingBoats++;
                    }
                }

                if (boat is Catamaran)
                {
                    boatParked = Catamaran.ParkCatamaranInHarbour(boat, dock1, dock2);
                    if (boatParked == false)
                    {
                        rejectedCatamarans++;
                    }
                }

                if (boat is CargoShip)
                {
                    boatParked = CargoShip.ParkCargoshipInHarbour(boat, dock1, dock2);
                    if (boatParked == false)
                    {
                        rejectedCargoShips++;
                    }
                }
            }

            PrintHarbourTable(dock1, dock2);

            string dockName   = "Kaj 1";
            int    dockLenght = dock1.Length;

            dock1Canvas.Children.Clear();
            AddSpacesToDockCanvas(dockName, dockLenght, dock1Canvas);
            PlaceBoatsInDockCanvas(dock1, dock1Canvas);

            dockName   = "Kaj 2";
            dockLenght = dock2.Length;
            dock2Canvas.Children.Clear();
            AddSpacesToDockCanvas(dockName, dockLenght, dock2Canvas);
            PlaceBoatsInDockCanvas(dock2, dock2Canvas);

            boatsInDock1 = GenerateBoatsInHarbourList(dock1);
            boatsInDock2 = GenerateBoatsInHarbourList(dock2);

            var boatsInBothDocks = boatsInDock1
                                   .Concat(boatsInDock2);

            if (boatsInBothDocks.Count() > 0)
            {
                int    sumOfWeight          = GenerateSumOfWeight(boatsInBothDocks);
                double averageSpeed         = GenerateAverageSpeed(boatsInBothDocks);
                int    availableSpacesDock1 = CountAvailableSpaces(dock1);
                int    availableSpacesDock2 = CountAvailableSpaces(dock2);

                summaryListBox.Items.Clear();
                PrintSummaryOfBoats(boatsInBothDocks);
                summaryListBox.Items.Add("\n");
                PrintStatistics(sumOfWeight, averageSpeed, availableSpacesDock1, availableSpacesDock2);
                summaryListBox.Items.Add("");
                PrintRejectedBoats(rejectedRowingBoats, rejectedMotorBoats, rejectedSailingBoats, rejectedCatamarans, rejectedCargoShips);
            }

            StreamWriter sw1 = new StreamWriter("BoatsInDock1.txt", false, System.Text.Encoding.UTF7);

            SaveToFile(sw1, dock1);
            sw1.Close();

            StreamWriter sw2 = new StreamWriter("BoatsInDock2.txt", false, System.Text.Encoding.UTF7);

            SaveToFile(sw2, dock2);
            sw2.Close();
        }