Advertisement
Guest User

practest5

a guest
Sep 10th, 2012
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.18 KB | None | 0 0
  1. using System;
  2. namespace PracTest5
  3. {
  4. class Pay
  5. {
  6. static void Main()
  7. {
  8. // Define and initialise all variables
  9. string full_name = "";
  10. string street_number = "";
  11. string address = "";
  12. string state = "";
  13. string post_code = "";
  14. string phone_number = "";
  15. float hours_worked = 0.0F;
  16. int salary = 0;
  17. float gross_pay = 0.0F;
  18. float tax = 0.0F;
  19. float net_pay = 0.0F;
  20. float pay_rate = 0.0F;
  21. float tax_rate = 0.0F;
  22. // stores the user response for the menu.
  23. // Must be initialised to not "x" to ensure
  24. // the menu is displayed atleast once
  25. string response = "";
  26. // Tempory variable used to store ReadLine value
  27. string vtemp = "";
  28. bool parseAttempt = true;
  29.  
  30. // The following tewo variables are used as part of the post code validation routine.
  31. string first_character;
  32. bool valid_post_code;
  33.  
  34. // Beginning of main menu loop
  35. while (response != "x" && response != "X") // Check for both upper and lower case
  36. {
  37. // Clear the ascreen then display the menu
  38. Console.Clear();
  39. Console.WriteLine("------- Menu ------------");
  40. Console.WriteLine("Get a persons details (p)");
  41. Console.WriteLine("Get the Salary (s)");
  42. Console.WriteLine("Calculate and display (d)");
  43. Console.WriteLine("Exit (x)");
  44. Console.Write("\nEnter your choice : > ");
  45.  
  46. // Get the response from the user
  47. response = Console.ReadLine();
  48.  
  49. // Switch the response and execute the appropriate code
  50. switch (response)
  51. {
  52. // Get a persons details (p)
  53. case "p":
  54. case "P":
  55. // initialise variables
  56. full_name = "";
  57. street_number = "";
  58. address = "";
  59. state = "";
  60. post_code = "";
  61. phone_number = "";
  62. hours_worked = 0.0F;
  63.  
  64. // Get data from user
  65. do
  66. {
  67. Console.Write("Enter your Full Name: > ");
  68. full_name = Console.ReadLine();
  69. if (full_name == "")
  70. Console.WriteLine("\r\nMust not be left blank");
  71. } while (full_name == "");
  72.  
  73. do
  74. {
  75. Console.Write("Enter your Street number: > ");
  76. street_number = Console.ReadLine();
  77. if (street_number == "")
  78. Console.WriteLine("\r\nMust not be left blank");
  79. } while (street_number == "");
  80.  
  81.  
  82. do
  83. {
  84. Console.Write("Enter your Street address: > ");
  85. address = Console.ReadLine();
  86. if (address == "")
  87. Console.WriteLine("\r\nMust not be left blank");
  88. } while (address == "");
  89.  
  90. do
  91. {
  92. Console.Write("Enter your State: > ");
  93. state = Console.ReadLine();
  94. if (state == "")
  95. Console.WriteLine("\r\nMust not be left blank");
  96. } while (state == "");
  97.  
  98. /*do
  99. {
  100. Console.Write("Enter your Post Code: > ");
  101. post_code = Console.ReadLine();
  102. if (post_code == "")
  103. Console.WriteLine("\r\nMust not be left blank");
  104. } while (post_code == "");*/
  105.  
  106. // The following code for validating the post code is an example of a reasonably thorough approach
  107. // as it checks length, and allowable characters.
  108. do
  109. {
  110. Console.Write("Enter your Post Code: > ");
  111. post_code = Console.ReadLine();
  112.  
  113. // Postcode validation takes a few steps
  114. // Begin by assuming that the postcode is valid and then perform tests to prove otherwise
  115. valid_post_code = true;
  116.  
  117. // Check the postcode length first
  118. // It must be either 4 characters or may be left blank if the user
  119. // does not know the postcode.
  120. if (post_code.Length != 4 && post_code.Length != 0)
  121. {
  122. valid_post_code = false;
  123. }
  124. else
  125. {
  126. // Only if the user has a entered a postcode of valid length then we must do other tests
  127. if (post_code.Length == 4)
  128. {
  129. // First check that the first character is one of 0, 2, 3, 4, 5, 6, 7
  130. // Anything else is not valid
  131. first_character = post_code.Substring(0, 1);
  132. switch (first_character)
  133. {
  134. case "0":
  135. case "2":
  136. case "3":
  137. case "4":
  138. case "5":
  139. case "6":
  140. case "7":
  141. // Passed the fist test so assume that it is valid for now and then run another test
  142. valid_post_code = true;
  143. break;
  144. default:
  145. valid_post_code = false;
  146. break;
  147. }
  148.  
  149. // Now make sure that the rest of the characters are valid digits
  150. // One way is to convert each part of the string to characters and use the .IsDigit() method
  151. if (valid_post_code)
  152. {
  153. if (!Char.IsDigit(post_code[1]) || !Char.IsDigit(post_code[2]) || !Char.IsDigit(post_code[3]))
  154. {
  155. valid_post_code = false;
  156. }
  157. }
  158. }
  159. }
  160.  
  161. // Now we know if it is valid or not so make a final decision
  162. if (!valid_post_code)
  163. {
  164. Console.WriteLine(" ** ERROR. Not a valid Australian Post Code.");
  165. }
  166. } while (valid_post_code == false);
  167.  
  168.  
  169.  
  170.  
  171. Console.Write("Enter your Phone number: > ");
  172. phone_number = Console.ReadLine();
  173.  
  174. // Must reset parseAttempt as to get here means it had to be true
  175. parseAttempt = false;
  176.  
  177. // Get total hours and convert to a float
  178. while (parseAttempt == false)
  179. {
  180. Console.Write("Enter Total Hours Worked: > ");
  181. vtemp = Console.ReadLine();
  182. parseAttempt = float.TryParse(vtemp, out hours_worked);
  183. if (parseAttempt == false)
  184. {
  185. Console.WriteLine("Error: Hours Worked must be a valid numeric value (decimal)");
  186. }
  187. else
  188. {
  189. // Check data is logical
  190. if (hours_worked <= 0)
  191. {
  192. Console.WriteLine("Error: Hours Worked must be greater than 0");
  193. parseAttempt = false;
  194. }
  195. }
  196. }
  197. break;
  198. // Get the Salary (s)
  199. case "s":
  200. case "S":
  201. // check person data has been entered.
  202. if (hours_worked > 0)
  203. {
  204. // initialise variables
  205. salary = 0;
  206. pay_rate = 0.0F;
  207. tax_rate = 0.0F;
  208. // Must reset parseAttempt as to get here means it had to be true
  209. parseAttempt = false;
  210. // Get salary and convert to a int
  211. while (parseAttempt == false)
  212. {
  213. Console.Write("Enter Salary: (Whole numbers only) > ");
  214. vtemp = Console.ReadLine();
  215. parseAttempt = int.TryParse(vtemp, out salary);
  216. if (parseAttempt == false)
  217. {
  218. Console.WriteLine("Error: Salary must be a valid numeric value (whole number)");
  219. }
  220. else
  221. {
  222. // Check data is logical
  223. if (salary <= 0)
  224. {
  225. Console.WriteLine("Error: Salary must be greater than 0");
  226. parseAttempt = false;
  227. }
  228. }
  229. }
  230. // Determine the users Tax rate and Pay rate by using the salary entered as follows :
  231. //* Annual Salary $0 - $16,500 : Tax rate is 11.32%, Pay rate is $8.68 per hour
  232. if (salary >= 0 && salary <= 16500)
  233. {
  234. tax_rate = 11.32F;
  235. pay_rate = 8.68F;
  236. }
  237. //* Annual Salary $16,501 - $19,500 : Tax rate is 15.14%, Pay rate is $10.26 per hour
  238. else if (salary >= 16501 && salary <= 19500)
  239. {
  240. tax_rate = 15.14F;
  241. pay_rate = 10.26F;
  242. }
  243. //* Annual Salary $19,501 - $29,500 : Tax rate is 22.65%, Pay rate is $15.54 per hour
  244. else if (salary >= 19501 && salary <= 29500)
  245. {
  246. tax_rate = 22.65F;
  247. pay_rate = 15.54F;
  248. }
  249. //* Annual Salary $29,501 - $33,500 : Tax rate is 27.12%, Pay rate is $17.63 per hour
  250. else if (salary >= 29501 && salary <= 33500)
  251. {
  252. tax_rate = 27.12F;
  253. pay_rate = 17.63F;
  254. }
  255. //* Annual Salary $33,501 - $39,500 : Tax rate is 30.92%, Pay rate is $20.79 per hour
  256. else if (salary >= 33501 && salary <= 39500)
  257. {
  258. tax_rate = 30.92F;
  259. pay_rate = 20.79F;
  260. }
  261. //* Annual Salary $39,501 - $59,500 : Tax rate is 35.72%, Pay rate is $31.31 per hour
  262. else if (salary >= 39501 && salary <= 59500)
  263. {
  264. tax_rate = 35.72F;
  265. pay_rate = 31.31F;
  266. }
  267. //* Annual Salary $59,501 - $89,500 : Tax rate is 40.72%, Pay rate is $47.12 per hour
  268. else if (salary >= 59501 && salary <= 89500)
  269. {
  270. tax_rate = 40.72F;
  271. pay_rate = 47.12F;
  272. }
  273. //* Annual Salary greater than $89,500 : Tax rate is 50.52%, Pay rate is $55.67 per hour
  274. else
  275. {
  276. tax_rate = 50.52F;
  277. pay_rate = 55.67F;
  278. }
  279. }
  280. else
  281. {
  282. Console.WriteLine("Cannot calculate as person data has not been entered.");
  283. Console.WriteLine("Press ENTER to continue");
  284. Console.ReadLine();
  285. }
  286. break;
  287. // Calculate and display (d)
  288. case "d":
  289. case "D":
  290. // Check output can be displayed by checking the pay rate is set.
  291. if (pay_rate > 0 && full_name != "")
  292. {
  293. // initialise variables
  294. gross_pay = 0.0F;
  295. tax = 0.0F;
  296. net_pay = 0.0F;
  297. // Calculate gross pay
  298. gross_pay = hours_worked * pay_rate;
  299. // Calculate total tax
  300. tax = gross_pay * (tax_rate / 100);
  301. // Calculate net pay
  302. net_pay = gross_pay - tax;
  303.  
  304. // Output the Person details
  305. Console.WriteLine("\nPay slip");
  306. Console.WriteLine("-------------------------------------");
  307. Console.WriteLine("{0, 20}{1, -5}", "Person Details : ", full_name);
  308. Console.WriteLine("{0, 20}{1} {2}, {3}, {4}", " ", street_number, address, state, post_code);
  309. Console.WriteLine("{0, 20}{1, -5}", " ", phone_number);
  310. Console.WriteLine("{0, 20}${1, -5}", "Annual Salary : ", salary.ToString("N0"));
  311. Console.WriteLine("{0, 20}{1} hours", "Hours Worked : ", hours_worked.ToString("F1"));
  312. Console.WriteLine("");
  313. // Output the Pay details
  314. Console.WriteLine("{0, 20}{1, 5}", "Pay Rate : ", pay_rate.ToString("C2"));
  315. Console.WriteLine("{0, 20}{1, 4}%", "Tax Rate : ", tax_rate.ToString("F2"));
  316. Console.WriteLine(" ");
  317. Console.WriteLine("{0, 20}{1, 8}", "Gross Pay : ", gross_pay.ToString("C2"));
  318. Console.WriteLine("{0, 20}{1, 8}", "Tax : ", tax.ToString("C2"));
  319. Console.WriteLine("{0, 20}{1, 8}", "Net Pay : ", net_pay.ToString("C2"));
  320. Console.WriteLine("-------------------------------------");
  321. }
  322. else
  323. {
  324. Console.WriteLine("Cannot output as the pay and tax rate have not been set.");
  325. }
  326. Console.WriteLine("Please press ENTER to continue");
  327. Console.ReadLine();
  328. break;
  329. case "x":
  330. case "X":
  331. Console.WriteLine("Press ENTER to exit.");
  332. Console.ReadLine();
  333. break;
  334. // Unkown selection
  335. default:
  336. Console.WriteLine("Unknown menu selection.");
  337. break;
  338.  
  339. }
  340. }
  341. }
  342. }
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement