Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am a beginning programmer in C# and have a project for a class assignment where I have to create a program that calculates a payroll system. I choice a Piecework type payroll. I have the first few parts done, but can't seem to get it to pull up the correct UnitPay or calculate Gross, Withholdings, or Net Pay. below is a copy of my code as it is now (it is rough as I am still working on it). If anyone can give me help, it will be greatly appreciated. I have hit a brick wall and just can't get pass this point on my own.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _2ndPayrollUpgrade
{
    class Program
    {
        static void Main(string[] args)
        {
            // variables
            string SelectedOption;
            string EmployeeName = "";
            decimal TotalUnits = 0;
            decimal UnitRate = 0;

            do
            {
                Console.WriteLine("    ** PIECEWORK PAYROLL MENU **");
                Console.WriteLine("A - Employee Details");
                Console.WriteLine("B - Determine Rate Per Unit");
                Console.WriteLine("C - Calculate Pay and Display Results");
                Console.WriteLine("X - Exit");
                Console.WriteLine("");
                Console.Write("   Choose an option by typing the letter: ");
                SelectedOption = Console.ReadLine();

                if (SelectedOption.Equals("A") || SelectedOption.Equals("a"))
                { EmployeeDetails(out EmployeeName, out TotalUnits); }
                else if (SelectedOption.Equals("B") || SelectedOption.Equals("b"))
                { UnitRate = CalculateUnitPayRate(TotalUnits); }
                else if (SelectedOption.Equals("C") || SelectedOption.Equals("c"))
                { CalculatePayAndDisplayResults(out EmployeeName, out TotalUnits, out UnitRate); }
                else if (SelectedOption.Equals("X") || SelectedOption.Equals("x"))
                { Console.WriteLine("You requested to exit"); }
                else
                {
                    Console.WriteLine("\nI got " + SelectedOption + " But I did not know what to do");
                    Console.WriteLine("You will have to try again");
                    Console.Write("Press \"Enter\" to continue"); Console.ReadLine();
                }
                    Console.WriteLine("Name: {0}", EmployeeName);  // BBM
                    Console.WriteLine("TotalUnits: {0}", TotalUnits);  // BBM
                    Console.WriteLine("UnitRate: {0}", UnitRate);  // BBM
                    Console.WriteLine("Press enter to continue"); Console.ReadLine();

                   Console.Clear();  // Clears screen for new input/information

            } while (!SelectedOption.Equals("X") && !SelectedOption.Equals("x"));

            Console.WriteLine("\n * * * All Done * * * Press \"Enter\""); Console.ReadLine();

        }   // End of MAIN method

        static void EmployeeDetails(out string EnteredName, out decimal UnitsEarned)
        {
            // variables
            const decimal MIN_UNITS = 1;
            const decimal MAX_UNITS = 200;

            Console.Clear();

            Console.WriteLine("Enter Employee's Name: ");
            EnteredName = Console.ReadLine();

            Console.WriteLine("Enter Total Units Completed: ");
            UnitsEarned = isInputGood(MIN_UNITS, MAX_UNITS);
        }
        static decimal isInputGood(decimal MIN, decimal MAX)
        {
            // variables
            bool isInputGood;
            bool isRangeGood = false;
            decimal userInput;

            do
            {
                Console.Write("Enter A Valid Number Between {0} and {1}: ", MIN, MAX);
                isInputGood = (decimal.TryParse(Console.ReadLine(), out userInput));
                if (MIN > userInput || MAX < userInput)
                { isRangeGood = false; }
                if (MIN <= userInput && MAX >= userInput)
                { isRangeGood = true; }
            } while (isInputGood == false || isRangeGood == false);
            return userInput;

        }  // End of Employee Details Method
        static decimal CalculateUnitPayRate(decimal TotalUnits)
        {
            // variable
            decimal RatePerUnit = 0;

            if (TotalUnits > 0 || TotalUnits <= 25) { RatePerUnit = 10.00m; }
            else if (TotalUnits > 25 || TotalUnits <= 50) { RatePerUnit = 15.00m; }
            else if (TotalUnits > 50 || TotalUnits <= 75) { RatePerUnit = 20.00m; }
            else if (TotalUnits > 75) { RatePerUnit = 25.00m; }
            return RatePerUnit;
        }  // End of Rate Per Unit Method

        static void CalculatePayAndDisplayResults(out string EnteredName, out decimal TotalUnits, out decimal UnitRate)
        {
            

            Console.Clear();
            Console.Write("Press \"Enter\" to continue");
            EnteredName = Console.ReadLine();
            TotalUnits = Console.Read();
            UnitRate = Console.Read();

            
            Console.WriteLine();
            Console.WriteLine("Employee's Name: {0}", EnteredName);
            Console.WriteLine("Total Units: {0}", TotalUnits);
            Console.WriteLine("{0:$##.##}", UnitRate);

            // variables
            const decimal FedTaxRate = 0.12m;
            const decimal SocSecRate = 0.0765m;
            const decimal MedicareRate = 0.0145m;
            const decimal StateTaxRate = 0.08m;
            decimal GrossPay = 0;
            decimal FedIncTaxWH = 0;
            decimal SocSecWH = 0;
            decimal MedicareWH = 0;
            decimal StateIncTaxWH = 0;
            decimal TotalWH = 0;
            decimal NetPay = 0;

            GrossPay = TotalUnits * UnitRate;
            FedIncTaxWH = FedTaxRate * GrossPay;
            SocSecWH = SocSecRate * GrossPay;
            MedicareWH = MedicareRate * GrossPay;
            StateIncTaxWH = StateTaxRate * GrossPay;
            TotalWH = (FedIncTaxWH + SocSecWH + MedicareWH + StateIncTaxWH);
            NetPay = GrossPay - TotalWH;
                       
            Console.WriteLine("Gross Pay              : {0:$#,###.##}", GrossPay);
            Console.WriteLine("FIT Withheld           : {0:$#,###.##}", FedIncTaxWH);
            Console.WriteLine("SS Withheld            : {0:$#,###.##}", SocSecWH);
            Console.WriteLine("MC Withheld            : {0:$#,###.##}", MedicareWH);
            Console.WriteLine("SIT Withheld           : {0:$#,###.##}", StateIncTaxWH);
            Console.WriteLine("NetPay                 : {0:$#,###.##}", NetPay);
            Console.Read();

        } // End of Calculate Pay and Display Results Method}
    }
}


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 5-Dec-14 5:29am
v2
Comments
Richard MacCutchan 5-Dec-14 10:10am    
1. Please use <pre> tags around your code so we can read it easily.
2. Explain exactly where in the code the error occurs and what is not working.
ZurdoDev 5-Dec-14 11:41am    
For starters, use the debugger to step through your code line by line so you can see exactly what is happening.

Then, if you're still stuck, you'll need to explain exactly where you are stuck because we can't look at all this code and guess what your problem is.

start by replacing || with &&
C#
static decimal CalculateUnitPayRate(decimal TotalUnits)
       {
           // variable
           decimal RatePerUnit = 0;

           if (TotalUnits > 0 && TotalUnits <= 25) { RatePerUnit = 10.00m; }
           else if (TotalUnits > 25 && TotalUnits <= 50) { RatePerUnit = 15.00m; }
           else if (TotalUnits > 50 && TotalUnits <= 75) { RatePerUnit = 20.00m; }
           else if (TotalUnits > 75) { RatePerUnit = 25.00m; }
           return RatePerUnit;
       }  // End of Rate Per Unit Method


if you use || , If the first operand evaluates to true, the second operand isn't evaluated. so TotalUnits>0 you always end up with RatePerUnit = 10.00m;
refer : || operator[^]
 
Share this answer
 
v2
Thanks everyone. Will follow your suggestions, etc. when I get back from doing laundry. Unfortunately the laundromat doesn't have wifi where I can log in and do what I need to do (my program is save on school servers that I access remotely). Will be back later this afternoon to respond to questions and suggestions.

Roxy
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900