Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace PracTest5
- {
- class Pay
- {
- static void Main()
- {
- // Define and initialise all variables
- string full_name = "";
- string street_number = "";
- string address = "";
- string state = "";
- string post_code = "";
- string phone_number = "";
- float hours_worked = 0.0F;
- int salary = 0;
- float gross_pay = 0.0F;
- float tax = 0.0F;
- float net_pay = 0.0F;
- float pay_rate = 0.0F;
- float tax_rate = 0.0F;
- // stores the user response for the menu.
- // Must be initialised to not "x" to ensure
- // the menu is displayed atleast once
- string response = "";
- // Tempory variable used to store ReadLine value
- string vtemp = "";
- bool parseAttempt = true;
- // The following tewo variables are used as part of the post code validation routine.
- string first_character;
- bool valid_post_code;
- // Beginning of main menu loop
- while (response != "x" && response != "X") // Check for both upper and lower case
- {
- // Clear the ascreen then display the menu
- Console.Clear();
- Console.WriteLine("------- Menu ------------");
- Console.WriteLine("Get a persons details (p)");
- Console.WriteLine("Get the Salary (s)");
- Console.WriteLine("Calculate and display (d)");
- Console.WriteLine("Exit (x)");
- Console.Write("\nEnter your choice : > ");
- // Get the response from the user
- response = Console.ReadLine();
- // Switch the response and execute the appropriate code
- switch (response)
- {
- // Get a persons details (p)
- case "p":
- case "P":
- // initialise variables
- full_name = "";
- street_number = "";
- address = "";
- state = "";
- post_code = "";
- phone_number = "";
- hours_worked = 0.0F;
- // Get data from user
- do
- {
- Console.Write("Enter your Full Name: > ");
- full_name = Console.ReadLine();
- if (full_name == "")
- Console.WriteLine("\r\nMust not be left blank");
- } while (full_name == "");
- do
- {
- Console.Write("Enter your Street number: > ");
- street_number = Console.ReadLine();
- if (street_number == "")
- Console.WriteLine("\r\nMust not be left blank");
- } while (street_number == "");
- do
- {
- Console.Write("Enter your Street address: > ");
- address = Console.ReadLine();
- if (address == "")
- Console.WriteLine("\r\nMust not be left blank");
- } while (address == "");
- do
- {
- Console.Write("Enter your State: > ");
- state = Console.ReadLine();
- if (state == "")
- Console.WriteLine("\r\nMust not be left blank");
- } while (state == "");
- /*do
- {
- Console.Write("Enter your Post Code: > ");
- post_code = Console.ReadLine();
- if (post_code == "")
- Console.WriteLine("\r\nMust not be left blank");
- } while (post_code == "");*/
- // The following code for validating the post code is an example of a reasonably thorough approach
- // as it checks length, and allowable characters.
- do
- {
- Console.Write("Enter your Post Code: > ");
- post_code = Console.ReadLine();
- // Postcode validation takes a few steps
- // Begin by assuming that the postcode is valid and then perform tests to prove otherwise
- valid_post_code = true;
- // Check the postcode length first
- // It must be either 4 characters or may be left blank if the user
- // does not know the postcode.
- if (post_code.Length != 4 && post_code.Length != 0)
- {
- valid_post_code = false;
- }
- else
- {
- // Only if the user has a entered a postcode of valid length then we must do other tests
- if (post_code.Length == 4)
- {
- // First check that the first character is one of 0, 2, 3, 4, 5, 6, 7
- // Anything else is not valid
- first_character = post_code.Substring(0, 1);
- switch (first_character)
- {
- case "0":
- case "2":
- case "3":
- case "4":
- case "5":
- case "6":
- case "7":
- // Passed the fist test so assume that it is valid for now and then run another test
- valid_post_code = true;
- break;
- default:
- valid_post_code = false;
- break;
- }
- // Now make sure that the rest of the characters are valid digits
- // One way is to convert each part of the string to characters and use the .IsDigit() method
- if (valid_post_code)
- {
- if (!Char.IsDigit(post_code[1]) || !Char.IsDigit(post_code[2]) || !Char.IsDigit(post_code[3]))
- {
- valid_post_code = false;
- }
- }
- }
- }
- // Now we know if it is valid or not so make a final decision
- if (!valid_post_code)
- {
- Console.WriteLine(" ** ERROR. Not a valid Australian Post Code.");
- }
- } while (valid_post_code == false);
- Console.Write("Enter your Phone number: > ");
- phone_number = Console.ReadLine();
- // Must reset parseAttempt as to get here means it had to be true
- parseAttempt = false;
- // Get total hours and convert to a float
- while (parseAttempt == false)
- {
- Console.Write("Enter Total Hours Worked: > ");
- vtemp = Console.ReadLine();
- parseAttempt = float.TryParse(vtemp, out hours_worked);
- if (parseAttempt == false)
- {
- Console.WriteLine("Error: Hours Worked must be a valid numeric value (decimal)");
- }
- else
- {
- // Check data is logical
- if (hours_worked <= 0)
- {
- Console.WriteLine("Error: Hours Worked must be greater than 0");
- parseAttempt = false;
- }
- }
- }
- break;
- // Get the Salary (s)
- case "s":
- case "S":
- // check person data has been entered.
- if (hours_worked > 0)
- {
- // initialise variables
- salary = 0;
- pay_rate = 0.0F;
- tax_rate = 0.0F;
- // Must reset parseAttempt as to get here means it had to be true
- parseAttempt = false;
- // Get salary and convert to a int
- while (parseAttempt == false)
- {
- Console.Write("Enter Salary: (Whole numbers only) > ");
- vtemp = Console.ReadLine();
- parseAttempt = int.TryParse(vtemp, out salary);
- if (parseAttempt == false)
- {
- Console.WriteLine("Error: Salary must be a valid numeric value (whole number)");
- }
- else
- {
- // Check data is logical
- if (salary <= 0)
- {
- Console.WriteLine("Error: Salary must be greater than 0");
- parseAttempt = false;
- }
- }
- }
- // Determine the users Tax rate and Pay rate by using the salary entered as follows :
- //* Annual Salary $0 - $16,500 : Tax rate is 11.32%, Pay rate is $8.68 per hour
- if (salary >= 0 && salary <= 16500)
- {
- tax_rate = 11.32F;
- pay_rate = 8.68F;
- }
- //* Annual Salary $16,501 - $19,500 : Tax rate is 15.14%, Pay rate is $10.26 per hour
- else if (salary >= 16501 && salary <= 19500)
- {
- tax_rate = 15.14F;
- pay_rate = 10.26F;
- }
- //* Annual Salary $19,501 - $29,500 : Tax rate is 22.65%, Pay rate is $15.54 per hour
- else if (salary >= 19501 && salary <= 29500)
- {
- tax_rate = 22.65F;
- pay_rate = 15.54F;
- }
- //* Annual Salary $29,501 - $33,500 : Tax rate is 27.12%, Pay rate is $17.63 per hour
- else if (salary >= 29501 && salary <= 33500)
- {
- tax_rate = 27.12F;
- pay_rate = 17.63F;
- }
- //* Annual Salary $33,501 - $39,500 : Tax rate is 30.92%, Pay rate is $20.79 per hour
- else if (salary >= 33501 && salary <= 39500)
- {
- tax_rate = 30.92F;
- pay_rate = 20.79F;
- }
- //* Annual Salary $39,501 - $59,500 : Tax rate is 35.72%, Pay rate is $31.31 per hour
- else if (salary >= 39501 && salary <= 59500)
- {
- tax_rate = 35.72F;
- pay_rate = 31.31F;
- }
- //* Annual Salary $59,501 - $89,500 : Tax rate is 40.72%, Pay rate is $47.12 per hour
- else if (salary >= 59501 && salary <= 89500)
- {
- tax_rate = 40.72F;
- pay_rate = 47.12F;
- }
- //* Annual Salary greater than $89,500 : Tax rate is 50.52%, Pay rate is $55.67 per hour
- else
- {
- tax_rate = 50.52F;
- pay_rate = 55.67F;
- }
- }
- else
- {
- Console.WriteLine("Cannot calculate as person data has not been entered.");
- Console.WriteLine("Press ENTER to continue");
- Console.ReadLine();
- }
- break;
- // Calculate and display (d)
- case "d":
- case "D":
- // Check output can be displayed by checking the pay rate is set.
- if (pay_rate > 0 && full_name != "")
- {
- // initialise variables
- gross_pay = 0.0F;
- tax = 0.0F;
- net_pay = 0.0F;
- // Calculate gross pay
- gross_pay = hours_worked * pay_rate;
- // Calculate total tax
- tax = gross_pay * (tax_rate / 100);
- // Calculate net pay
- net_pay = gross_pay - tax;
- // Output the Person details
- Console.WriteLine("\nPay slip");
- Console.WriteLine("-------------------------------------");
- Console.WriteLine("{0, 20}{1, -5}", "Person Details : ", full_name);
- Console.WriteLine("{0, 20}{1} {2}, {3}, {4}", " ", street_number, address, state, post_code);
- Console.WriteLine("{0, 20}{1, -5}", " ", phone_number);
- Console.WriteLine("{0, 20}${1, -5}", "Annual Salary : ", salary.ToString("N0"));
- Console.WriteLine("{0, 20}{1} hours", "Hours Worked : ", hours_worked.ToString("F1"));
- Console.WriteLine("");
- // Output the Pay details
- Console.WriteLine("{0, 20}{1, 5}", "Pay Rate : ", pay_rate.ToString("C2"));
- Console.WriteLine("{0, 20}{1, 4}%", "Tax Rate : ", tax_rate.ToString("F2"));
- Console.WriteLine(" ");
- Console.WriteLine("{0, 20}{1, 8}", "Gross Pay : ", gross_pay.ToString("C2"));
- Console.WriteLine("{0, 20}{1, 8}", "Tax : ", tax.ToString("C2"));
- Console.WriteLine("{0, 20}{1, 8}", "Net Pay : ", net_pay.ToString("C2"));
- Console.WriteLine("-------------------------------------");
- }
- else
- {
- Console.WriteLine("Cannot output as the pay and tax rate have not been set.");
- }
- Console.WriteLine("Please press ENTER to continue");
- Console.ReadLine();
- break;
- case "x":
- case "X":
- Console.WriteLine("Press ENTER to exit.");
- Console.ReadLine();
- break;
- // Unkown selection
- default:
- Console.WriteLine("Unknown menu selection.");
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement