/// <summary> /// get file name/path from command parameter,otherwise display an error mesasge /// </summary> /// <param name="args">commad parameters</param> /// <param name="writeFilePath"></param> /// <returns></returns> public static NamesFile GetFile(string[] args, string writeFilePath) { NamesFile file = null; // //first command parameter is file name/path for reading names // int argsReadFileElementIndex = 0; // //create a error message object from the factory // IMessage errorMessage = MessageFactory.CreatMessage(Message.MessageType.Error); // //if command parameter is not suppied or file does not exist,give an error message on screen. //otherwise return the file object // if (args.Length == 0 || string.IsNullOrEmpty(args[argsReadFileElementIndex])) { errorMessage.DisplayMessage("No unsorted names file path being supplied!"); } else if (!File.Exists(args[argsReadFileElementIndex])) { errorMessage.DisplayMessage("File not existed " + args[argsReadFileElementIndex]); } else { file = new NamesFile(args[argsReadFileElementIndex], writeFilePath); } return(file); }
/// <summary> /// Write the list of name to storage /// </summary> /// <param name="file">file name and path</param> /// <param name="names">list of name object</param> public static void WriteNamesToStorage(NamesFile file, List <Name> names) { string namesString; // //create storage and message object from the factory // IStorage storage = StorageFactory.CreatStorage(file); IMessage promptMessage = MessageFactory.CreatMessage(Message.MessageType.Prompt); // //convert list of name object to a string // namesString = NamesStringConverter.ConvertNamesToString(names); // //write names string to storage // storage.WriteData(namesString); // //give user a message of writing completion at the end // promptMessage.DisplayMessage("Writing data to " + file.WriteFilePath + " is completed"); }
/// <summary> /// Help read data from a storage; convert data into list of name object /// </summary> /// <param name="file">storage file</param> /// <returns></returns> public static List <Name> ReadNamesFromStorage(NamesFile file) { string namesString; List <Name> names; // //create storage and message object from the respective factories // IStorage storage = StorageFactory.CreatStorage(file); IMessage promptMessage = MessageFactory.CreatMessage(Message.MessageType.Prompt); // //read name list into a string from a storage // namesString = storage.ReadData(); // //make a list of name object from the names in string // names = NamesStringConverter.ConvertStringToNames(namesString); // //give a reading completion message on screen // promptMessage.DisplayMessage("Reading data from " + file.ReadFilePath + " is completed"); return(names); }
static void Main(string[] args) { string FileNamePathToWrite; ILogger logger = LogFactory.CreatLogger(); IMessage errorMessage = MessageFactory.CreatMessage(Message.MessageType.Error); IDisplay display; NamesFile file; List <Name> names; try { // //Get file name/path from app.config // FileNamePathToWrite = ConfigurationManager.AppSettings["FileNamePathToWrite"]; // //create screen display object from the factory // display = DisplayFactory.CreatDisplay(); // //get file name and path for reading names from command parameter; file name and path for writing names to is set by program // file = FileInfoValidator.GetFile(args, FileNamePathToWrite); // //only proceed further if file is available // if (file != null) { // // get list of name object by reading // names = ReadNamesHelper.ReadNamesFromStorage(file); // //do sorting on names // names = NameSorter.Sort(names); // //writing sorted names to a storage // WriteNamesHelper.WriteNamesToStorage(file, names); // //display sorted name list on screen // display.DisplayName(names); } } catch (Exception ex) { logger.Log(ex.Message); errorMessage.DisplayMessage(ex.Message); } }