// Sparar alla medlemmar till en textfil. public void SaveMembers(Member[] members) { Member[] membersList = members; int i = 0; string[] lines = new string[400]; foreach (Member memb in membersList) { if (memb != null) { lines[i] = memb.FirstName; i++; lines[i] = memb.LastName; i++; lines[i] = memb.MemberNumber.ToString(); i++; lines[i] = memb.SocialSecurityNumber.ToString(); i++; } } lines = lines.Where(h => !string.IsNullOrEmpty(h)).ToArray(); string membersPath = path + "/../../Members.txt"; System.IO.File.WriteAllLines(membersPath, lines); }
// Skapar en ny medlem när alla kriterier uppfylls. public void CreateNewMember(string[] regArr) { Member newMemb; do { newMemb = new Member(regArr[0], regArr[1], 0, int.Parse(regArr[2])); } while (IsMemberNumberValid(newMemb) == false); AddMember(newMemb); }
// Lägger till en medlem i katalogen public void AddMember(Member memberToAdd) { for (int i = 0; i < this.members.Length; i++) { if (this.members[i] == null) { this.members[i] = memberToAdd; break; } } }
// Räknar antal båtar en medlem äger. public int CountBoatsByMember(Member m) { int counter = 0; for (int i = 0; i < this.boats.Length; i++) { if (this.boats[i] != null && this.boats[i].OwnedBy == m) { counter++; } } return counter; }
// Visar alla båtar som en medlem äger. public int ListAllBoatsByMember(Member memb, string title) { if (this.boatCatalog.CountBoatsByMember(memb) == 0) { ShowErrorMessage("Inga båtar finns registrerade för vald medlem.", true); return 0; } else { return ListBoatsByMember(memb, title); } }
// 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; } } }
// Hämtar alla medlemmar från en textfil. public Member[] GetMembers() { Member[] allMembers = new Member[100]; string membersPath = path + "/../../Members.txt"; string[] lines = System.IO.File.ReadAllLines(membersPath); int count = 1; string firstName = ""; string lastName = ""; int membNumber = 0; int secNumber = 0; foreach (string line in lines)// foreach som löper igenom tex filen och skapar ett objekt som tilldelas värden från textfilen { if (count == 1) { firstName = line; } if (count == 2) { lastName = line; } if (count == 3) { membNumber = Int32.Parse(line); } if (count == 4) { count = 0; secNumber = Int32.Parse(line); Member newMember = new Member(firstName, lastName, membNumber, secNumber); allMembers = AddMemberToArray(allMembers, newMember); } count++; } return allMembers; }
// Tar bort en medlem från katalogen via ett medlemsnummer. public void DeleteMember(Member m) { Member[] membersTemporary = new Member[100]; for (int i = 0; i < this.members.Length; i++) { if (this.members[i] != null) { if (this.members[i].MemberNumber == m.MemberNumber) { membersTemporary = members.Where(w => w != members[i]).ToArray(); break; } } } members = new Member[100]; for (int i = 0; i < membersTemporary.Length; i++) { members[i] = membersTemporary[i]; } }
// 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]; } }
// Läser in information vid redigering av båt. public object[] ViewEditBoat(Member m, int boatToEditIndex) { ClearConsole(); ShowMessage("[ REDIGERA BÅT ]"); ShowMessage(PrintFullBoatWithNumberList(this.boatCatalog.GetBoatByMemberAndIndex(m,boatToEditIndex))); ShowMessage("\nVälj i listan vilken egenskap som skall ändras."); int selectedAlternative; do { int.TryParse(ReadInput(), out selectedAlternative); if (!(selectedAlternative >= 1 && selectedAlternative <= 3)) { ShowErrorMessage("Välj ett alternativ från listan.", true); } } while (!(selectedAlternative >= 1 && selectedAlternative <= 3)); ClearConsole(); ShowMessage("[ REDIGERA BÅT ]\n"); int inputInteger; bool inputIntegerResult = false; object[] returnArr = new object[2]; returnArr[0] = selectedAlternative; if (selectedAlternative == 1) { ListAllMembers(); ShowMessage("\nÄndra ägare till: "); returnArr[1] = ChooseMember(); inputIntegerResult = false; } else if (selectedAlternative == 2) { ShowMessage(ViewBoatTypes()); ShowMessage("Ändra typ till: "); inputIntegerResult = false; do { inputIntegerResult = int.TryParse(ReadInput(), out inputInteger); if (!inputIntegerResult || inputInteger < 1 || inputInteger > 5) { ShowErrorMessage("Välj ett alternativ från listan.", true); } } while (!inputIntegerResult || inputInteger < 1 || inputInteger > 5); returnArr[1] = inputInteger; } else { ShowMessage("Ändra längd till: "); do { inputIntegerResult = int.TryParse(ReadInput(), out inputInteger); if (!inputIntegerResult || inputInteger < 1) { ShowErrorMessage("Ange en giltlig längd.", true); } } while (!inputIntegerResult || inputInteger < 1); returnArr[1] = inputInteger; inputIntegerResult = false; } return returnArr; }
// Kontrollerar om ett medlemsnummer redan används. public bool IsMemberCorrect(Member m) { for (int i = 0; i < this.members.Length; i++) { if (this.members[i] != null) { if (this.members[i] == m) { return true; } } } return false; }
// Skapar en sträng med kompakt information om medlemmen. public string PrintMemberCompact(Member m) { return String.Format("\nNamn:\t\t{0}\nEfternamn:\t{1}\nMedlemsnummer\t{2}", m.FirstName, m.LastName, m.MemberNumber); }
// Skapar en sträng med medlemsinformation. public string PrintMemberListInfo(Member m) { return String.Format("{0} {1} ({2})", m.FirstName, m.LastName, m.MemberNumber); }
// Skapar en sträng med fullständig information om medlemmen. public string PrintFullMember(Member m) { return String.Format("\nNamn:\t\t{0}\nEfternamn:\t{1}\nPersonnummer:\t{2}\nMedlemsnummer:\t{3}", m.FirstName, m.LastName, m.SocialSecurityNumber, m.MemberNumber); }
// Skapar en numrerad sträng med fullständig information om medlemmen. public string PrintFullMemberWithNumberList(Member m) { return String.Format("\n1. Namn:\t\t{0}\n2. Efternamn:\t\t{1}\n3. Personnummer:\t{2}\n4. Medlemsnummer:\t{3}", m.FirstName, m.LastName, m.SocialSecurityNumber, m.MemberNumber); }
// Lägger till en medlem i katalogen vid hämtning av medlemmar från en textfil. private Member[] AddMemberToArray(Member[] membersArray, Member memberToAdd) { for (int i = 0; i < membersArray.Length; i++) { if (membersArray[i] == null) { membersArray[i] = memberToAdd; return membersArray; } } return membersArray; }
// Visar lista med båtar som en medlem äger. public int ListBoatsByMember(Member memb, string title) { ClearConsole(); ShowMessage(String.Format("[ {0} ]\n", title)); Boat[] boatsList = this.boatCatalog.GetBoats(); int listOfBoatsCounter = 0; for (int i = 0; i < boatsList.Length; i++) { if (boatsList[i] != null && boatsList[i].OwnedBy == memb) { listOfBoatsCounter++; ShowMessage(String.Format("{0}. {1} {2}m", listOfBoatsCounter, GetBoatTypeString(boatsList[i]), boatsList[i].Length)); } } ShowMessage("\nVälj i listan vilken båt som skall raderas."); bool validNumber = false; int number; do { validNumber = int.TryParse(ReadInput(), out number); if (!(number >= 1 && number <= listOfBoatsCounter)) { ShowErrorMessage("Välj en båt från listan.", true); } } while (!(number >= 1 && number <= listOfBoatsCounter)); return number; }
public Boat(int type, int length, Member ownedBy) { this._length = length; this._type = type; this._ownedBy = ownedBy; }
// Hämtar en medlem via en Member. public Member GetMemberByMember(Member m) { for (int i = 0; i < this.members.Length; i++) { if (this.members[i] != null) { if (this.members[i] == m) { return this.members[i]; } } } return null; }
// Tar bort alla båtar av en medlem från katalogen. public void DeleteBoats(Member m) { while (CountBoatsByMember(m) > 0) { DeleteBoat(m, 1); } }
// Kontrollerar om ett medlemsnummer är upptaget. public bool IsMemberNumberValid(Member memberToAdd) { for (int i = 0; i < this.members.Length; i++) { if (this.members[i] != null) { if (this.members[i].MemberNumber == memberToAdd.MemberNumber) { return false; } } } return true; }
// Ändrar en båt. public void EditBoat(object[] boatEditInfo, Member memberToEditBoatTo, int boatToEdit) { Boat boatEdit = GetBoatByMemberAndIndex(memberToEditBoatTo, boatToEdit); if ((int)boatEditInfo[0] == 1) { boatEdit.OwnedBy = membC.GetMemberByMemberNumber((int)boatEditInfo[1]); } else if ((int)boatEditInfo[0] == 2) { boatEdit.Type = (int)boatEditInfo[1]; } else { boatEdit.Length = (int)boatEditInfo[1]; } }
// Ändrar medlemsnumret på medlemmens båtar. public void EditBoatsMemberNumber(Member m, int newMemberNumber) { for (int i = 0; i < this.boats.Length; i++) { if (this.boats[i] != null && this.boats[i].OwnedBy == m) { this.boats[i].OwnedBy.MemberNumber = newMemberNumber; } } }
// Hämtar en båt via medlemsnummer och index. public Boat GetBoatByMemberAndIndex(Member m, int boatIndex) { int bi = 1; for (int i = 0; i < this.boats.Length; i++) { if (this.boats[i] != null && this.boats[i].OwnedBy == m) { if (bi == boatIndex) { return this.boats[i]; } bi++; } } return null; }