Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.46 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class EmployeeApp
  7. {
  8. static Scanner input = new Scanner(System.in);
  9. static InputStreamReader textInput = new InputStreamReader(System.in);
  10. static BufferedReader reader = new BufferedReader(textInput);
  11.  
  12. static int centralEmployeeID = 105;
  13. static int centralDepartmentID = 1005;
  14. static ArrayList<Employee> allEmployee = new ArrayList<Employee>();
  15. static ArrayList<Department> allDepartments = new ArrayList<Department>();
  16.  
  17. public static void main(String[] args)
  18. {
  19. prePopulate();
  20. mainMenu();
  21. }
  22.  
  23. public static void prePopulate()
  24. {
  25. Department d1 = new Department(1001, "Sales");
  26. Department d2 = new Department(1002, "Production");
  27. Department d3 = new Department(1003, "Finance");
  28. Department d4 = new Department(1004, "Marketing");
  29. allDepartments.add(d1);
  30. allDepartments.add(d2);
  31. allDepartments.add(d3);
  32. allDepartments.add(d4);
  33.  
  34. Employee e1 = new Employee(101, "Anton", 18, d1);
  35. Employee e2 = new Employee(102, "Dato", 17, d2);
  36. Employee e3 = new Employee(103, "Dyl", 19, d3);
  37. Employee e4 = new Employee(104, "Karl", 19, d4);
  38.  
  39. d1.addEmployee(e1);
  40. d2.addEmployee(e2);
  41. d3.addEmployee(e3);
  42. d4.addEmployee(e4);
  43.  
  44. allEmployee.add(e1);
  45. allEmployee.add(e2);
  46. allEmployee.add(e3);
  47. allEmployee.add(e4);
  48. }
  49.  
  50. public static void mainMenu()
  51. {
  52. System.out.println("Press 1 for the Employee Menu");
  53. System.out.println("Press 2 for the Department Menu");
  54. System.out.println("Press 3 to Exit");
  55.  
  56. String choice = input.next();
  57.  
  58. switch(choice)
  59. {
  60. case "1":
  61. {
  62. System.out.println("Loading Employee Menu...");
  63. employeeMenu();
  64. break;
  65. }
  66. case "2":
  67. {
  68. System.out.println("Loading Department Menu...");
  69. departmentMenu();
  70. break;
  71. }
  72. case"3":
  73. {
  74. System.exit(0);
  75. break;
  76. }
  77.  
  78. }
  79. mainMenu();
  80. }
  81.  
  82.  
  83.  
  84. public static void departmentMenu()
  85. {
  86. System.out.println("Press 1 to Create a Department");
  87. System.out.println("Press 2 to View All Departments");
  88. System.out.println("Press 3 to Edit a Department");
  89. System.out.println("Press 4 to Delete a Department");
  90. System.out.println("Press 5 to Return to Main Menu");
  91.  
  92. String choice = input.next();
  93.  
  94. switch(choice)
  95. {
  96. case"1":
  97. {
  98. try {
  99. createDepartment();
  100. }
  101. catch(Exception e)
  102. {
  103. System.out.println("Error was logged");
  104. }
  105. break;
  106. }
  107. case"2":
  108. {
  109. viewDepartments();
  110. break;
  111. }
  112. case"3":
  113. {
  114. try {
  115. editDepartment();
  116. }
  117. catch(Exception e)
  118. {
  119. System.out.println("Error was logged");
  120. }
  121. break;
  122. }
  123. case"4":
  124. {
  125. deleteDepartment();
  126. break;
  127. }
  128. case"5":
  129. {
  130. mainMenu();
  131. break;
  132. }
  133. }
  134. }
  135.  
  136. public static void employeeMenu()
  137. {
  138. System.out.println("Press 1 to Create a Employee");
  139. System.out.println("Press 2 to View All Employees");
  140. System.out.println("Press 3 to Edit a Employee");
  141. System.out.println("Press 4 to Delete a Employee");
  142. System.out.println("Press 5 to view Employees by Department");
  143. System.out.println("Press 6 to Return to Main Menu");
  144.  
  145. String choice = input.next();
  146.  
  147. switch(choice)
  148. {
  149. case"1":
  150. {
  151. try {
  152. createEmployee();
  153. }
  154. catch(Exception e)
  155. {
  156. System.out.println("Error was logged");
  157. }
  158. break;
  159. }
  160. case"2":
  161. {
  162. viewEmployees();
  163. break;
  164. }
  165. case"3":
  166. {
  167. try {
  168. editEmployees();
  169. }
  170. catch(Exception e)
  171. {
  172. System.out.println("Error was logged");
  173. }
  174. break;
  175. }
  176. case"4":
  177. {
  178. deleteEmployee();
  179. break;
  180. }
  181. case"5":
  182. {
  183. viewEmployeesByDepartment();
  184. break;
  185. }
  186. case"6":
  187. {
  188. mainMenu();
  189. break;
  190. }
  191. }
  192. }
  193.  
  194. public static void createDepartment() throws Exception
  195. {
  196. Department d = new Department();
  197. d.setDepartmentID(centralDepartmentID);
  198. centralDepartmentID++;
  199. System.out.println("Enter Department Name");
  200. d.setDepartmentName(reader.readLine());
  201. allDepartments.add(d);
  202. System.out.println(d.getDepartmentName() + " has successfully been added");
  203.  
  204. mainMenu();
  205. }
  206.  
  207. public static void viewDepartments()
  208. {
  209. for(Department d: allDepartments)
  210. {
  211. System.out.println(d.getDepartmentID() + "\t" + d.getDepartmentName());
  212. }
  213. mainMenu();
  214. }
  215.  
  216. public static void editDepartment() throws Exception
  217. {
  218.  
  219. System.out.println("Please choose ID number of Department to edit");
  220. int chosenDepartment = input.nextInt();
  221.  
  222.  
  223. for(Department d: allDepartments)
  224. {
  225. if(chosenDepartment==d.getDepartmentID())
  226. {
  227.  
  228. System.out.println("Enter Department Name");
  229. d.setDepartmentName(reader.readLine());
  230.  
  231. System.out.println(d.getDepartmentName() + " has successfully been Edited");
  232. }
  233. }
  234. mainMenu();
  235. }
  236.  
  237. public static void deleteDepartment()
  238. {
  239. System.out.println("Please choose ID number of Department to Delete");
  240. int chosenDepartment = input.nextInt();
  241.  
  242. for(Department d: allDepartments)
  243. {
  244. if(chosenDepartment==d.getDepartmentID())
  245. {
  246. allDepartments.remove(d);
  247. System.out.println(d.getDepartmentName() + " has successfully been Deleted");
  248.  
  249. mainMenu();
  250. }
  251.  
  252. }
  253.  
  254. }
  255.  
  256. public static void createEmployee() throws Exception
  257. {
  258. Employee e = new Employee();
  259. e.setEmployeeID(centralEmployeeID);
  260. centralEmployeeID++;
  261. System.out.println("Enter Employee Name");
  262. e.setEmployeeName(reader.readLine());
  263. System.out.println("Enter Employee Age");
  264. e.setEmployeeAge(input.nextInt());
  265. System.out.println("Enter Employee Department");
  266.  
  267.  
  268. System.out.println("Enter Employee Department by using Department ID");
  269. int chosenDepartmentID = input.nextInt();
  270.  
  271. for(Department d: allDepartments)
  272. {
  273. if(chosenDepartmentID == d.getDepartmentID())
  274. {
  275. e.setEmployeeDepartment(d);
  276. d.addEmployee(e);
  277. System.out.println(e.getEmployeeName() + " has applied for " + d.getDepartmentName());
  278. allEmployee.add(e);
  279. }
  280.  
  281. }
  282. mainMenu();
  283. }
  284.  
  285. public static void viewEmployees()
  286. {
  287. for(Employee e: allEmployee)
  288. {
  289. System.out.println(e.getEmployeeID() + "\t" + e.getEmployeeName() + "\t" + e.getEmployeeAge() + "\t" + e.getEmployeeDepartment().getDepartmentName());
  290. }
  291. mainMenu();
  292. }
  293.  
  294. public static void editEmployees() throws Exception
  295. {
  296. System.out.println("Please choose Employee by ID Number");
  297. int chosenEmployee = input.nextInt();
  298.  
  299. for(Employee e: allEmployee)
  300. {
  301. if(chosenEmployee==e.getEmployeeID())
  302. {
  303.  
  304. System.out.println("Enter Employee Name");
  305. e.setEmployeeName(reader.readLine());
  306. System.out.println("Enter Employee Age");
  307. e.setEmployeeAge(input.nextInt());
  308. System.out.println("Enter Employee Department");
  309.  
  310.  
  311. System.out.println("Enter Employee Department by using Department ID");
  312. int chosenDepartmentID = input.nextInt();
  313.  
  314. for(Department d: allDepartments)
  315. {
  316. if(chosenDepartmentID == d.getDepartmentID())
  317. {
  318. e.setEmployeeDepartment(d);
  319. d.addEmployee(e);
  320. System.out.println(e.getEmployeeName() + " has applied for " + d.getDepartmentName());
  321. System.out.println(e.getEmployeeName() + " has successfully been Edited");
  322. }
  323.  
  324. }
  325. }
  326. }
  327. mainMenu();
  328. }
  329.  
  330. public static void deleteEmployee()
  331. {
  332. System.out.println("Please choose ID number of Employee to Delete");
  333. int chosenEmployee = input.nextInt();
  334.  
  335. for(Employee e: allEmployee)
  336. {
  337. if(chosenEmployee==e.getEmployeeID())
  338. {
  339. allEmployee.remove(e);
  340. System.out.println(e.getEmployeeName() + " has successfully been Deleted");
  341.  
  342. mainMenu();
  343. }
  344.  
  345. }
  346. }
  347.  
  348. public static void viewEmployeesByDepartment()
  349. {
  350. System.out.println("Enter Department by ID number");
  351. int chosenDepartmentID = input.nextInt();
  352.  
  353. for(Department d: allDepartments)
  354. {
  355.  
  356.  
  357. if(chosenDepartmentID == d.getDepartmentID())
  358. {
  359. d.printStaff();
  360. }
  361. }
  362. mainMenu();
  363. }
  364.  
  365. public static void admin()
  366. {
  367.  
  368. }
  369.  
  370. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement