Пример #1
0
        static bool IsTaxSale(InvoiceClass invoice)
        {
            decimal itemsTotal = 0;

            for (int i = 0; i < invoice.InvoiceItemArray.Count(); i++)
            {
                itemsTotal += invoice.InvoiceItemArray[i].ItemPrice;
            }

            // If the items total = the invoice total, then it was not a tax sale.
            // The difference comes from the taxed amount.
            if (itemsTotal == invoice.InvoiceTotal)
            {
                // Not taxed - IsTaxSale = False
                return(false);
            }
            else
            {
                // Taxed - IsTaxeSale = True
                return(true);
            }
        }
Пример #2
0
        static List <InvoiceClass> ReadInvoiceFile(string invoiceFile)
        {
            string prevInvoiceNumber = "0";

            List <InvoiceClass> invoiceList = new List <InvoiceClass>();
            List <ItemsClass>   itemsList   = new List <ItemsClass>();


            var format = new NumberFormatInfo();

            format.NegativeSign = "-";


            /*
             * i is used to iterate through invoiceList. If an invoice number is the same
             * as the previous invoice number (prevInvoiceNumber), it does not increment. This creates in object
             * that includes all items with the same invoice number.
             * - invoice #12345 might include multiple items
             */
            int i = -1;



            try
            {
                /*
                 * using TextFieldParser from Visual Basic to parse the CSV file
                 * I originally had a problem parsing company names that contained commas.
                 * This is the simplest method I found to ignore anything between double quotes.
                 */
                using (Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(invoiceFile))
                {
                    parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
                    parser.SetDelimiters(",");

                    while (!parser.EndOfData)
                    {
                        string[] words = parser.ReadFields();

                        InvoiceClass invoice = new InvoiceClass(0, "", itemsList, 0);


                        // Only continue if the first item in the words list does not contain any letters.
                        // This is to skip anything that doesn't start with an invoice number.
                        // Usually the first two lines and the last line of the file should be skipped.
                        if (!words[0].Any(Char.IsLetter))
                        {
                            if (words[0] != prevInvoiceNumber)
                            {
                                List <ItemsClass> items = new List <ItemsClass>();

                                // words[2] is the item name words[3] is the item price.
                                items.Add(new ItemsClass(words[2], decimal.Parse(words[3], format)));


                                // words[0] is the item number. words[1] is the invoice name
                                invoiceList.Add(new InvoiceClass(int.Parse(words[0]), words[1], items, 0));


                                i++;


                                prevInvoiceNumber = words[0];
                            }
                            else
                            {
                                // words[2] is the item name. words[3] is the item price.
                                invoiceList[i].InvoiceItemArray.Add(new ItemsClass(words[2], decimal.Parse(words[3], format)));

                                prevInvoiceNumber = words[0];
                            }
                        }
                    }

                    parser.Close();
                }
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return(invoiceList);
        }