/// <summary>
        /// Method to read and parse the invoicedata.txt file
        /// </summary>
        /// <param name="lineData">file to parse</param>
        public void ReadLineData(string lineData)
        {
            string[] line    = lineData.Split('|', ':');
            Invoice  invoice = new Invoice();
            List <InvoiceDetailLine> items = new List <InvoiceDetailLine>();
            int element = NUMBER_OF_ELEMENTS;

            invoice.InvoiceNumber = line[element++];

            DateFormatter dateFormatter = new DateFormatter();

            invoice.InvoiceDate = dateFormatter.Date(line[element++]);

            // If line[element].Length does not equal TERMS_LENGTH invoice will not generate
            // and will return error message
            if (line[element].Length == TERMS_LENGTH)
            {
                invoice.DiscountAmount = int.Parse(line[element].Substring(0, 1));

                int discountDays = int.Parse(line[element++].Substring(1, 2));

                invoice.DiscountPeriod = discountDays;
                invoice.DiscountDate   = dateFormatter.AddDays(discountDays);
            }
            else
            {
                ConsolePrinter.PrintError("Error with data file. Unable to parse the invoice's terms " + line[element], " - An invoice not generated, terms must only be 3 numeric characters.");
                return;
            }

            while (line.Length > element)
            {
                InvoiceDetailLine item = new InvoiceDetailLine();

                item.Quantity           = int.Parse(line[element++]);
                item.SkuNumber          = line[element++];
                item.ItemDescription    = line[element++];
                item.ItemPrice          = decimal.Parse(line[element++]);
                item.ProvincialSalesTax = line[element++];

                // If items.Count is greater than MAXIMUM_NUMBER_OF_ITEMS invoice will not generate
                // and will return error message
                if (items.Count <= MAXIMUM_NUMBER_OF_ITEMS)
                {
                    items.Add(item);
                }
                else if (items.Count > MAXIMUM_NUMBER_OF_ITEMS)
                {
                    ConsolePrinter.PrintError("Error with data file. Unable to parse the invoice's items list", " - An invoice not generated, total of items must be less than or equal to " + items.Count);
                    return;
                }
            }

            invoice.Items = items.ToArray();
            invoices.Add(invoice);
        }
        /// <summary>
        /// Drives the program
        /// </summary>
        /// <param name="args">Command line arguments</param>
        static void Main(string[] args)
        {
            StreamReader streamReader = null;
            string       path         = string.Empty;
            string       lineData;

            if (args.Length > NUMBER_OF_ARGS)
            {
                path = args[NUMBER_OF_ARGS];
            }
            else
            {
                Console.WriteLine($"Usage: COMP2614Assign03 invoicedata.txt");
                return;
            }

            if (File.Exists(path))
            {
                try
                {
                    streamReader = new StreamReader(path);
                    InvoiceReader invoiceReader = new InvoiceReader();

                    while (streamReader.Peek() > DATA)
                    {
                        lineData = streamReader.ReadLine();
                        invoiceReader.ReadLineData(lineData);
                    }
                    ConsolePrinter print = new ConsolePrinter();

                    print.PrintInvoices(invoiceReader.Invoices.ToArray());
                }
                catch (Exception e)
                {
                    Console.WriteLine($"\n{e.Message}\n");
                }
                finally
                {
                    if (streamReader != null)
                    {
                        streamReader.Close();
                    }
                }
            }
            else
            {
                Console.WriteLine("\nFile not found.\n");
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            InvoiceCollection invoices = DataGenerator.GetInvoices();

            ConsolePrinter.ShowInvoice(invoices);
        }