private static void AddNewBoats(List <Boat> boats, int newBoats) { for (int i = 0; i < newBoats; i++) { int boatType = Utils.random.Next(4 + 1); switch (boatType) { case 0: RowingBoat.AddRowingBoat(boats); break; case 1: MotorBoat.AddMotorBoat(boats); break; case 2: SailingBoat.AddSailingBoat(boats); break; case 3: Catamaran.AddCatamaran(boats); break; case 4: CargoShip.AddCargoShip(boats); break; } } }
internal static bool ParkMotorBoatInHarbour(Boat boat, DockSpace[] dock1, DockSpace[] dock2) { bool boatParked; while (true) { int selectedSpace; (selectedSpace, boatParked) = RowingBoat.FindSingleSpaceBetweenOccupiedSpaces(dock1); if (boatParked) { dock1[selectedSpace].ParkedBoats.Add(boat); break; } (selectedSpace, boatParked) = RowingBoat.FindSingleSpaceBetweenOccupiedSpaces(dock2); if (boatParked) { dock2[selectedSpace].ParkedBoats.Add(boat); break; } (selectedSpace, boatParked) = RowingBoat.FindFirstFreeSpace(dock1); if (boatParked) { dock1[selectedSpace].ParkedBoats.Add(boat); break; } (selectedSpace, boatParked) = RowingBoat.FindFirstFreeSpace(dock2); if (boatParked) { dock2[selectedSpace].ParkedBoats.Add(boat); break; } break; } return(boatParked); }
private void AddBoatsFromFileToDock(IEnumerable <string> fileText, DockSpace[] dock) { // File: spaceId;Id;Weight;MaxSpeed;Type;DaysStaying;DaySinceArrival;Special // Index: 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]); RowingBoat rowingBoat = new RowingBoat(boatData[1], int.Parse(boatData[2]), int.Parse(boatData[3]), int.Parse(boatData[5]), int.Parse(boatData[6]), int.Parse(boatData[7])); dock[index].ParkedBoats.Add(rowingBoat); break; case "Motorbåt": index = int.Parse(boatData[0]); MotorBoat motorBoat = new MotorBoat(boatData[1], int.Parse(boatData[2]), int.Parse(boatData[3]), int.Parse(boatData[5]), int.Parse(boatData[6]), int.Parse(boatData[7])); dock[index].ParkedBoats.Add(motorBoat); break; case "Segelbåt": index = int.Parse(boatData[0]); if (dock[index].ParkedBoats.Count == 0) // När andra halvan av segelbåten kommmer från foreach är den redan tillagd, se 6 rader ned { 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])); dock[index].ParkedBoats.Add(sailingBoat); // samma båt täcker två platser dock[index + 1].ParkedBoats.Add(sailingBoat); } break; case "Katamaran": index = int.Parse(boatData[0]); if (dock[index].ParkedBoats.Count == 0) // När resten av lastfartyget kommmer från foreach är det redan tillagt, se 6 rader ned { 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])); dock[index].ParkedBoats.Add(catamaran); // samma båt täcker tre platser dock[index + 1].ParkedBoats.Add(catamaran); dock[index + 2].ParkedBoats.Add(catamaran); } break; case "Lastfartyg": index = int.Parse(boatData[0]); if (dock[index].ParkedBoats.Count == 0) // När resten av lastfartyget kommmer från foreach är det redan tillagt, se 6 rader ned { 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])); dock[index].ParkedBoats.Add(cargoship); // samma båt täcker fyra platser dock[index + 1].ParkedBoats.Add(cargoship); dock[index + 2].ParkedBoats.Add(cargoship); dock[index + 3].ParkedBoats.Add(cargoship); } break; default: break; } } }
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(); }