コード例 #1
0
ファイル: Program.cs プロジェクト: damcodes/TaxCalculator
        static decimal CalcTaxForOneBracket(bool verbose, List <StateTaxInfo> stateTaxObjects, State state, decimal originalIncome, Employee employee)
        {
            StateTaxInfo currTaxBracket = stateTaxObjects[0];
            decimal      tax            = originalIncome * stateTaxObjects[0].TaxRate;

            if (verbose)
            {
                Console.WriteLine("==================================================\n");
                Console.WriteLine($"Computing state tax for ${originalIncome:0.00} earned in {state.Name}....\n");
                Console.WriteLine($"This state has only 1 tax bracket for {employee.Name}'s income. {employee.Name} claims ${originalIncome:0.00} in {state.Name}. \n" +
                                  $"With a tax rate of {stateTaxObjects[0].TaxRate * 100:0.00}% for income between ${stateTaxObjects[0].Floor:0.00} and ${stateTaxObjects[0].Ceil:0.00}, " +
                                  $"{employee.Name}'s taxes are calculated by multiplying their income by the tax rate.\n\ntaxes = " +
                                  $"(income)*(tax rate) = ({originalIncome})*({stateTaxObjects[0].TaxRate}) " +
                                  $"= ${originalIncome * stateTaxObjects[0].TaxRate:0.00}\n");
                Console.WriteLine($"                   Bracket 1                     ");
                Console.WriteLine("--------------------------------------------------");
                Console.WriteLine($"{"Tax Rate(%)",-10}{currTaxBracket.TaxRate * 100,19:0.00}");
                Console.WriteLine($"{"Floor(USD)",-10}{currTaxBracket.Floor,20:0.00}");
                Console.WriteLine($"{"Ceil(USD)",-10}{currTaxBracket.Ceil,20:0.00}");
                Console.WriteLine("--------------------------------------------------");
                Console.WriteLine($"\n{employee.Name} would owe ${tax:0.00} in {state.Name} state taxes on ${originalIncome:0.00}\n");
                Console.WriteLine("==================================================\n\n\n");
            }
            return(tax);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: damcodes/TaxCalculator
        static decimal CalcTaxForMultiBracket(bool verbose, List <StateTaxInfo> stateTaxObjects, State state, decimal originalIncome, Employee employee)
        {
            decimal ogIncome = originalIncome; // hold onto this to display at line 370
            decimal tax      = 0;

            switch (verbose) // lines 375-381 identical to lines 354-373 without any writing to the console
            {
            case true:
                Console.WriteLine("==================================================\n");
                Console.WriteLine($"Computing state tax for ${originalIncome:0.00} earned in {state.Name}....\n");
                Console.WriteLine($"{state.Name} has {stateTaxObjects.Count} tax brackets for {employee.Name}'s income. To calculate {employee.Name}'s taxes you must cummulatively " +
                                  $"sum the products of each tax rate with the portion of their" +
                                  $" income within it's range.\n");

                for (int i = stateTaxObjects.Count - 1; i >= 0; i--)     // starting at highest tax bracket and iterate down
                {
                    StateTaxInfo currTaxBracket = stateTaxObjects[i];
                    tax += (originalIncome - currTaxBracket.Floor) * currTaxBracket.TaxRate;     // subtract the floor of this bracket from your originalIncome and multiply the difference
                                                                                                 // by the tax rate of this bracket. Then add the product to tax
                    Console.WriteLine($"                  Bracket {i+1}                  ");
                    Console.WriteLine("--------------------------------------------------");
                    Console.WriteLine($"{"Tax Rate(%)",-10}{currTaxBracket.TaxRate*100,19:0.00}");
                    Console.WriteLine($"{"Floor(USD)",-10}{currTaxBracket.Floor,20:0.00}");
                    Console.WriteLine($"{"Ceil(USD)",-10}{currTaxBracket.Ceil,20:0.00}");
                    Console.WriteLine("\n\n");
                    Console.WriteLine($"{"Taxable Income", -10}{originalIncome - currTaxBracket.Floor,16:0.00}");
                    Console.WriteLine($"{"Tax", -10}{(originalIncome - currTaxBracket.Floor)*(currTaxBracket.TaxRate), 20:0.00}");
                    Console.WriteLine("--------------------------------------------------\n\n\n");

                    originalIncome = currTaxBracket.Floor;     // set originalIncome equal to the floor of the current bracket to be used as originalIncome for next tax bracket down
                }
                Console.WriteLine($"\n{employee.Name} would owe ${tax:0.00} in {state.Name} state taxes on ${ogIncome:0.00}\n");
                Console.WriteLine("==================================================\n\n\n");
                break;

            default:
                for (int i = stateTaxObjects.Count - 1; i > 0; i--)
                {
                    StateTaxInfo currTaxBracket = stateTaxObjects[i];
                    tax           += (originalIncome - currTaxBracket.Floor) * currTaxBracket.TaxRate;
                    originalIncome = currTaxBracket.Floor;
                }
                break;
            }
            return(tax);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: damcodes/TaxCalculator
        static List <StateTaxInfo> GetStateData(string filePath) // read from csv file and return list of StateTaxInfo objs
        {
            List <StateTaxInfo> statesTaxList = new();
            StreamReader        reader        = new(File.OpenRead(filePath));

            while (!reader.EndOfStream)
            {
                string csv = reader.ReadLine().Trim();
                try
                {
                    StateTaxInfo newState = ValidateCreateStateTaxInfo(csv);
                    statesTaxList.Add(newState); // if StateTaxInfo creation success, add to statesTaxList
                }
                catch (Exception)
                {
                    //Console.WriteLine(e.Message + '\n');
                    continue;
                }
            }
            return(statesTaxList);
        }