/// <summary> /// Creates a random name using the specified format. /// </summary> /// <param name="format">The name format.</param> /// <returns>A random name with the specified format.</returns> /// <include file='Docs/NameFormatsExample.xml' path='example' /> public static string FullName(NameFormats format) { lock (s_formatMapLock) { return(string.Join(" ", s_formatMap[format].Invoke())); } }
/// <summary> /// Returns the name of this <see cref="Person"/> in the specified /// format. /// </summary> /// <param name="nameFormat"> /// Format to generate and return /// </param> /// <returns> /// Returns the name of this <see cref="Person"/> in the /// specified <see cref="NameFormats"/>. /// </returns> public string GetFormattedName(NameFormats nameFormat) { if (nameFormat == NameFormats.FirstMiddleInitialLast) { if (!string.IsNullOrEmpty(this.MiddleName)) { return(string.Format("{0} {1}. {2}", this.FirstName, this.MiddleName.Substring(0, 1).ToUpper(), this.LastName)); } else { return(string.Format("{0} {1}", this.FirstName, this.LastName)); } } else if (nameFormat == NameFormats.LastFirstMiddleInitial) { if (!string.IsNullOrEmpty(this.MiddleName)) { return(string.Format("{0}, {1} {2}.", this.LastName, this.FirstName, this.MiddleName.Substring(0, 1).ToUpper())); } else { return(string.Format("{0}, {1}", this.LastName, this.FirstName)); } } return(string.Empty); }
/// <summary> /// Creates a random name using the specified format. /// </summary> /// <param name="format">The name format.</param> /// <returns>A random name with the specified format.</returns> /// <include file='Docs/NameFormatsExample.xml' path='example' /> public static string FullName(NameFormats format) { lock (s_formatMapLock) { return string.Join(" ", s_formatMap[format].Invoke()); } }
/** * This method is used to sort a list of names contained in a .txt file. It takes a file path and the format that * corresponds to the format of the names within the file. */ public void Sort(string path, NameFormats format, bool isAscending) { var names = ExtractNames(path, format); if (names == null) { return; } names = isAscending ? names.OrderBy(s => s.Forenames).ThenBy(s => s.Surname).ToArray() : names.OrderByDescending(s => s.Surname).ThenByDescending(s => s.Forenames).ToArray(); OutputToFile(path, names); }
public string GetName(NameFormats format) { switch (format) { case NameFormats.Last: return(name.LastName); case NameFormats.First: return((name.FirstName) ?? ""); case NameFormats.Type: return(type.Name); case NameFormats.First_Last: return((!(string.IsNullOrEmpty(name.FirstName)) ? name.FirstName + " " : "") + name.LastName); case NameFormats.Last_First: return(name.LastName + ((!string.IsNullOrEmpty(name.FirstName)) ? (", " + name.FirstName) : "")); case NameFormats.First_Last_Type: if (this.type_id == 3 || type_id == 4 || type_id == 5) { return(GetName(NameFormats.First_Last)); } else { return(GetName(NameFormats.First_Last) + ", " + type.Name); } case NameFormats.Last_First_Type: if (this.type_id == 3 || type_id == 4 || type_id == 5) { return(GetName(NameFormats.Last_First)); } else { return(GetName(NameFormats.Last_First) + " (" + type.Name + ")"); } default: return(""); } }
/** * Function for extracting the arguments from the args input in the main function, it takes the input string * array directly and extracts the flags from it and also the fullpaths. */ private static void ExtractArgs(string[] args) { var formatIndex = -1; _filePaths = new List <string>(); foreach ((string value, int i) in args.Select((value, i) => (value, i))) { if (value.StartsWith("-")) { switch (value.Substring(1)) { case "a": _ascending = true; break; case "d": _ascending = false; break; case "f": formatIndex = i + 1; break; default: Console.WriteLine("Error Unrecognised Flag, the Flag \"" + value + "\" is going to be ignored!"); break; } } else { if (i == formatIndex) { _format = StringToNameFormat(value); } else { _filePaths.Add(value); } } } }
/** * This function takes the inputted filepath and extracts the names from this file returning it as a Name object * array. */ private Name[] ExtractNames(string filePath, NameFormats format) { try { var fileLines = _fileWrapper.ReadAllLines(filePath); var names = new Name[fileLines.Length]; Console.WriteLine("Names from File \"" + filePath + "\":"); for (var i = 0; i < fileLines.Length; i++) { names[i] = new Name(fileLines[i], format); Console.WriteLine(names[i]); } Console.WriteLine("\n"); return(names); } catch (FileNotFoundException e) { Console.WriteLine("File Not Found Exception. Msg: " + e.Message); return(null); } }
private void TestSort(bool order, NameFormats format, string[] input, string[] output) { string testOutPath = "sorted" + TestFilePath; Mock <IFileWrapper> mockFileWrapper = new Mock <IFileWrapper>(); mockFileWrapper.Setup <string[]>(m => m.ReadAllLines(It.Is <string>(s => String.Compare(s, TestFilePath, StringComparison.Ordinal) == 0))).Returns(input); Mock <StreamWriter> streamWriter = new Mock <StreamWriter>(testOutPath); mockFileWrapper.Setup <StreamWriter>(m => m.GetStreamWriter(It.Is <string>(s => string.Compare(s, testOutPath, StringComparison.Ordinal) == 0))) .Returns(streamWriter.Object); global::NameSorter.NameSorter nameSorter = new global::NameSorter.NameSorter(mockFileWrapper.Object); nameSorter.Sort(TestFilePath, format, order); foreach (var line in output) { streamWriter.Verify(a => a.WriteLine(line), Times.Exactly(1)); } }
private void BuildNameFromRawString(string rawString, NameFormats format) { var nameComponents = Regex.Split(rawString, @"\s+"); var cleanComponents = SanitiseInputName(nameComponents); switch (format) { case NameFormats.InOrder: BuildNameFromInOrderString(cleanComponents); break; case NameFormats.Backwards: BuildNameFromBackwardsString(cleanComponents); break; case NameFormats.SurnameFirst: BuildNameFromSurnameFirstString(cleanComponents); break; default: Console.WriteLine("Error name format not recognised or implemented! Format used: " + format); break; } }
/// <summary> /// Create a name using a specified format. /// </summary> public static string FullName(NameFormats format) { return(string.Join(" ", FormatMap[format].Invoke())); }
/// <summary> /// Create a name using a specified format. /// </summary> public static string FullName(NameFormats format) { return System.String.Join(" ", _formatMap[format].Invoke()); }
public static string Name() { return(NameFormats.Random()); }