Advertisement
Guest User

Untitled

a guest
May 29th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8. class Program
  9. {
  10.  
  11. // ------------------------------------------------------------------
  12. // Display menu, get and return users response
  13. static char get_menu()
  14. {
  15. char response = ' ';
  16. // Display the menu
  17. Console.WriteLine("------- Menu ------------");
  18. Console.WriteLine("Add a person (a)");
  19. Console.WriteLine("Find a person (s)");
  20. Console.WriteLine("Display current person (p)");
  21. Console.WriteLine("Delete current person (d)");
  22. Console.WriteLine("Calculate and display pay slip for current person (c)");
  23. Console.WriteLine("Save to File (w)");
  24. Console.WriteLine("Load form File (l)");
  25. Console.WriteLine("Exit (x)");
  26. Console.Write("\nEnter your choice : >");
  27.  
  28. // Get the response from the user
  29. char.TryParse(Console.ReadLine(), out response);
  30. return response;
  31. }
  32. // ------------------------------------------------------------------
  33. // Display person details
  34. static void display_person_details(string full_name, string street_number, string address, string post_code, string state, string phone_number, int salary)
  35. {
  36. Console.WriteLine("-------------------------------------");
  37. Console.WriteLine("{0, 20}{1, -5}", "Person Details : ", full_name);
  38. Console.WriteLine("{0, 20}{1} {2}, {3}, {4}", " ", street_number, address, state, post_code);
  39. Console.WriteLine("{0, 20}{1, -5}", " ", phone_number);
  40. Console.WriteLine("{0, 20}${1, -5}", "Annual Salary : ", salary.ToString("N0"));
  41. }
  42. // ------------------------------------------------------------------
  43. // Get tax and pay rate for a given salary
  44. static void get_tax_pay_rate(int salary, ref float pay_rate, ref float tax_rate)
  45. {
  46. // initialise variables
  47. pay_rate = 0.0F;
  48. tax_rate = 0.0F;
  49.  
  50. // Determine the users Tax rate and Pay rate by using the salary entered as follows :
  51. //* Annual Salary $0 - $16,500 : Tax rate is 11.32%, Pay rate is $8.68 per hour
  52. if (salary >= 0 && salary <= 16500)
  53. {
  54. tax_rate = 11.32F;
  55. pay_rate = 8.68F;
  56. }
  57. //* Annual Salary $16,501 - $19,500 : Tax rate is 15.14%, Pay rate is $10.26 per hour
  58. else
  59. if (salary >= 16501 && salary <= 19500)
  60. {
  61. tax_rate = 15.14F;
  62. pay_rate = 10.26F;
  63. }
  64. //* Annual Salary $19,501 - $29,500 : Tax rate is 22.65%, Pay rate is $15.54 per hour
  65. else
  66. if (salary >= 19501 && salary <= 29500)
  67. {
  68. tax_rate = 22.65F;
  69. pay_rate = 15.54F;
  70. }
  71. //* Annual Salary $29,501 - $33,500 : Tax rate is 27.12%, Pay rate is $17.63 per hour
  72. else
  73. if (salary >= 29501 && salary <= 33500)
  74. {
  75. tax_rate = 27.12F;
  76. pay_rate = 17.63F;
  77. }
  78. //* Annual Salary $33,501 - $39,500 : Tax rate is 30.92%, Pay rate is $20.79 per hour
  79. else
  80. if (salary >= 33501 && salary <= 39500)
  81. {
  82. tax_rate = 30.92F;
  83. pay_rate = 20.79F;
  84. }
  85. //* Annual Salary $39,501 - $59,500 : Tax rate is 35.72%, Pay rate is $31.31 per hour
  86. else
  87. if (salary >= 39501 && salary <= 59500)
  88. {
  89. tax_rate = 35.72F;
  90. pay_rate = 31.31F;
  91. }
  92. //* Annual Salary $59,501 - $89,500 : Tax rate is 40.72%, Pay rate is $47.12 per hour
  93. else
  94. if (salary >= 59501 && salary <= 89500)
  95. {
  96. tax_rate = 40.72F;
  97. pay_rate = 47.12F;
  98. }
  99. //* Annual Salary greater than $89,500 : Tax rate is 50.52%, Pay rate is $55.67 per hour
  100. else
  101. {
  102. tax_rate = 50.52F;
  103. pay_rate = 55.67F;
  104. }
  105. }
  106. // ------------------------------------------------------------------
  107. // Find a given person
  108. static int find_person(string[] full_name, int current_person, int number_of_persons_entered)
  109. {
  110. int p = 0;
  111. Console.Write("Enter the person name to search >");
  112. string searchStr = Console.ReadLine();
  113. for (p = 0; p < number_of_persons_entered; p++)
  114. {
  115. if (full_name[p] == searchStr || full_name[p].Contains(searchStr))
  116. {
  117. Console.WriteLine("Person found");
  118. return p;
  119. }
  120. }
  121. Console.WriteLine("Person not found");
  122. return current_person;
  123. }
  124. // ------------------------------------------------------------------
  125. // Delete a person. Set all data to nothing.
  126. 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)
  127. {
  128. string vtemp = "";
  129. Console.Write("Are you sure you wish to delete {0}(y/n)?", full_name[current_person]);
  130. vtemp = Console.ReadLine();
  131. if (vtemp == "y")
  132. {
  133. // initialise all variables
  134. full_name[current_person] = "";
  135. street_number[current_person] = "";
  136. address[current_person] = "";
  137. state[current_person] = "";
  138. post_code[current_person] = "";
  139. phone_number[current_person] = "";
  140. salary[current_person] = 0;
  141. }
  142. }
  143. // ------------------------------------------------------------------
  144. // Calculate and Display: Gets hours worked, determine pay and tax rate, them displays result.
  145. static void calculate_display(string full_name, string street_number, string address, string post_code, string state, string phone_number, int salary)
  146. {
  147. // Use when calculating and displaying data
  148. float hours_worked = 0.0F;
  149. float gross_pay = 0.0F;
  150. float tax = 0.0F;
  151. float net_pay = 0.0F;
  152. float pay_rate = 0.0F;
  153. float tax_rate = 0.0F;
  154. string vtemp = "";
  155. bool parseAttempt = true;
  156. // Must reset parseAttempt as to get here means it had to be true
  157. parseAttempt = false;
  158. // Check if person active
  159. if (salary > 0)
  160. {
  161. // Get total hours and convert to a float
  162. while (parseAttempt == false)
  163. {
  164. Console.Write("Enter Total Hours Worked: >");
  165. vtemp = Console.ReadLine();
  166. parseAttempt = float.TryParse(vtemp, out hours_worked);
  167. if (parseAttempt == false)
  168. {
  169. Console.WriteLine("Error: Hours Worked must be a valid numeric value (decimal)");
  170. }
  171. else
  172. {
  173. // Check data is logical
  174. if (hours_worked <= 0)
  175. {
  176. Console.WriteLine("Error: Hours Worked must be greater than 0");
  177. parseAttempt = false;
  178. }
  179. }
  180. }
  181.  
  182. get_tax_pay_rate(salary, ref pay_rate, ref tax_rate);
  183.  
  184. // initialise variables
  185. gross_pay = 0.0F;
  186. tax = 0.0F;
  187. net_pay = 0.0F;
  188. // Calculate gross pay
  189. gross_pay = hours_worked * pay_rate;
  190. // Calculate total tax
  191. tax = gross_pay * (tax_rate / 100);
  192. // Calculate net pay
  193. net_pay = gross_pay - tax;
  194.  
  195. // Output the Person details
  196. Console.WriteLine("\nPay slip");
  197.  
  198. // Output the Pay details
  199. Console.WriteLine("{0, 20}{1} hours", "Hours Worked : ", hours_worked.ToString("F1"));
  200. Console.WriteLine("{0, 20}{1, 5}", "Pay Rate : ", pay_rate.ToString("C2"));
  201. Console.WriteLine("{0, 20}{1, 4}%", "Tax Rate : ", tax_rate.ToString("F2"));
  202. Console.WriteLine(" ");
  203. Console.WriteLine("{0, 20}{1, 8}", "Gross Pay : ", gross_pay.ToString("C2"));
  204. Console.WriteLine("{0, 20}{1, 8}", "Tax : ", tax.ToString("C2"));
  205. Console.WriteLine("{0, 20}{1, 8}", "Net Pay : ", net_pay.ToString("C2"));
  206. Console.WriteLine("-------------------------------------");
  207. }
  208. else
  209. {
  210. Console.WriteLine("No active person found. Either add a person or find a person");
  211. }
  212. }
  213. // ------------------------------------------------------------------
  214. // Validate post code
  215. static bool validate_post_code(string input)
  216. {
  217. // Must be 4 digits
  218. if (input.Length != 4)
  219. return false;
  220. // Must all be digits
  221. for (int i = 0; i < input.Length; i++)
  222. {
  223. if (Char.IsDigit(input[i]) == false)
  224. return false;
  225. }
  226. return true;
  227. }
  228. // ------------------------------------------------------------------
  229. // Validate phone number
  230. static bool validate_phone_number(string input)
  231. {
  232. // Must be 0 to 10 digits
  233. if (input.Length > 10)
  234. return false;
  235. // Must all be digits
  236. for (int i = 0; i < input.Length; i++)
  237. {
  238. if (Char.IsDigit(input[i]) == false)
  239. return false;
  240. }
  241. return true;
  242. }
  243. // ------------------------------------------------------------------
  244. // Add person : prompt and collect person data
  245. 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)
  246. {
  247. // Set the current to the next available array position
  248. current_person = number_of_persons_entered;
  249.  
  250. // initialise all variables
  251. full_name[current_person] = "";
  252. street_number[current_person] = "";
  253. address[current_person] = "";
  254. state[current_person] = "";
  255. post_code[current_person] = "";
  256. phone_number[current_person] = "";
  257. salary[current_person] = 0;
  258.  
  259. bool parseAttempt = true;
  260. string vtemp = "";
  261. // Get data from user
  262. Console.Write("Enter your Full Name: >");
  263. full_name[current_person] = Console.ReadLine();
  264.  
  265. Console.Write("Enter your Street number: >");
  266. street_number[current_person] = Console.ReadLine();
  267.  
  268. Console.Write("Enter your Street address: >");
  269. address[current_person] = Console.ReadLine();
  270.  
  271. Console.Write("Enter your State: >");
  272. state[current_person] = Console.ReadLine();
  273. // Must reset parseAttempt as to get here means it had to be true
  274. parseAttempt = false;
  275. // Get post code
  276. while (parseAttempt == false)
  277. {
  278. Console.Write("Enter your Post Code: >");
  279. vtemp = Console.ReadLine();
  280. parseAttempt = validate_post_code(vtemp);
  281. if (parseAttempt == true)
  282. post_code[current_person] = vtemp;
  283. else
  284. Console.WriteLine("Error: Post Code must be 4 digits.");
  285.  
  286. }
  287. // Must reset parseAttempt as to get here means it had to be true
  288. parseAttempt = false;
  289. // Get phone number
  290. while (parseAttempt == false)
  291. {
  292. Console.Write("Enter your Phone number: >");
  293. vtemp = Console.ReadLine();
  294. parseAttempt = validate_phone_number(vtemp);
  295. if(parseAttempt == true)
  296. phone_number[current_person] = vtemp;
  297. else
  298. Console.WriteLine("Error: Phone number must be either empty or less than 10 digits.");
  299.  
  300. }
  301.  
  302. // Must reset parseAttempt as to get here means it had to be true
  303. parseAttempt = false;
  304. // Get salary and convert to a int
  305. while (parseAttempt == false)
  306. {
  307. Console.Write("Enter Salary: (Whole numbers only) >");
  308. vtemp = Console.ReadLine();
  309. parseAttempt = int.TryParse(vtemp, out salary[current_person]);
  310. if (parseAttempt == false)
  311. {
  312. Console.WriteLine("Error: Salary must be a valid numeric value (whole number)");
  313. }
  314. else
  315. {
  316. // Check data is logical
  317. if (salary[current_person] <= 0)
  318. {
  319. Console.WriteLine("Error: Salary must be greater than 0");
  320. parseAttempt = false;
  321. }
  322. }
  323. }
  324. // Update the total person entered
  325. number_of_persons_entered = number_of_persons_entered + 1; ;
  326. }
  327. // ------------------------------------------------------------------
  328. // Main
  329. static void Main()
  330. {
  331. // Define and initialise all variables
  332. const int MAX_PERSONS = 50;
  333.  
  334. // Person arrays
  335. string[] full_name;
  336. full_name = new string[MAX_PERSONS];
  337. string[] street_number;
  338. street_number = new string[MAX_PERSONS];
  339. string[] address;
  340. address = new string[MAX_PERSONS];
  341. string[] state;
  342. state = new string[MAX_PERSONS];
  343. string[] post_code;
  344. post_code = new string[MAX_PERSONS];
  345. string[] phone_number;
  346. phone_number = new string[MAX_PERSONS];
  347. int[] salary;
  348. salary = new int[MAX_PERSONS];
  349.  
  350. // counters used to identify the person
  351. int current_person = -1;
  352. int number_of_persons_entered = 0;
  353.  
  354. // stores the user response for the menu.
  355. // Must be initialised to not 'x' to ensure
  356. // the menu is displayed atleast once
  357. char response = ' ';
  358.  
  359. while (response != 'x')
  360. {
  361. response = get_menu();
  362.  
  363. // Switch the response and execute the appropriate code
  364. switch (response)
  365. {
  366. // Add a person (a)
  367. case 'a':
  368. // check available slot to store data
  369. if (number_of_persons_entered == MAX_PERSONS)
  370. {
  371. Console.WriteLine("Maximum number of persons entered. {0} allowed.", MAX_PERSONS);
  372. break;
  373. }
  374. // Add the new person
  375. add_person(full_name, street_number, address, post_code, state, phone_number, salary, ref current_person, ref number_of_persons_entered);
  376. break;
  377. // Find a person (s)
  378. case 's':
  379. if (current_person >= 0)
  380. {
  381. // Find the person
  382. current_person = find_person(full_name, current_person, number_of_persons_entered);
  383. // Display the person
  384. 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]);
  385. }
  386. else
  387. Console.WriteLine("No data entered.");
  388. break;
  389. // Display current person (p)
  390. case 'p':
  391. if (current_person >= 0)
  392. {
  393. // Display the person
  394. 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]);
  395. }
  396. else
  397. Console.WriteLine("No data entered.");
  398. break;
  399. // Delete current person (d)
  400. case 'd':
  401. if (current_person >= 0)
  402. {
  403. // Display the person
  404. 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]);
  405. // Delete the current person
  406. delete_person(full_name, street_number, address, post_code, state, phone_number, salary, current_person);
  407. // Set to first person entered as default
  408. current_person = 0;
  409. }
  410. else
  411. Console.WriteLine("No data entered.");
  412. break;
  413. // Calculate and display pay slip for current person (c)
  414. case 'c':
  415. if (current_person >= 0)
  416. {
  417. // Display the person
  418. 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]);
  419. // Calculate pay slip and display
  420. 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]);
  421. }
  422. else
  423. Console.WriteLine("No data entered.");
  424. break;
  425. case 'w':
  426. StreamWriter fstr_out;
  427. try
  428. {
  429. fstr_out = new StreamWriter("Pay.txt");
  430. }
  431. catch (IOException exc)
  432. {
  433. Console.WriteLine(exc.Message);
  434. break;
  435. }
  436. // Write all data to the file
  437. try
  438. {
  439. for (int c = 0; c < number_of_persons_entered; c++)
  440. {
  441. fstr_out.Write("{0},", full_name[c]);
  442. fstr_out.Write("{0},", street_number [c]);
  443. fstr_out.Write("{0},", address [c]);
  444. fstr_out.Write("{0},", post_code [c]);
  445. fstr_out.Write("{0}", state [c]);
  446. fstr_out.Write("{0}", phone_number [c]);
  447. fstr_out.Write("{0}", salary [c].ToString ("F2"));
  448. fstr_out.WriteLine();
  449. }
  450. }
  451. catch (IOException exc)
  452. {
  453. Console.WriteLine(exc.Message);
  454. fstr_out.Close();
  455. break;
  456. }
  457. // close the file
  458. fstr_out.Close();
  459. break;
  460.  
  461. break;
  462. case 'l':
  463. FileStream fin;
  464. string str = "";
  465. string[] data = null;
  466.  
  467. // Open a file for reading
  468. try
  469. {
  470. fin = new FileStream("Pay.txt", FileMode.Open);
  471. }
  472. // Report an error if file cannot be opened
  473. catch (IOException exc)
  474. {
  475. Console.WriteLine(exc.Message);
  476. break;
  477. }
  478. // Setup the reading variable to the open file
  479. StreamReader fstr_in = new StreamReader(fin);
  480. try
  481. {
  482. int c = 0;
  483. while ((str = fstr_in.ReadLine()) != null)
  484. {
  485. data = str.Split(',');
  486. if (data.Length != 7)
  487. {
  488. Console.WriteLine("Error : file format is incorrect");
  489. break;
  490. }
  491. full_name [c] = int.Parse (data[full_name]) ;
  492. street_number[c] = int.Parse (data[street_number]);
  493. address[c] = int.Parse (data[address]);
  494. post_code[c] =int.Parse (data[post_code]);
  495. state[c] = int.Parse (data[state]);
  496. phone_number [c] = int.Parse (data[phone_number ]);
  497. salary [c] = int.Parse (data[salary]);
  498. c++;
  499. }
  500. }
  501. // Report an error if file cannot be opened
  502. catch (IOException exc)
  503. {
  504. Console.WriteLine(exc.Message);
  505. fstr_in.Close();
  506. break;
  507. }
  508. // Update internal counters
  509. number_of_persons_entered = c;
  510. current_person = 0;
  511. // Close input file
  512. fstr_in.Close();
  513. break;
  514.  
  515. break;
  516.  
  517. // Unkown selection
  518. default:
  519. Console.WriteLine("Unknown menu selection.");
  520. break;
  521.  
  522. }
  523. }
  524. }
  525. }
  526. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement