//Edits a current members info. public void EditMember(Member member) { List<Member> CurrMembers = ReadMembers(); for (int i = 0; i < CurrMembers.Count; i++) { if (CurrMembers[i].MemberID == member.MemberID){ CurrMembers[i] = member; //object with old data Save(CurrMembers); //object with new data break; } } }
//Adds a boat to a existing member public void AddnewBoat(Member membertoaddboat) { Console.WriteLine("What kind of boat ?"); boatnumber = 0; for (i = 0; i < diffrentBoatTypes.Length; i++) { boatnumber++; Console.WriteLine("{0}. {1}", boatnumber, diffrentBoatTypes[i]); } int choice = EditMenu() - 1; typeofboat = diffrentBoatTypes[choice]; Console.WriteLine("New length:"); boatLength = int.Parse(Console.ReadLine()); }
//Reads all members in the database.txt file and returns a List of Member objects. public List<Member> ReadMembers() { Members = new List<Member>(100); MemberBoats = new List<Boat>(5); Name = null; using (StreamReader read = new StreamReader(_path)) { string reader; MemberReadStatus CurrentStatus = MemberReadStatus.Indefinite; while ((reader = read.ReadLine()) != null){ if (reader != "") { if (reader == sectionMemberName) { CurrentStatus = MemberReadStatus.MemberFirstName; } else if (reader == sectionMemberLastName) { CurrentStatus = MemberReadStatus.MemberLastName; } else if (reader == sectionMemberID) { CurrentStatus = MemberReadStatus.MemberID; } else if (reader == sectionPersonalNumber) { CurrentStatus = MemberReadStatus.MemberPersonalNumber; } else if (reader == sectionBoats) { CurrentStatus = MemberReadStatus.Boats; } else if(reader == sectionEnd && Name != null){ Member member = new Member(Name, LastName, PersonalNumber, MemberBoats, MemberID); Members.Add(member); //clears the list for the next member object MemberBoats = new List<Boat>(5); } else { //Read data here. switch (CurrentStatus) { case MemberReadStatus.MemberFirstName: //if name isent null a person just was read in. if (Name != null) { Member member = new Member(Name, LastName, PersonalNumber, MemberBoats, MemberID); Members.Add(member); //create new member object and att it to the list //clears the list for the next member object MemberBoats = new List<Boat>(5); } Name = reader; break; case MemberReadStatus.MemberLastName: LastName = reader; break; case MemberReadStatus.MemberID: //GuidConverter conver = new GuidConverter() MemberID = reader; break; case MemberReadStatus.MemberPersonalNumber: PersonalNumber = reader; break; case MemberReadStatus.Boats: string[] BoatInfo = reader.Split(new char[] { ';' }, StringSplitOptions.None); MemberBoats.Add(new Boat(double.Parse(BoatInfo[0]), BoatInfo[1])); break; default: throw new Exception(); } } } } } return Members; }
//adds @member parameter to the .txt file public void AddMember(Member member) { List<Member> CurrMembers = ReadMembers(); CurrMembers.Add(member); Save(CurrMembers); }
//removes a registerd member public void RemoveMember(Member member) { List<Member> CurrMembers = ReadMembers(); foreach (Member m in CurrMembers) { //it is EXREMLY unlikely 2 members have the same ID, feel free to check up GUID's odds to generate same ID if (m.MemberID == member.MemberID) { CurrMembers.Remove(m); Save(CurrMembers); break; } } }
//removes a boat from an existing member public void DeleteBoat(Member _toDelete) { Console.WriteLine("Choose a boat from the person that you would like to remove:"); i = 0; boatnumber = 0; foreach (Boat boat in _toDelete.Boats) { boatnumber++; Console.WriteLine("{0} Length: {1}m, Type: {2}", boatnumber, boat.Length, boat.BoatType); } b = EditMenu() - 1; Console.WriteLine("Current boatchoice name: {0}, Length {1}", _toDelete.Boats[b].BoatType, _toDelete.Boats[b].Length); updateMess = string.Format("The boat {0} with the length {1} was removed from {2} {3}", _toDelete.Boats[b].BoatType, _toDelete.Boats[b].Length, _toDelete.FirstName, _toDelete.LastName); }
//Used to edit member data public void editPersonalInfo(Member membertoedit) { Console.WriteLine("Current first name: {0}", membertoedit.FirstName); Console.WriteLine("Current last name: {0}", membertoedit.LastName); Console.WriteLine("Current personal number: {0}", membertoedit.PersonalNumber); Console.WriteLine("New Firstname"); firstname = Console.ReadLine(); Console.WriteLine("New Lastname"); lastname = Console.ReadLine(); Console.WriteLine("New PersonID"); persnr = Console.ReadLine(); updateMess = String.Format("You have just updated member\n {0}, {1}\n With personID: {2}\n To {3}, {4}\n With PersonID: {5}\n", membertoedit.FirstName, membertoedit.LastName, membertoedit.PersonalNumber, firstname, lastname, persnr); }
public int EditMemberInfo(Member m) { int choice = int.Parse(Console.ReadLine()); return choice; }
//Used to edit boat data public void editBoatInfo(Member _membertoedit) { int i = 0; foreach (Boat boat in _membertoedit.Boats) { i++; Console.WriteLine("{0} Length: {1}m, Type: {2}", i, boat.Length, boat.BoatType); } b = EditMenu() - 1; Console.WriteLine("Current boatchoice name: {0}, Length {1}", _membertoedit.Boats[b].BoatType, _membertoedit.Boats[b].Length); Console.WriteLine("New Boattype:"); boatnumber = 0; for ( i = 0; i < diffrentBoatTypes.Length; i++) { boatnumber++; Console.WriteLine("{0}. {1}",boatnumber, diffrentBoatTypes[i]); } int choice = EditMenu() - 1; typeofboat = diffrentBoatTypes[choice]; Console.WriteLine("New length:"); boatLength = double.Parse(Console.ReadLine()); updateMess = String.Format("You have just updated the boat {0} with the length: {1}\n To the boat {2} with the Length {3}", _membertoedit.Boats[b].BoatType, _membertoedit.Boats[b].Length, typeofboat, boatLength); }
//returns a member that gonna be deleted public Member deleteperson(Member toDelete) { return toDelete; }
public void MenuChoice() { switch (choice) //Mainmenus switch, CRUD { case 0: Environment.Exit(0); break; case 1: try { List<Boat> boats = CreateMemberBoats(view.registerperson()); // CREATE Member newMember = new Member(view.getfirstname, view.getlastname, view.getpersnr, boats); memberDAL.AddMember(newMember); } catch { view.ValidationError(); } Console.Clear(); break; case 2: //UPDATE try { Console.Clear(); Member m = view.editperson(memberDAL.ReadMembers()); switch (view.EditMenu()) { case 1: //updates person data try { view.editPersonalInfo(m); m.FirstName = view.getfirstname; m.LastName = view.getlastname; m.PersonalNumber = view.getpersnr; } catch { view.ValidationError(); } break; case 2: //Updates boat data try { view.editBoatInfo(m); boattoedit = view.getcurrentboat; m.Boats[boattoedit].Length = view.getboatlength; m.Boats[boattoedit].BoatType = view.getboattype; } catch { view.ValidationError(); } break; case 3: try { view.AddnewBoat(m); m.Boats.Add(new Boat(view.getboatlength, view.getboattype)); } catch { view.ValidationError(); } break; } memberDAL.EditMember(m); } catch { view.EditError(); } break; case 3: //DELETE try { Console.Clear(); Member toDelete = view.Deletechoice(memberDAL.ReadMembers()); switch (view.EditMenu()) { case 1: //removes a member try{ view.deleteperson(toDelete); memberDAL.RemoveMember(toDelete); } catch { view.ValidationError(); } break; case 2: //removes a boat try { view.DeleteBoat(toDelete); toDelete.Boats.RemoveAt(view.getcurrentboat); memberDAL.EditMember(toDelete); } catch { view.ValidationError(); } break; } } catch { view.EditError(); } Console.Clear(); break; case 4: //VIEW try { Console.Clear(); view.Viewperson(memberDAL.ReadMembers()); switch (view.EditMenu()) { case 1: try { view.listpersoncompact(memberDAL.ReadMembers()); } catch { view.ValidationError(); } break; case 2: try { view.listpersonverbose(memberDAL.ReadMembers()); } catch { view.ValidationError(); } break; case 3: try{ view.listpersonspecific(memberDAL.ReadMembers()); } catch { view.ValidationError(); } break; } } catch { view.EmptyDataBaseError(); } break; } Runapplication(); }