Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. import java.util.*;
  2. import java.text.NumberFormat;
  3.  
  4. public class EmployeeData
  5. {
  6. String[][] empData;
  7. int top;
  8. private static final int MAXCOL = 6;
  9. NumberFormat fv = NumberFormat.getCurrencyInstance();
  10.  
  11. public EmployeeData()
  12. {
  13. empData = new String[1][MAXCOL];
  14. top = -1;
  15. }
  16. public void getInput()
  17. {
  18. Scanner in = new Scanner(System.in);
  19. boolean flag = true;
  20. int empHours;
  21. float empWage, sales;
  22. String empName, empType;
  23. char rwrd;
  24.  
  25. while(flag)
  26. {
  27. empHours = 0;
  28. empWage = 0;
  29. sales = 0;
  30. rwrd = ' ';
  31.  
  32. System.out.println();
  33. System.out.print("Enter employee's name: ");
  34. empName = in.next();
  35. System.out.print("Enter employee type(Salaried, Hourly, Commissioned): ");
  36. empType = in.next();
  37. if(empType.equalsIgnoreCase("Salaried"))
  38. {
  39. System.out.print("Enter weekly salary: ");
  40. validateInput(in);
  41. empWage = in.nextFloat();
  42. System.out.print("Give Reward to this employee(y/n)? ");
  43. while(!in.hasNext("[ny]"))
  44. {
  45. System.out.print("Please enter (y/n): ");
  46. in.next();
  47. }
  48. rwrd = Character.toLowerCase(in.next().charAt(0));
  49. }
  50. else if(empType.equalsIgnoreCase("Hourly"))
  51. {
  52. empType = "Hourly ";
  53. System.out.print("Enter hourly rate: ");
  54. validateInput(in);
  55. empWage = in.nextFloat();
  56. System.out.print("Enter number of hours worked: ");
  57. while (!in.hasNextInt())
  58. {
  59. System.out.print("Its not valid input, Please enter a number: ");
  60. in.next();
  61. }
  62. empHours = in.nextInt();
  63. }
  64. else if(empType.equalsIgnoreCase("Commissioned"))
  65. {
  66. System.out.print("Enter weekly sales: ");
  67. validateInput(in);
  68. sales = in.nextFloat();
  69. empWage = 500;
  70. }
  71. else
  72. {
  73. System.out.println("Please enter correct type...");
  74. flag = false;
  75. }
  76.  
  77. if(top < 0 && flag == true) //add data into an array
  78. {
  79. addData(empName, empType, empHours, sales, empWage, rwrd);
  80. }
  81. else{
  82. if(!duplicateRec(empName) && flag == true)
  83. {
  84. addData(empName, empType, empHours, sales, empWage, rwrd);
  85. }
  86. else if(flag)
  87. {
  88. System.out.println("Duplicate record!!!!");
  89. }
  90. }
  91. System.out.println();
  92. System.out.print("Do you want to enter another employee data(y/n): ");
  93. while(!in.hasNext("[ny]"))
  94. {
  95. System.out.print("Please enter (y/n): ");
  96. in.next();
  97. }
  98. String ans = in.next();
  99. if(ans.equalsIgnoreCase("y"))
  100. {
  101. flag = true;
  102. }
  103. else
  104. {
  105. flag = false;
  106. }
  107. }
  108. }
  109.  
  110. public boolean duplicateRec(String valToComp)
  111. {
  112. for(int i = 0; i<=top; i++){
  113. String s = empData[i][0];
  114. if(s.equalsIgnoreCase(valToComp))
  115. {
  116. return true;
  117. }
  118. }
  119. return false;
  120. }
  121.  
  122. void validateInput(Scanner sc)
  123. {
  124. while (!sc.hasNextFloat())
  125. {
  126. System.out.print("Its not valid input, Please enter a number: ");
  127. sc.next();
  128. }
  129. }
  130.  
  131. public void addData(String nm, String st, int hrs, float sale, float wage, char r)
  132. {
  133. double empPay = 0;
  134. ++top;
  135. arrayResize(top+1,MAXCOL);
  136.  
  137. empData[top][0] = toTitleCase(nm);
  138. empData[top][1] = toTitleCase(st);
  139. empData[top][2] = ifZero(hrs);
  140. empData[top][3] = ifZero(sale);
  141. empData[top][4] = fv.format(wage);
  142.  
  143. if(st.equalsIgnoreCase("Salaried"))
  144. {
  145. empPay = wage * 40;
  146. if(r == 'y'){
  147. double bonus = empPay * 10/100;
  148. empPay = empPay + bonus;
  149. }
  150. }
  151. else if(st.equalsIgnoreCase("Hourly "))
  152. {
  153. if(hrs > 40){
  154. float extraW = (hrs - 40) * (wage * 2);
  155. empPay = 40 * wage + extraW;
  156. }
  157. else{
  158. empPay = hrs * wage;
  159. }
  160. }
  161. else if(st.equalsIgnoreCase("Commissioned"))
  162. {
  163. if(sale!= 0)
  164. {
  165. empData[top][3] = fv.format(sale);
  166. sale = sale * 10/100;
  167. empPay = wage + sale;
  168. }
  169. else
  170. {
  171. empPay = wage;
  172. }
  173. }
  174. if(r == 'y')
  175. {
  176. empData[top][5] = fv.format(empPay) + "*";
  177. }
  178. else
  179. {
  180. empData[top][5] = fv.format(empPay);
  181. }
  182. }
  183.  
  184. String ifZero(float val){
  185. if(val == 0.0){ return "- "; }
  186. else{ return Double.toString(val);
  187. }
  188. }
  189. String ifZero(int val){
  190. if(val == 0){ return "-"; }
  191. else{ return Integer.toString(val);
  192. }
  193. }
  194.  
  195. public void arrayResize(int rows , int columns)
  196. {
  197. String[][] holdArray = new String[empData.length][empData[0].length];//copy the current array into temporary array
  198. for (int r=0; r < holdArray.length; r++)
  199. {
  200. for (int c=0; c < holdArray[0].length; c++)
  201. holdArray[r][c] = empData[r][c];
  202. }
  203.  
  204. empData= new String[rows][columns]; // re-instantiate the array to be the wished size
  205.  
  206. if(rows>holdArray.length||columns>holdArray[0].length) //copy the temporary array into the new array
  207. {
  208. for (int j=0; j < holdArray.length; j++)
  209. {
  210. for (int k=0; k < holdArray[0].length; k++)
  211. {
  212. empData[j][k] = holdArray[j][k];
  213. }
  214. }
  215. }
  216. else
  217. {
  218. for (int j=0; j <rows; j++)
  219. {
  220. for (int k=0; k <columns; k++)
  221. {
  222. empData[j][k] = holdArray[j][k];
  223. }
  224. }
  225. }
  226. }
  227.  
  228. public static String toTitleCase(String input)
  229. {
  230. StringBuilder titleCase = new StringBuilder();
  231. boolean nextTitleCase = true;
  232.  
  233. for (char c : input.toCharArray())
  234. {
  235. if (Character.isSpaceChar(c))
  236. {
  237. nextTitleCase = true;
  238. }
  239. else if (nextTitleCase)
  240. {
  241. c = Character.toTitleCase(c);
  242. nextTitleCase = false;
  243. }
  244. titleCase.append(c);
  245. }
  246. return titleCase.toString();
  247. }
  248.  
  249. public void sortData()
  250. {
  251. Arrays.sort(empData, new Comparator<String[]>()
  252. {
  253. @Override
  254. public int compare(String[] e1, String[] e2)
  255. {
  256. int result = e1[1].compareTo(e2[1]);
  257. if (result != 0)
  258. {
  259. return result;
  260. }
  261. result = e1[5].compareTo(e2[5]);
  262. if (result != 0)
  263. {
  264. return result;
  265. }
  266. return e1[0].compareTo(e2[0]);
  267. }
  268. });
  269. }
  270.  
  271. public void print()
  272. {
  273. System.out.println();
  274. System.out.println("Name" + "\t" + "Status " + "\t" + "Hours" + "\t" + "Sales" + "\t\t" + "Wages" + "\t" + "Pay");
  275. System.out.println("------\t-------------\t-----\t-----\t\t-----\t--------");
  276. for(int i=0; i<empData.length; i++)
  277. {
  278. String[] row = empData[i];
  279. for(int j=0; j<row.length; j++)
  280. {
  281. System.out.print(row[j] + "\t");
  282. }
  283. System.out.print("\n");
  284. }
  285. System.out.println("------\t-------------\t-----\t-----\t\t-----\t--------");
  286. System.out.print("*");
  287. for(int i=0; i <empData.length; i++)
  288. {
  289. if(empData[i][5].contains("*"))
  290. {
  291. System.out.print(empData[i][0] + " ");
  292. }
  293. }
  294. System.out.println( " 10% bonus is awarded");
  295. }
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement