Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- namespace ConsoleApplication1
- {
- class Program
- {
- // ------------------------------------------------------------------
- // Display menu, get and return users response
- static char get_menu()
- {
- char response = ' ';
- // Display the menu
- Console.WriteLine("------- Menu ------------");
- Console.WriteLine("Add a person (a)");
- Console.WriteLine("Find a person (s)");
- Console.WriteLine("Display current person (p)");
- Console.WriteLine("Delete current person (d)");
- Console.WriteLine("Calculate and display pay slip for current person (c)");
- Console.WriteLine("Save to File (w)");
- Console.WriteLine("Load form File (l)");
- Console.WriteLine("Exit (x)");
- Console.Write("\nEnter your choice : >");
- // Get the response from the user
- char.TryParse(Console.ReadLine(), out response);
- return response;
- }
- // ------------------------------------------------------------------
- // Display person details
- static void display_person_details(string full_name, string street_number, string address, string post_code, string state, string phone_number, int salary)
- {
- 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"));
- }
- // ------------------------------------------------------------------
- // Get tax and pay rate for a given salary
- static void get_tax_pay_rate(int salary, ref float pay_rate, ref float tax_rate)
- {
- // initialise variables
- pay_rate = 0.0F;
- tax_rate = 0.0F;
- // 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;
- }
- }
- // ------------------------------------------------------------------
- // Find a given person
- static int find_person(string[] full_name, int current_person, int number_of_persons_entered)
- {
- int p = 0;
- Console.Write("Enter the person name to search >");
- string searchStr = Console.ReadLine();
- for (p = 0; p < number_of_persons_entered; p++)
- {
- if (full_name[p] == searchStr || full_name[p].Contains(searchStr))
- {
- Console.WriteLine("Person found");
- return p;
- }
- }
- Console.WriteLine("Person not found");
- return current_person;
- }
- // ------------------------------------------------------------------
- // Delete a person. Set all data to nothing.
- static void delete_person(string[] full_name, string[] street_number, string[] address, string[] post_code, string[] state, string[] phone_number, int[] salary, int current_person)
- {
- string vtemp = "";
- Console.Write("Are you sure you wish to delete {0}(y/n)?", full_name[current_person]);
- vtemp = Console.ReadLine();
- if (vtemp == "y")
- {
- // initialise all variables
- full_name[current_person] = "";
- street_number[current_person] = "";
- address[current_person] = "";
- state[current_person] = "";
- post_code[current_person] = "";
- phone_number[current_person] = "";
- salary[current_person] = 0;
- }
- }
- // ------------------------------------------------------------------
- // Calculate and Display: Gets hours worked, determine pay and tax rate, them displays result.
- static void calculate_display(string full_name, string street_number, string address, string post_code, string state, string phone_number, int salary)
- {
- // Use when calculating and displaying data
- float hours_worked = 0.0F;
- float gross_pay = 0.0F;
- float tax = 0.0F;
- float net_pay = 0.0F;
- float pay_rate = 0.0F;
- float tax_rate = 0.0F;
- string vtemp = "";
- bool parseAttempt = true;
- // Must reset parseAttempt as to get here means it had to be true
- parseAttempt = false;
- // Check if person active
- if (salary > 0)
- {
- // 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;
- }
- }
- }
- get_tax_pay_rate(salary, ref pay_rate, ref tax_rate);
- // 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");
- // Output the Pay details
- Console.WriteLine("{0, 20}{1} hours", "Hours Worked : ", hours_worked.ToString("F1"));
- 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("No active person found. Either add a person or find a person");
- }
- }
- // ------------------------------------------------------------------
- // Validate post code
- static bool validate_post_code(string input)
- {
- // Must be 4 digits
- if (input.Length != 4)
- return false;
- // Must all be digits
- for (int i = 0; i < input.Length; i++)
- {
- if (Char.IsDigit(input[i]) == false)
- return false;
- }
- return true;
- }
- // ------------------------------------------------------------------
- // Validate phone number
- static bool validate_phone_number(string input)
- {
- // Must be 0 to 10 digits
- if (input.Length > 10)
- return false;
- // Must all be digits
- for (int i = 0; i < input.Length; i++)
- {
- if (Char.IsDigit(input[i]) == false)
- return false;
- }
- return true;
- }
- // ------------------------------------------------------------------
- // Add person : prompt and collect person data
- static void add_person(string[] full_name, string[] street_number, string[] address, string[] post_code, string[] state, string[] phone_number, int[] salary, ref int current_person, ref int number_of_persons_entered)
- {
- // Set the current to the next available array position
- current_person = number_of_persons_entered;
- // initialise all variables
- full_name[current_person] = "";
- street_number[current_person] = "";
- address[current_person] = "";
- state[current_person] = "";
- post_code[current_person] = "";
- phone_number[current_person] = "";
- salary[current_person] = 0;
- bool parseAttempt = true;
- string vtemp = "";
- // Get data from user
- Console.Write("Enter your Full Name: >");
- full_name[current_person] = Console.ReadLine();
- Console.Write("Enter your Street number: >");
- street_number[current_person] = Console.ReadLine();
- Console.Write("Enter your Street address: >");
- address[current_person] = Console.ReadLine();
- Console.Write("Enter your State: >");
- state[current_person] = Console.ReadLine();
- // Must reset parseAttempt as to get here means it had to be true
- parseAttempt = false;
- // Get post code
- while (parseAttempt == false)
- {
- Console.Write("Enter your Post Code: >");
- vtemp = Console.ReadLine();
- parseAttempt = validate_post_code(vtemp);
- if (parseAttempt == true)
- post_code[current_person] = vtemp;
- else
- Console.WriteLine("Error: Post Code must be 4 digits.");
- }
- // Must reset parseAttempt as to get here means it had to be true
- parseAttempt = false;
- // Get phone number
- while (parseAttempt == false)
- {
- Console.Write("Enter your Phone number: >");
- vtemp = Console.ReadLine();
- parseAttempt = validate_phone_number(vtemp);
- if(parseAttempt == true)
- phone_number[current_person] = vtemp;
- else
- Console.WriteLine("Error: Phone number must be either empty or less than 10 digits.");
- }
- // 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[current_person]);
- if (parseAttempt == false)
- {
- Console.WriteLine("Error: Salary must be a valid numeric value (whole number)");
- }
- else
- {
- // Check data is logical
- if (salary[current_person] <= 0)
- {
- Console.WriteLine("Error: Salary must be greater than 0");
- parseAttempt = false;
- }
- }
- }
- // Update the total person entered
- number_of_persons_entered = number_of_persons_entered + 1; ;
- }
- // ------------------------------------------------------------------
- // Main
- static void Main()
- {
- // Define and initialise all variables
- const int MAX_PERSONS = 50;
- // Person arrays
- string[] full_name;
- full_name = new string[MAX_PERSONS];
- string[] street_number;
- street_number = new string[MAX_PERSONS];
- string[] address;
- address = new string[MAX_PERSONS];
- string[] state;
- state = new string[MAX_PERSONS];
- string[] post_code;
- post_code = new string[MAX_PERSONS];
- string[] phone_number;
- phone_number = new string[MAX_PERSONS];
- int[] salary;
- salary = new int[MAX_PERSONS];
- // counters used to identify the person
- int current_person = -1;
- int number_of_persons_entered = 0;
- // stores the user response for the menu.
- // Must be initialised to not 'x' to ensure
- // the menu is displayed atleast once
- char response = ' ';
- while (response != 'x')
- {
- response = get_menu();
- // Switch the response and execute the appropriate code
- switch (response)
- {
- // Add a person (a)
- case 'a':
- // check available slot to store data
- if (number_of_persons_entered == MAX_PERSONS)
- {
- Console.WriteLine("Maximum number of persons entered. {0} allowed.", MAX_PERSONS);
- break;
- }
- // Add the new person
- add_person(full_name, street_number, address, post_code, state, phone_number, salary, ref current_person, ref number_of_persons_entered);
- break;
- // Find a person (s)
- case 's':
- if (current_person >= 0)
- {
- // Find the person
- current_person = find_person(full_name, current_person, number_of_persons_entered);
- // Display the person
- display_person_details(full_name[current_person], street_number[current_person], address[current_person], post_code[current_person], state[current_person], phone_number[current_person], salary[current_person]);
- }
- else
- Console.WriteLine("No data entered.");
- break;
- // Display current person (p)
- case 'p':
- if (current_person >= 0)
- {
- // Display the person
- display_person_details(full_name[current_person], street_number[current_person], address[current_person], post_code[current_person], state[current_person], phone_number[current_person], salary[current_person]);
- }
- else
- Console.WriteLine("No data entered.");
- break;
- // Delete current person (d)
- case 'd':
- if (current_person >= 0)
- {
- // Display the person
- display_person_details(full_name[current_person], street_number[current_person], address[current_person], post_code[current_person], state[current_person], phone_number[current_person], salary[current_person]);
- // Delete the current person
- delete_person(full_name, street_number, address, post_code, state, phone_number, salary, current_person);
- // Set to first person entered as default
- current_person = 0;
- }
- else
- Console.WriteLine("No data entered.");
- break;
- // Calculate and display pay slip for current person (c)
- case 'c':
- if (current_person >= 0)
- {
- // Display the person
- display_person_details(full_name[current_person], street_number[current_person], address[current_person], post_code[current_person], state[current_person], phone_number[current_person], salary[current_person]);
- // Calculate pay slip and display
- calculate_display(full_name[current_person], street_number[current_person], address[current_person], post_code[current_person], state[current_person], phone_number[current_person], salary[current_person]);
- }
- else
- Console.WriteLine("No data entered.");
- break;
- case 'w':
- StreamWriter fstr_out;
- try
- {
- fstr_out = new StreamWriter("Pay.txt");
- }
- catch (IOException exc)
- {
- Console.WriteLine(exc.Message);
- break;
- }
- // Write all data to the file
- try
- {
- for (int c = 0; c < number_of_persons_entered; c++)
- {
- fstr_out.Write("{0},", full_name[c]);
- fstr_out.Write("{0},", street_number [c]);
- fstr_out.Write("{0},", address [c]);
- fstr_out.Write("{0},", post_code [c]);
- fstr_out.Write("{0}", state [c]);
- fstr_out.Write("{0}", phone_number [c]);
- fstr_out.Write("{0}", salary [c].ToString ("F2"));
- fstr_out.WriteLine();
- }
- }
- catch (IOException exc)
- {
- Console.WriteLine(exc.Message);
- fstr_out.Close();
- break;
- }
- // close the file
- fstr_out.Close();
- break;
- break;
- case 'l':
- FileStream fin;
- string str = "";
- string[] data = null;
- // Open a file for reading
- try
- {
- fin = new FileStream("Pay.txt", FileMode.Open);
- }
- // Report an error if file cannot be opened
- catch (IOException exc)
- {
- Console.WriteLine(exc.Message);
- break;
- }
- // Setup the reading variable to the open file
- StreamReader fstr_in = new StreamReader(fin);
- try
- {
- int c = 0;
- while ((str = fstr_in.ReadLine()) != null)
- {
- data = str.Split(',');
- if (data.Length != 7)
- {
- Console.WriteLine("Error : file format is incorrect");
- break;
- }
- full_name [c] = int.Parse (data[full_name]) ;
- street_number[c] = int.Parse (data[street_number]);
- address[c] = int.Parse (data[address]);
- post_code[c] =int.Parse (data[post_code]);
- state[c] = int.Parse (data[state]);
- phone_number [c] = int.Parse (data[phone_number ]);
- salary [c] = int.Parse (data[salary]);
- c++;
- }
- }
- // Report an error if file cannot be opened
- catch (IOException exc)
- {
- Console.WriteLine(exc.Message);
- fstr_in.Close();
- break;
- }
- // Update internal counters
- number_of_persons_entered = c;
- current_person = 0;
- // Close input file
- fstr_in.Close();
- break;
- break;
- // Unkown selection
- default:
- Console.WriteLine("Unknown menu selection.");
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement