public void can_split_on_name_spaces() { RsvpItem item = new RsvpItem { Name = "first last", Answers = new[] { "First Person1 Second Person2 Third Person3" }, Guests = 2 }; Attendee attendee = manager.GetAttendee(item, 0); Assert.AreEqual("First", attendee.FirstName); Assert.AreEqual("Person1", attendee.LastName); Attendee attendee1 = manager.GetAttendee(item, 1); Assert.AreEqual("Second", attendee1.FirstName); Assert.AreEqual("Person2", attendee1.LastName); Attendee attendee2 = manager.GetAttendee(item, 2); Assert.AreEqual("Third", attendee2.FirstName); Assert.AreEqual("Person3", attendee2.LastName); }
public Attendee GetAttendee(RsvpItem item, int guestNumber) { Attendee attendee = new Attendee(); string[] nameSplit = item.Name.Split(' '); //we hope that the first answer is the guests names string firstAnswer = item.Answers != null && item.Answers.Count > 0 ? item.Answers[0].Replace(Environment.NewLine, ",") : ""; if (!string.IsNullOrEmpty(firstAnswer)) { string proposedName = ""; if ((item.Guests > 0 && guestNumber > 0) && (!firstAnswer.Contains(",") || !firstAnswer.Contains(" and "))) { //will try spaces string[] spaceSplit = firstAnswer.Split(' '); if (spaceSplit.Length == (item.Guests * 2) + 2) { int index = guestNumber; if (guestNumber > 0) { index = guestNumber * 2; proposedName = spaceSplit[index] + " " + spaceSplit[index + 1]; } else { proposedName = spaceSplit[index] + " " + spaceSplit[index + 1]; } } } if (string.IsNullOrEmpty(proposedName)) { proposedName = firstAnswer .Split(new[] { ",", " and " }, 5, StringSplitOptions.RemoveEmptyEntries)[guestNumber] .Trim(); } string[] strings = proposedName.Split(' '); if (guestNumber == 0) { if (strings.Length < 2 || strings[0].Contains("myself")) { strings = nameSplit; } } ExtractName(strings, attendee); attendee.RSVPAnswer = firstAnswer; } else { ExtractName(nameSplit, attendee); } attendee.RSVPName = item.Name; attendee.RSVPGuests = item.Guests; return(attendee); }