Exemplo n.º 1
0
        private void ImportFromCSVFile(string filePath)
        {
            FileCabinetServiceSnapshot snapshot   = new FileCabinetServiceSnapshot();
            Dictionary <int, string>   exceptions = new Dictionary <int, string>();
            List <string> lineExceptions          = new List <string>();
            int           recordsCount            = 0;

            using (StreamReader reader = new StreamReader(filePath))
            {
                snapshot.LoadFromCSV(reader, out recordsCount, out lineExceptions);
                this.Service.Restore(snapshot, out exceptions);
            }

            foreach (var lex in lineExceptions)
            {
                write(lex);
            }

            foreach (var ex in exceptions)
            {
                write($"Record #{ex.Key} was not imported.");
            }

            write($"{recordsCount - exceptions.Count} records were imported from {filePath}.");
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        protected override void Make(AppCommandRequest commandRequest)
        {
            (string format, string fileName) = CommandHandleBase.SplitParam(commandRequest.Parameters);
            if (!File.Exists(fileName))
            {
                Console.WriteLine($"File \"{fileName}\"is not exist.");
                return;
            }

            FileStream fileStream;

            try
            {
                fileStream = File.OpenRead(fileName);
            }
            catch (Exception)
            {
                Console.WriteLine("Can't open file");
                return;
            }

            int count    = 0;
            var snapshot = new FileCabinetServiceSnapshot();

            switch (format)
            {
            case "csv":
                snapshot.LoadFromCSV(fileStream);
                count = this.Service.Restore(snapshot);
                break;

            case "xml":
                snapshot.LoadFromXml(fileStream);
                count = this.Service.Restore(snapshot);
                break;

            default:
                Console.WriteLine("Format is not supported.");
                break;
            }

            Console.WriteLine($"{count} records were imported from {fileName}.");
        }