payrollsystem.xhtml 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. <?php
  2. /**
  3. * <https://y.st./>
  4. * Copyright © 2017 Alex Yst <mailto:copyright@y.st>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org./licenses/>.
  18. **/
  19. $xhtml = array(
  20. 'title' => '<code>package payrollsystem;</code>',
  21. 'subtitle' => 'Written in <span title="Programming 1">CS 1102</span> of <a href="http://www.uopeople.edu/">University of the People</a>, finalised on <del>2017-05-10</del> <del><ins>2017-05-17</ins></del> <ins>2017-05-24</ins>',
  22. 'copyright year' => '2017',
  23. 'body' => <<<END
  24. <h2>Employee.java</h2>
  25. <blockquote>
  26. <pre><code>/*
  27. * Copyright (C) 2017 Alex Yst
  28. *
  29. * This program is free software: you can redistribute it and/or modify
  30. * it under the terms of the GNU General Public License as published by
  31. * the Free Software Foundation, either version 3 of the License, or
  32. * (at your option) any later version.
  33. *
  34. * This program is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. * GNU General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU General Public License
  40. * along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;.
  41. */
  42. package payrollsystem;
  43. /**
  44. * An abstract class for representing employee pay information
  45. *
  46. * @author Alex Yst
  47. */
  48. public abstract class Employee {
  49. private int empID;
  50. private String name;
  51. private Vehicle vehicle;
  52. /**
  53. * @return the empID
  54. */
  55. public int getEmpID() {
  56. return empID;
  57. }
  58. /**
  59. * @param empID the empID to set
  60. */
  61. public void setEmpID(int empID) {
  62. this.empID = empID;
  63. }
  64. /**
  65. * @return the name
  66. */
  67. public String getName() {
  68. return name;
  69. }
  70. /**
  71. * @param name the name to set
  72. */
  73. public void setName(String name) {
  74. this.name = name;
  75. }
  76. /**
  77. * @return the vehicle
  78. */
  79. public Vehicle getVehicle() {
  80. return vehicle;
  81. }
  82. /**
  83. * @param vehicle the vehicle to set
  84. */
  85. public void setVehicle(Vehicle vehicle) {
  86. this.vehicle = vehicle;
  87. }
  88. /**
  89. * A bare constructor method. Not used by the program.
  90. */
  91. public Employee() {
  92. this.empID = 0;
  93. this.name = &quot;&quot;;
  94. }
  95. /**
  96. * Constructor method. This is the only one of the three constructor methods
  97. * actually used by the program.
  98. *
  99. * @param pEmpID The employee&apos;s ID number
  100. * @param pName The employee&apos;s name
  101. * @param pV An object representing the employee&apos;s vehicle, if any
  102. */
  103. public Employee(int pEmpID, String pName, Vehicle pV) {
  104. this.empID = pEmpID;
  105. this.name = pName;
  106. this.vehicle = pV;
  107. }
  108. /**
  109. * The most comprehensive constructor method of this class. Not used by the
  110. * program
  111. *
  112. * @param pEmpID The ID number of the employee
  113. * @param pName The name of the employee
  114. * @param pPlate The registration plate number of the employee&apos;s vehicle
  115. * @param pColour The colour of the employee&apos;s vehicle
  116. */
  117. public Employee(int pEmpID, String pName, String pPlate, String pColour) {
  118. this.empID = pEmpID;
  119. this.name = pName;
  120. this.vehicle = new Vehicle(pPlate, pColour);
  121. }
  122. /**
  123. * The rules for determining an employee&apos;s pay are determined by what type of
  124. * employee they are. This method must be defined in child classes.
  125. *
  126. * @return The calculated pay
  127. */
  128. public abstract double calculatePay();
  129. }</code></pre>
  130. </blockquote>
  131. <h2>FullTime.java</h2>
  132. <blockquote>
  133. <pre><code>/*
  134. * Copyright (C) 2017 Alex Yst
  135. *
  136. * This program is free software: you can redistribute it and/or modify
  137. * it under the terms of the GNU General Public License as published by
  138. * the Free Software Foundation, either version 3 of the License, or
  139. * (at your option) any later version.
  140. *
  141. * This program is distributed in the hope that it will be useful,
  142. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  143. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  144. * GNU General Public License for more details.
  145. *
  146. * You should have received a copy of the GNU General Public License
  147. * along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;.
  148. */
  149. package payrollsystem;
  150. /**
  151. * A class for representing full-time employees
  152. *
  153. * @author Alex Yst
  154. */
  155. public class FullTime extends Employee {
  156. private double salary;
  157. private double overtime;
  158. /**
  159. * Calculates the ammount that sould be paid to the employee
  160. *
  161. * @return The ammout of pay due to the employee
  162. */
  163. @Override
  164. public double calculatePay() {
  165. System.out.println(&quot;Full time employee.&quot;);
  166. return this.getSalary() + this.getOvertime();
  167. }
  168. /**
  169. * @return the salary
  170. */
  171. public double getSalary() {
  172. return salary;
  173. }
  174. /**
  175. * @param salary the salary to set
  176. */
  177. public void setSalary(double salary) {
  178. this.salary = salary;
  179. }
  180. /**
  181. * @return the overtime
  182. */
  183. public double getOvertime() {
  184. return overtime;
  185. }
  186. /**
  187. * @param overtime the overtime to set
  188. */
  189. public void setOvertime(double overtime) {
  190. this.overtime = overtime;
  191. }
  192. /**
  193. * Constructor method
  194. *
  195. * @param id The ID number of the employee
  196. * @param name The name of the employee
  197. * @param sal The salary of the employee
  198. * @param hourAndHalf unclear - might be overtime pay or might be employee bonus
  199. * @param vehicle an object representing the employee&apos;s vehicle, if any
  200. */
  201. public FullTime(int id, String name, double sal, double hourAndHalf, Vehicle vehicle) {
  202. super(id, name, vehicle);
  203. this.overtime = hourAndHalf;
  204. this.salary = sal;
  205. }
  206. }</code></pre>
  207. </blockquote>
  208. <h2>PartTime.java</h2>
  209. <blockquote>
  210. <pre><code>/*
  211. * Copyright (C) 2017 Alex Yst
  212. *
  213. * This program is free software: you can redistribute it and/or modify
  214. * it under the terms of the GNU General Public License as published by
  215. * the Free Software Foundation, either version 3 of the License, or
  216. * (at your option) any later version.
  217. *
  218. * This program is distributed in the hope that it will be useful,
  219. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  220. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  221. * GNU General Public License for more details.
  222. *
  223. * You should have received a copy of the GNU General Public License
  224. * along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;.
  225. */
  226. package payrollsystem;
  227. /**
  228. * A class for representing part-time employees
  229. *
  230. * @author Alex Yst
  231. */
  232. public class PartTime extends Employee {
  233. private double rate;
  234. private double hoursWorked;
  235. /**
  236. * Calculates the ammount that sould be paid to the employee
  237. *
  238. * @return The ammout of pay due to the employee
  239. */
  240. @Override
  241. public double calculatePay() {
  242. System.out.println(&quot;Part time employee.&quot;);
  243. return this.getHoursWorked() * this.getRate();
  244. }
  245. /**
  246. * @return the rate
  247. */
  248. public double getRate() {
  249. return rate;
  250. }
  251. /**
  252. * @param rate the rate to set
  253. */
  254. public void setRate(double rate) {
  255. this.rate = rate;
  256. }
  257. /**
  258. * @return the hoursWorked
  259. */
  260. public double getHoursWorked() {
  261. return hoursWorked;
  262. }
  263. /**
  264. * @param hoursWorked the hoursWorked to set
  265. */
  266. public void setHoursWorked(double hoursWorked) {
  267. this.hoursWorked = hoursWorked;
  268. }
  269. /**
  270. * Constructor method
  271. *
  272. * @param id The ID number of the employee
  273. * @param name The name of the employee
  274. * @param rate The hourly rate of the employee
  275. * @param hoursWorked2 The number of hours the employee has worked
  276. * @param v1 An object representing the vehicle of the employee
  277. */
  278. public PartTime(int id, String name, double rate, double hoursWorked2, Vehicle v1) {
  279. super(id, name, v1);
  280. this.rate = rate;
  281. this.hoursWorked = hoursWorked2;
  282. }
  283. }</code></pre>
  284. </blockquote>
  285. <h2>PayrollSystem.java</h2>
  286. <blockquote>
  287. <pre><code>/*
  288. * Copyright (C) 2017 Alex Yst
  289. *
  290. * This program is free software: you can redistribute it and/or modify
  291. * it under the terms of the GNU General Public License as published by
  292. * the Free Software Foundation, either version 3 of the License, or
  293. * (at your option) any later version.
  294. *
  295. * This program is distributed in the hope that it will be useful,
  296. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  297. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  298. * GNU General Public License for more details.
  299. *
  300. * You should have received a copy of the GNU General Public License
  301. * along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;.
  302. */
  303. package payrollsystem;
  304. import java.util.ArrayList;
  305. import java.util.Scanner;
  306. /**
  307. * A simple program for calculating employee pay
  308. *
  309. * This program facilitates the entering of employee pay information, then adds
  310. * it up to show the total amount paid to employees by the company. Information
  311. * on employee vehicles can also be entered, but because the program doesn&apos;t
  312. * store any information in a database and this program doesn&apos;t use the vehicle
  313. * information in any way besides simply desplaying it with the employee&apos;s pay,
  314. * entering that information has no significant benefit.
  315. *
  316. * @author Alex Yst
  317. */
  318. public class PayrollSystem {
  319. /**
  320. * This method simply runs the main program.
  321. *
  322. * @param arguments Not implemented
  323. */
  324. public static void main(String[] arguments) {
  325. ArrayList&lt;Employee&gt; arrEmp = new ArrayList&lt;&gt;();
  326. String varCont = &quot;N&quot;;
  327. byte menuOption;
  328. do {
  329. menuOption = showMenu();
  330. switch(menuOption) {
  331. case 1:
  332. addEmployee(arrEmp, readNewFullTime());
  333. break;
  334. case 2:
  335. addEmployee(arrEmp, readNewPartTime());
  336. break;
  337. case 3:
  338. calcPayroll(arrEmp);
  339. break;
  340. default:
  341. break;
  342. }
  343. } while(menuOption != 4);
  344. }
  345. /**
  346. * This method promps the user for an employee&apos;s information and generates an
  347. * object based on that information to represent the employee.
  348. *
  349. * @return An object representing the full-time employee
  350. */
  351. public static FullTime readNewFullTime() {
  352. Scanner kbd = new Scanner(System.in);
  353. System.out.print(&quot;Enter Id: &quot;);
  354. int id = kbd.nextInt();
  355. System.out.print(&quot;\nEnter Name: &quot;);
  356. String name = kbd.next();
  357. System.out.print(&quot;\nEnter Salary: &quot;);
  358. double sal = kbd.nextDouble();
  359. System.out.print(&quot;\nEnter Bonus: &quot;);
  360. double hourAndHalf = kbd.nextDouble();
  361. return new FullTime(id, name, sal, hourAndHalf, getVehicle());
  362. }
  363. /**
  364. * This method promps the user for an employee&apos;s information and generates an
  365. * object based on that information to represent the employee.
  366. *
  367. * @return An object representing the part-time employee
  368. */
  369. public static PartTime readNewPartTime() {
  370. Scanner kbd = new Scanner(System.in);
  371. System.out.print(&quot;Enter Id: &quot;);
  372. int id = kbd.nextInt();
  373. System.out.print(&quot;\nEnter Name: &quot;);
  374. String name = kbd.next();
  375. System.out.print(&quot;\nEnter Hourly Rate: &quot;);
  376. double rate = kbd.nextDouble();
  377. System.out.print(&quot;\nEnter Number of Hours Worked: &quot;);
  378. double hoursWorked = kbd.nextDouble();
  379. return new PartTime(id, name, rate, hoursWorked, getVehicle());
  380. }
  381. /**
  382. * This one-line method takes an employee list and an employee, and add the
  383. * employee to the list. On first glance, it doesn&apos;t seem like a useful method,
  384. * but it actually provides a layer of abstraction. In the future, employee
  385. * lists or employee representations may be implemented another way. If and
  386. * when that happens, this method can be updated instead of updating every
  387. * instance of adding an employee to a dist throught the code.
  388. *
  389. * @param pArrEmp The list of employees
  390. * @param pEmp The employee to add to the list
  391. */
  392. public static void addEmployee(ArrayList&lt;Employee&gt; pArrEmp, Employee pEmp) {
  393. pArrEmp.add(pEmp);
  394. }
  395. /**
  396. * This method shows the user the options and returns their input. Why is our
  397. * menu indexed from one instead of zero? Are we common fools that don&apos;t
  398. * understand zero comes before one!?
  399. *
  400. * @return A byte specified by the user
  401. */
  402. public static byte showMenu() {
  403. Scanner kbd = new Scanner(System.in);
  404. System.out.println(&quot;/* *********************************************************/&quot;);
  405. System.out.println(&quot;/* 1. Add FullTime */&quot;);
  406. System.out.println(&quot;/* 2. Add PartTime */&quot;);
  407. System.out.println(&quot;/* 3. Calculate Payroll */&quot;);
  408. System.out.println(&quot;/* 4. Exit */&quot;);
  409. System.out.println(&quot;/* *********************************************************/&quot;);
  410. System.out.print(&quot;Input: &quot;);
  411. return kbd.nextByte();
  412. }
  413. /**
  414. * This method displays vehicle information of every employee and, more
  415. * importantly, calculates every employee&apos;s pay and adds the pay of all
  416. * employees up to show the total paid out by the company. This should be the
  417. * final option specified by the user before exiting the program, at least if
  418. * the user knows what they&apos;re doing.
  419. *
  420. * @param pArrEmp A list of employees
  421. */
  422. public static void calcPayroll(ArrayList&lt;Employee&gt; pArrEmp) {
  423. double totalCompanyPay = 0.0;
  424. double individualPay;
  425. for(int i=0; i&lt;pArrEmp.size(); i++) {
  426. Employee emp = pArrEmp.get(i);
  427. System.out.println(&quot;\n**************************\n&quot;);
  428. individualPay = emp.calculatePay();
  429. Vehicle v = emp.getVehicle();
  430. String hasVehicle;
  431. if(v == null) {
  432. hasVehicle = &quot;No&quot;;
  433. } else {
  434. hasVehicle = &quot;Yes&quot;;
  435. }
  436. System.out.print(&quot;Employee Name: &quot;);
  437. System.out.println(emp.getName());
  438. System.out.print(&quot;Has Vehicle: &quot;);
  439. System.out.println(hasVehicle);
  440. if(v != null) {
  441. System.out.print(&quot;Plate Number: &quot;);
  442. System.out.println(v.getPlateNumber());
  443. System.out.print(&quot;Colour: &quot;);
  444. System.out.println(v.getColour());
  445. }
  446. System.out.print(&quot;Take Home Pay: &quot;);
  447. System.out.println(individualPay);
  448. totalCompanyPay += individualPay;
  449. }
  450. System.out.print(&quot;------------------\nTotal paroll of the company: &quot;);
  451. System.out.println(totalCompanyPay);
  452. System.out.println(&quot;------------------&quot;);
  453. }
  454. /**
  455. * This method promps the user for information regarding the employee&apos;s
  456. * vehicle. However, this information is actually useless for calculating
  457. * payrool and isn&apos;t stored in any database. Therefore, each pay period, the
  458. * vehicle information must be uselessly re-entered. A smart user of this
  459. * progran will simply type &quot;N&quot; when asked if the employee has a vehicle to
  460. * avoid having to look up the employee&apos;s registration plate number and vehicle
  461. * colour.
  462. *
  463. * @return An object representing the employee&apos;s vehicle
  464. */
  465. public static Vehicle getVehicle() {
  466. Scanner kbd = new Scanner(System.in);
  467. System.out.print(&quot;\nDoes this employee have a vehicle? Y/N :\nInput : &quot;);
  468. String hasVehicle = kbd.next();
  469. if(hasVehicle.equalsIgnoreCase(&quot;Y&quot;)) {
  470. System.out.print(&quot;\nEnter plate number: &quot;);
  471. String auxPlate = kbd.next();
  472. System.out.print(&quot;\nEnter vehicle colour: &quot;);
  473. String auxColour = kbd.next();
  474. return new Vehicle(auxPlate, auxColour);
  475. } else {
  476. return null;
  477. }
  478. }
  479. }</code></pre>
  480. </blockquote>
  481. <h2>Vehicle.java</h2>
  482. <blockquote>
  483. <pre><code>/*
  484. * Copyright (C) 2017 Alex Yst
  485. *
  486. * This program is free software: you can redistribute it and/or modify
  487. * it under the terms of the GNU General Public License as published by
  488. * the Free Software Foundation, either version 3 of the License, or
  489. * (at your option) any later version.
  490. *
  491. * This program is distributed in the hope that it will be useful,
  492. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  493. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  494. * GNU General Public License for more details.
  495. *
  496. * You should have received a copy of the GNU General Public License
  497. * along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;.
  498. */
  499. package payrollsystem;
  500. /**
  501. * A class for representing employee vehicles
  502. *
  503. * @author Alex Yst
  504. */
  505. public class Vehicle {
  506. private String plateNumber;
  507. private String colour;
  508. /**
  509. * Constructor method
  510. *
  511. * @param plateNumber The registration plate number of the vehicle
  512. * @param colour The colour of the vehicle
  513. */
  514. public Vehicle(String plateNumber, String colour) {
  515. this.plateNumber = plateNumber;
  516. this.colour = colour;
  517. }
  518. /**
  519. * @return the plateNumber
  520. */
  521. public String getPlateNumber() {
  522. return plateNumber;
  523. }
  524. /**
  525. * @param plateNumber the plateNumber to set
  526. */
  527. public void setPlateNumber(String plateNumber) {
  528. this.plateNumber = plateNumber;
  529. }
  530. /**
  531. * @return the colour
  532. */
  533. public String getColour() {
  534. return colour;
  535. }
  536. /**
  537. * @param colour the colour to set
  538. */
  539. public void setColour(String colour) {
  540. this.colour = colour;
  541. }
  542. }</code></pre>
  543. </blockquote>
  544. END
  545. );