void Parse(string csvText) { // Split by new lines in rows. string[] rowContents = csvText.Split('\n'); // For each row. for (int r = 1; r < rowContents.Length; r++) { string rowContent = rowContents[r]; string[] fieldContents = rowContent.Split(','); Person person = new Person(r); // For each field in this row. for (int f = 0; f < fieldContents.Length; f++) { string fieldContent = fieldContents[f]; switch (f) { case 0: // First name. person.firstName = fieldContent; break; case 2: // Age. int age; bool parseSucceeded = int.TryParse(fieldContent, out age); if (parseSucceeded) { person.age = age; } break; case 3: // Had covid person.hadCovid = fieldContent.ToLower() == "yes"; break; } } // Parse covid relation level. Person.CovidRelationLevel covidRelationLevel = Person.CovidRelationLevel.None; if (fieldContents.Length > 6) { bool familyHadCovid = fieldContents[4].ToLower() == "yes"; bool familyOrFriendsHadCovid = fieldContents[5].ToLower() == "yes"; bool anyoneHadCovid = fieldContents[6].ToLower() == "yes"; if (familyHadCovid) { covidRelationLevel = Person.CovidRelationLevel.Family; } else if (familyOrFriendsHadCovid) { covidRelationLevel = Person.CovidRelationLevel.FamilyOrFriend; } else if (anyoneHadCovid) { covidRelationLevel = Person.CovidRelationLevel.Anyone; } } person.covidRelationLevel = covidRelationLevel; // Add to person list. _people.Add(person); } }
void Parse(string csvText) { string[] rowContents = csvText.Split('\n'); for (int i = 1; i < rowContents.Length; i++) { string rowContent = rowContents[i]; string[] fieldContents = rowContent.Split(','); Person person = new Person(i); for (int f = 0; f < fieldContents.Length; f++) { string fieldContent = fieldContents[f]; switch (f) { case 0: // First name person.firstName = fieldContent; break; case 1: person.lastName = fieldContent; // Last name break; case 2: // Age int age; if (int.TryParse(fieldContent, out age)) { person.age = age; } break; case 3: break; case 7: int postNumber; if (int.TryParse(fieldContent, out postNumber)) { person.postNumber = postNumber; } break; case 8: bool hasPet; if (StringToBool(fieldContent, out hasPet)) { person.hasPet = hasPet; } break; case 9: int cohabitantsCount; if (int.TryParse(fieldContent, out cohabitantsCount)) { person.cohabitantsCount = cohabitantsCount; } break; case 10: int steamGamesCount; if (int.TryParse(fieldContent, out steamGamesCount)) { person.steamGamesCount = steamGamesCount; } break; case 11: int siblingsCount; if (int.TryParse(fieldContent, out siblingsCount)) { person.siblingsCount = siblingsCount; } break; } } // Parse covid relation level. Person.CovidRelationLevel covidRelationLevel = Person.CovidRelationLevel.None; if (fieldContents.Length > 6) { bool familyHadCovid; bool familyOrFriendsHadCovid; bool anyoneHadCovid; if (StringToBool(fieldContents[4], out anyoneHadCovid) && anyoneHadCovid) { covidRelationLevel = Person.CovidRelationLevel.Anyone; } if (StringToBool(fieldContents[5], out familyOrFriendsHadCovid) && familyOrFriendsHadCovid) { covidRelationLevel = Person.CovidRelationLevel.FamilyOrFriend; } if (StringToBool(fieldContents[6], out familyHadCovid) && familyHadCovid) { covidRelationLevel = Person.CovidRelationLevel.Family; } } person.covidRelationLevel = covidRelationLevel; Debug.Log(person.covidRelationLevel); _people.Add(person); } }