// Lägger till en båt i katalogen. public void AddBoat(int type, int length, Member ownedBy) { Boat boatToAdd = new Boat(type, length, ownedBy); for (int i = 0; i < this.boats.Length; i++) { if (this.boats[i] == null) { this.boats[i] = boatToAdd; break; } } }
// Tar bort en båt från katalogen. public void DeleteBoat(Member m, int boatToDelete) { int btd = 1; Boat[] boatsTemporary = new Boat[100]; for (int i = 0; i < this.boats.Length; i++) { if (this.boats[i] != null && this.boats[i].OwnedBy == m) { if (btd == boatToDelete) { boatsTemporary = this.boats.Where(w => w != this.boats[i]).ToArray(); break; } btd++; } } this.boats = new Boat[100]; for (int i = 0; i < boatsTemporary.Length; i++) { this.boats[i] = boatsTemporary[i]; } }
// Hämtar alla båtar från en textfil. public Boat[] GetBoats() { Boat[] allBoats = new Boat[100]; string boatsPath = path + "/../../Boats.txt"; string[] lines = System.IO.File.ReadAllLines(boatsPath); int count = 1; int type = 0; int length = 0; int ownedBy = 0; foreach (string line in lines) { if (count == 1) { ownedBy = Int32.Parse(line); } if (count == 2) { length = Int32.Parse(line); } if (count == 3) { count = 0; type = Int32.Parse(line); Member newMemb = membC.GetMemberByMemberNumber(ownedBy); Boat newBoat = new Boat(type, length, newMemb); allBoats = AddBoatToArray(allBoats, newBoat); } count++; } return allBoats; }
// Sparar alla båtar till en textfil. public void SaveBoats(Boat[] boats) { Boat[] boatslist = boats; int i = 0; string[] lines = new string[400]; foreach (Boat bo in boatslist) { if (bo != null) { lines[i] = bo.OwnedBy.MemberNumber.ToString(); i++; lines[i] = bo.Length.ToString(); i++; lines[i] = bo.Type.ToString(); i++; } } lines = lines.Where(h => !string.IsNullOrEmpty(h)).ToArray(); string boatsPath = path + "/../../Boats.txt"; System.IO.File.WriteAllLines(boatsPath, lines); }
// Returnerar båttyp. public string GetBoatTypeString(Boat b) { if (b.Type == 1) { return "Motorbåt"; } if (b.Type == 2) { return "Segelbåt"; } if (b.Type == 3) { return "Roddbåt"; } if (b.Type == 4) { return "Kanot"; } return "Övrigt"; }
// Lägger till en båt i katalogen vid hämtning av båtar från en textfil. private Boat[] AddBoatToArray(Boat[] boatsArray, Boat boatToAdd) { for (int i = 0; i < boatsArray.Length; i++) { if (boatsArray[i] == null) { boatsArray[i] = boatToAdd; return boatsArray; } } return boatsArray; }
// Skapar en numrerad sträng med fullständig information om en båt. public string PrintFullBoatWithNumberList(Boat b) { return String.Format("\n1. Ägare:\t{0}\n2. Typ:\t\t{1}\n3. Längd:\t{2}", b.OwnedBy.MemberNumber, GetBoatTypeString(b), b.Length); }