Posted By


trusktr on 10/17/10

Tagged


Statistics


Viewed 519 times
Favorited by 0 user(s)

CISP401 Employees.java


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. import java.util.*; //ArrayLists, Comparable, Collections.sort() ...
  2. import java.io.*;
  3.  
  4. public class Employees {
  5.  
  6. // Data Objects (
  7. private ArrayList<Employee> emps;
  8. // )
  9. // Constructors (
  10. public Employees() {
  11. emps = new ArrayList<Employee>();
  12. emps.add(new HourlyEmployee());
  13. }
  14. public Employees(String f, String l, double hrs, double pr) {
  15. emps = new ArrayList<Employee>();
  16. emps.add(new HourlyEmployee(f, l, hrs, pr));
  17. }
  18. public Employees(MyString f, MyString l, Number hrs, Number pr) {
  19. emps = new ArrayList<Employee>();
  20. emps.add(new HourlyEmployee(f, l, hrs, pr));
  21. }
  22. public Employees(EmployeeData d) {
  23. emps = new ArrayList<Employee>();
  24. emps.add(new HourlyEmployee(d));
  25. }
  26. public Employees(HourlyEmployee e) {
  27. emps = new ArrayList<Employee>();
  28. emps.add(new HourlyEmployee(e));
  29. }
  30. public Employees(Employees e) {
  31. emps = new ArrayList<Employee>(e.emps);
  32. }
  33. public Employees(File file) {
  34. emps = new ArrayList<Employee>();
  35. try {
  36. FileReader fr = null;
  37. BufferedReader br = null;
  38. fr = new FileReader(file);
  39. br = new BufferedReader(fr);
  40. while (br.ready()) { // br.read() returns false when the Buffer is not ready (end of file).
  41. MyString record = new MyString("" + br.readLine()); // Only use reaLine() once inside the while loop or you will process more than one line per iteration.
  42. DataExtractor de = new DataExtractor(record); // For extracting data from a MyString.
  43. de.setBuffer("|"); // Sets a buffer for accessing the data items deliminated by a space
  44. emps.add(new HourlyEmployee(de.get(0).toString(), de.get(1).toString(), Double.parseDouble(de.get(2).toString()), Double.parseDouble(de.get(3).toString())));
  45. }
  46. br.close(); // dispose of the resources after using them.
  47. }
  48. catch (FileNotFoundException e) { // File not found.
  49. System.out.println("\n ####\n #### !!! The file could not be found. !!!\n ####");
  50. }
  51. catch (IOException e) { // Error with transaction.
  52. System.out.println("\n ####\n #### !!! There was an error reading the file. !!!\n ####");
  53. }
  54. }
  55. // )
  56. // Accessors (
  57. public Employee employee(int n) {
  58. return emps.get(n); // ###########################################################
  59. } // HOW TO RETURN NEW HOURLY EMPLOYEE ????????
  60. public int size() {
  61. return emps.size();
  62. }
  63. // )
  64. // Mutators (
  65. public void add(String f, String l, double pr, double hrs) {
  66. if (emps.size()!=0) {
  67. EmployeeData tmp = emps.get(0).info();
  68. if (tmp.first.has("x") && tmp.first.length()==1 && tmp.last.has("x") && tmp.last.length()==1) { // If first and last are "x"
  69. emps.set(0, new HourlyEmployee(f, l, pr, hrs)); // Then our Employee list contains a default employee in the first position that we need to overwrite.
  70. }
  71. else {
  72. emps.add(new HourlyEmployee(f, l, pr, hrs)); // Otherwise just append the new employees to the list.
  73. }
  74. }
  75. else {
  76. emps.add(new HourlyEmployee(f, l, pr, hrs)); // Otherwise just append the new employees to the list.
  77. }
  78. }
  79. public void add(MyString f, MyString l, Number pr, Number hrs) {
  80. if (emps.size()!=0) {
  81. EmployeeData tmp = emps.get(0).info();
  82. if (tmp.first.has("x") && tmp.first.length()==1 && tmp.last.has("x") && tmp.last.length()==1) { // If first and last are "x"
  83. emps.set(0, new HourlyEmployee(f, l, pr, hrs)); // Then our Employee list contains a default employee in the first position that we need to overwrite.
  84. }
  85. else {
  86. emps.add(new HourlyEmployee(f, l, pr, hrs)); // Otherwise just append the new employees to the list.
  87. }
  88. }
  89. else {
  90. emps.add(new HourlyEmployee(f, l, pr, hrs)); // Otherwise just append the new employees to the list.
  91. }
  92. }
  93. public void add(EmployeeData d) {
  94. if (emps.size()!=0) {
  95. EmployeeData tmp = emps.get(0).info();
  96. if (tmp.first.has("x") && tmp.first.length()==1 && tmp.last.has("x") && tmp.last.length()==1) { // If first and last are "x"
  97. emps.set(0, new HourlyEmployee(d)); // Then our Employee list contains a default employee in the first position that we need to overwrite.
  98. }
  99. else {
  100. emps.add(new HourlyEmployee(d)); // Otherwise just append the new employees to the list.
  101. }
  102. }
  103. else {
  104. emps.add(new HourlyEmployee(d)); // Otherwise just append the new employees to the list.
  105. }
  106. }
  107. public void add(HourlyEmployee e) {
  108. if (emps.size()!=0) {
  109. EmployeeData tmp = emps.get(0).info();
  110. if (tmp.first.has("x") && tmp.first.length()==1 && tmp.last.has("x") && tmp.last.length()==1) { // If first and last are "x"
  111. emps.set(0, new HourlyEmployee(e)); // Then our Employee list contains a default employee in the first position that we need to overwrite.
  112. }
  113. else {
  114. emps.add(new HourlyEmployee(e)); // Otherwise just append the new employee to the list.
  115. }
  116. }
  117. else {
  118. emps.add(new HourlyEmployee(e)); // Otherwise just append the new employee to the list.
  119. }
  120. }
  121. public void add(Employees e) {
  122. if (emps.size()!=0) {
  123. EmployeeData tmp = emps.get(0).info();
  124. if (tmp.first.has("x") && tmp.first.length()==1 && tmp.last.has("x") && tmp.last.length()==1) { // If first and last are "x"
  125. for (int i=0; i<e.emps.size(); i++) { // Then our Employee list is default so we can overwrite it with the new list.
  126. emps.set(i, e.emps.get(i)); // SHOULD I USE THE NEW CALL???????
  127. //###########################################################################
  128. }
  129. }
  130. else {
  131. for (int i=this.emps.size(); i<this.emps.size()+e.emps.size(); i++) { // Otherwise just append the new employees to the list.
  132. emps.add(e.emps.get(i-this.emps.size())); // SHOULD I USE THE NEW CALL???????
  133. //###########################################################################
  134. }
  135. }
  136. }
  137. else {
  138. for (int i=this.emps.size(); i<this.emps.size()+e.emps.size(); i++) { // Otherwise just append the new employees to the list.
  139. emps.add(e.emps.get(i-this.emps.size())); // SHOULD I USE THE NEW CALL???????
  140. //###########################################################################
  141. }
  142. }
  143. }
  144. public void remove(int n) {
  145. emps.remove(n);
  146. }
  147. public int remove(String last) {
  148. int status = 0;
  149. if (emps.size()!=0) {
  150. for(int i=0; i<emps.size(); i++) {
  151. EmployeeData tmp = emps.get(i).info();
  152. if(tmp.last.has(last) && tmp.last.length()==last.length()) {
  153. emps.remove(i);
  154. }
  155. break;
  156. }
  157. }
  158. else {
  159. status = 1;
  160. }
  161. return status;
  162. }
  163. public int remove(String first, String last) {
  164. int status = 0;
  165. if (emps.size()!=0) {
  166. for(int i=0; i<emps.size(); i++) {
  167. EmployeeData tmp = emps.get(i).info();
  168. if(tmp.last.has(last) && tmp.last.length()==last.length()) {
  169. if(tmp.first.has(first) && tmp.first.length()==first.length()) {
  170. emps.remove(i);
  171. }
  172. }
  173. break;
  174. }
  175. }
  176. else {
  177. status = 1;
  178. }
  179. return status;
  180. }
  181. public EmployeeData getInfo(int n) {
  182. return emps.get(n).info();
  183. }
  184. public EmployeeData getInfo(String last) {
  185. EmployeeData info = new EmployeeData();
  186. if (emps.size()!=0) {
  187. for(int i=0; i<emps.size(); i++) {
  188. EmployeeData tmp = emps.get(i).info();
  189. if(tmp.last.has(last) && tmp.last.length()==last.length()) {
  190. info = new EmployeeData(emps.get(i).info());
  191. }
  192. break;
  193. }
  194. }
  195. else {
  196. System.out.println("Error: That employee doesn't exist.");
  197. }
  198. return info;
  199. }
  200. public EmployeeData getInfo(String first, String last) {
  201. EmployeeData info = new EmployeeData();
  202. if (emps.size()!=0) {
  203. for(int i=0; i<emps.size(); i++) {
  204. EmployeeData tmp = emps.get(i).info();
  205. if(tmp.last.has(last) && tmp.last.length()==last.length()) {
  206. if(tmp.first.has(first) && tmp.first.length()==first.length()) {
  207. info = new EmployeeData(emps.get(i).info());
  208. }
  209. }
  210. break;
  211. }
  212. }
  213. else {
  214. System.out.println("Error: That employee doesn't exist.");
  215. }
  216. return info;
  217. }
  218. public void sort() {
  219. Collections.sort(emps);
  220. }
  221. public String toString() {
  222. return "You have " + emps.size() + " employees under your belt.";
  223. }
  224. // )
  225. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.