Exemple #1
0
        static void Main(string[] args)
        {
            var myStaff = new List <Staff>();
            var fr      = new FileReader();
            int month   = 0;
            int year    = 0;

            while (year == 0)
            {
                Console.WriteLine("\nPlease enter the year: ");
                try
                {
                    year = Convert.ToInt32(Console.ReadLine());
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message + "Please try again.");
                }
            }

            while (month == 0)
            {
                Console.WriteLine("\nPlease enter the month: ");
                try
                {
                    month = Convert.ToInt32(Console.ReadLine());
                    if (month < 1 || month > 12)
                    {
                        Console.WriteLine("Month must be between 1 and 12. Please try again.");
                        month = 0;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message + "Please try again.");
                }
            }

            myStaff = fr.ReadFile();

            for (int i = 0; i < myStaff.Count; i++)
            {
                try
                {
                    Console.WriteLine("\nEnter hours worked for {0}", myStaff[i].NameOfStaff);
                    myStaff[i].HoursWorked = Convert.ToInt32(Console.ReadLine());
                    myStaff[i].CalculatePay();
                    Console.WriteLine(myStaff[i].ToString());
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    i--;
                }
            }

            var ps = new PaySlip(month, year);

            ps.GeneratePaySlip(myStaff);
            ps.GenerateSummary(myStaff);
            Console.Read();
        }
        static void Main(string[] args)
        {
            // create a new list of staff objects called myStaff
            List <Staff> myStaff = new List <Staff>();
            // create a new FileWriter object called fw
            FileWriter fw = new FileWriter();
            // create a new FileReader object called fr
            FileReader fr = new FileReader();
            // set month and year equal to 0
            int month = 0;
            int year  = 0;

            // Write out the title of the program
            Console.Write("Welcome to the Pay Slip Generator!!");

            // while loop to get the year from the user
            while (year == 0)
            {
                // prompt the user to submit the year
                Console.Write("\nPlease enter the year: ");

                // convert the user input to an integer and store the year value
                try
                {
                    string userInput = Console.ReadLine();
                    year = Convert.ToInt32(userInput);
                }

                // if the format is not valid, print an error message
                catch (FormatException)
                {
                    Console.WriteLine("Please enter a valid year.");
                }
            }

            // while loop to get the month from the user
            while (month == 0)
            {
                // prompt the user to submit the month
                Console.Write("\nPlease enter the month: ");

                // convert the user input to an integer and store the month value
                try
                {
                    string userInput = Console.ReadLine();
                    month = Convert.ToInt32(userInput);

                    // if the month is less than 1 or greater than 12, print error message and reset month to 0
                    if (month < 1 || month > 12)
                    {
                        Console.WriteLine("Input is invalid. Please enter a valid month.");
                        month = 0;
                    }
                }

                // if the format is not valid, print error message
                catch (Exception e)
                {
                    Console.WriteLine(e.Message + "Please try again.");
                }
            }


            // call the WriteFile method
            fw.WriteFile();

            // set the myStaff Staff object equal to the return value of the ReadFile method
            myStaff = fr.ReadFile();

            // loop through each staff member and prompt the user to submit the number of hours worked
            for (int i = 0; i < myStaff.Count; i++)
            {
                try
                {
                    // prompt the user to enter the number of hours worked for the staff member
                    Console.WriteLine("Enter hours worked for {0}", myStaff[i].NameOfStaff);
                    // read in the number of hours and convert to an integer
                    string userInput = Console.ReadLine();
                    myStaff[i].HoursWorked = Convert.ToInt32(userInput);
                    // calculate the pay for the staff member and print out the detailed results
                    myStaff[i].CalculatePay();
                    Console.WriteLine(myStaff[i].ToString());
                }

                // if there is an invalid input, print an error message, reset i to start the loop over
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    i--;
                }
            }

            // create a new PaySlip object and pass in the month and year values from the user
            PaySlip ps = new PaySlip(month, year);

            // generate the payslip and summary files
            ps.GeneratePaySlip(myStaff);
            ps.GenerateSummary(myStaff);
            // keep results on the output of the terminal
            Console.Read();
        }
Exemple #3
0
        static void Main(string[] args)
        {
            var myStaff    = new List <Staff>();
            var fileReader = new FileReader();
            int month      = 0;
            int year       = 0;

            while (year == 0)
            {
                Console.Write("Please enter the year between 1970 and 2020: ");
                try
                {
                    var input = Convert.ToInt32(Console.ReadLine());
                    if (input > 1970 && input <= 2020)
                    {
                        year = input;
                    }
                    else
                    {
                        Console.WriteLine("Invalid number");
                    }
                }
                catch (System.Exception)
                {
                    Console.WriteLine("Not a number");
                }
            }

            while (month == 0)
            {
                Console.Write("Please enter the month number: ");
                try
                {
                    var input = Convert.ToInt32(Console.ReadLine());
                    if (input >= 1 && input <= 12)
                    {
                        month = input;
                    }
                    else
                    {
                        Console.WriteLine("Invalid number");
                    }
                }
                catch (System.Exception)
                {
                    Console.WriteLine("Not a number");
                }
            }

            myStaff = fileReader.ReadFile();

            for (int i = 0; i < myStaff.Count; i++)
            {
                try
                {
                    Console.WriteLine();
                    Console.Write($"Enter hours worked for {myStaff[i].NameOfStaff}: ");
                    myStaff[i].HoursWorked = Convert.ToInt32(Console.ReadLine());
                    myStaff[i].CalculatePay();
                    Console.WriteLine(myStaff[i].ToString());
                }
                catch (System.Exception)
                {
                    Console.WriteLine("Invalid number");
                    i--;
                }
            }

            var paySlip = new PaySlip(month, year);

            paySlip.GeneratePaySlip(myStaff);
            paySlip.GenerateSummary(myStaff);

            Console.WriteLine();
            Console.WriteLine("Press any key to exit...");
            Console.ReadLine();
        }