Advertisement
Guest User

Untitled

a guest
Apr 8th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.13 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.sql.Date;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.ArrayList;
  6. import java.util.InputMismatchException;
  7. import java.util.Iterator;
  8. import java.util.Scanner;
  9.  
  10. public class Depot{
  11. @SuppressWarnings("deprecation")
  12. String name; //Class name
  13. int counter = 0; //Counter to measure login attempts
  14.  
  15. //declare long for dates used in preset work schedules, in milliseconds from 1970
  16. Long date3 = 1478217600000L;
  17. Date date1 = new Date(1478217600000L);
  18. Date date2 = new Date(1479217600000L);
  19.  
  20. //Instantiating new Driver/Manager objects
  21. Driver driver = new Driver("tom", "tom", false);
  22. Driver driver2 = new Driver("phil2", "phil2", false);
  23. Driver driver3 = new Driver("phil", "phil", true);
  24. Manager manager = new Manager("ben", "ben", true);
  25.  
  26. //Instantiating new Tanker/Trucks
  27. Truck vehicle1 = new Truck("audi", "t45", 46, "0000", true, 500);
  28. Tanker vehicle2 = new Tanker("audi", "t45", 32, "0001", true, 350);
  29. Tanker vehicle3 = new Tanker("audi", "t45", 50, "0002", false, 600);
  30.  
  31. //Instantiating new WorkSchedules
  32. WorkSchedule sche1 = new WorkSchedule("tom", "george", date1, date2, "0001", "Pending");
  33. WorkSchedule sche2 = new WorkSchedule("phil", "george2", date1, date2, "0002", "Pending");
  34.  
  35. //declaring new arrayList of given object; eg drivers..
  36. static ArrayList<Driver> driverArray = new ArrayList<Driver>();
  37. static ArrayList<Vehicle> vehicleArray = new ArrayList<Vehicle>();
  38. static ArrayList<WorkSchedule> workSArray = new ArrayList<WorkSchedule>();
  39.  
  40. //declare method logOn(), used for the login eg; checking credentials
  41. public void logOn(){
  42.  
  43. //Check if there's been 3 login attempts, if so output to console and cloe system
  44. if (counter > 3){
  45. System.out.print("You have entered the incorect details three times the system will now close! \n");
  46. System.exit(0);
  47. }
  48.  
  49. //Prompt user for Login details and save answer in Temp
  50. Scanner in = new Scanner(System.in);
  51. System.out.println(" Welcome to the depot system \n Enter your Username and Password to continue \n Username: ");
  52. String temp = in.nextLine();
  53.  
  54. //Iterate through drivers,
  55. for (int i = 0; i < driverArray.size(); i++) {
  56.  
  57. if(driverArray.get(i).CheckUsername(temp)){ //If username matches read in password to temp2
  58. System.out.println(" Password: ");
  59. String temp2 = in.nextLine();
  60.  
  61. if(driverArray.get(i)instanceof Manager){; //if user is a manager...
  62.  
  63. if(driverArray.get(i).CheckPassword(temp2)){ //if password is correct, print notifications and open menu
  64. System.out.print("\n Notifications: ");
  65. driverArray.get(i).PrintNotifications();
  66. managerMenu();
  67. }else {
  68. System.out.println("Your details are incorect please try again! \n");
  69. counter++; //increment log in attempt counter by 1
  70. logOn(); //re-executes LogOn() from the beginning
  71. }
  72.  
  73. }else {
  74. if(driverArray.get(i).CheckPassword(temp2)){ //if password is correct, print notifications
  75. System.out.print("\n Notifications: ");
  76. driverArray.get(i).PrintNotifications();
  77.  
  78. //prompt user for name then print their schedule
  79. System.out.print("\n To view your Work Schedule enter your username"); //normal drivers can only view their work schedule
  80. Scanner keyboard = new Scanner(System.in);
  81. String client = keyboard.nextLine();
  82. printSchedUN(client); //prints current drivers Schedule
  83. System.out.print("\n \n press Enter to Exit.");
  84. keyboard.nextLine();
  85. }else {
  86. System.out.println("Your details are incorect please try again! \n"); //details are incorrect message
  87. counter++; //increments counter by 1
  88. logOn(); //re-executes LogOn()
  89. }
  90. }
  91. }
  92. }
  93. counter++; //increments counter by 1
  94. }
  95.  
  96. //returns a container of two values isTrue and num
  97. public static Container getVehicle(String reg) {
  98. Container b = new Container(); //Instantiate two containers
  99. Container c = new Container();
  100. c.SetContainer(false, 0); //Set Null container
  101.  
  102. for (int i = 0; i < vehicleArray.size(); i++) { //iterate through vehicles
  103. b.SetContainer(true, i);
  104. if (vehicleArray.get(i).CheckRegNo(reg))return b; //if regNo's match return b
  105. }
  106. return c; // if RegNo's don't match return C (null)
  107. }
  108.  
  109. //returns a container of two values isTrue and num
  110. public static Container getDriver(String driver) {
  111. Container b = new Container(); //Instantiate two containers
  112. Container c = new Container();
  113. c.SetContainer(false, 0); //Set Null container
  114.  
  115. for (int i = 0; i < driverArray.size(); i++) { //iterate through vehicles
  116. b.SetContainer(true, i);
  117. if (driverArray.get(i).CheckUsername(driver)) //if usernames match return b
  118. return b;
  119. }
  120. return c; //if usernames don't match return b
  121. }
  122.  
  123. //prints all WorkSchedules to the console
  124. public static void printSched(String Sched) {
  125. for (int i = 0; i < workSArray.size(); i++) { //iterate through workSchedules
  126. if (workSArray.get(i).CheckClientName(Sched)) //if clients name matches
  127. workSArray.get(i).PrintSchedule(); //print the schedule
  128. else
  129. System.out.println("No Schedule under this name exists \n");
  130. }
  131. }
  132.  
  133. //prints all workSchedules to the console
  134. public static void printSchedUN(String Sched) {
  135. for (int i = 0; i < workSArray.size(); i++) { //iterate through workSchedules
  136. if (workSArray.get(i).CheckUsername(Sched)) //if usernames match
  137. workSArray.get(i).PrintSchedule(); //print the schedule
  138. else
  139. System.out.println("No Schedule under this name exists \n");
  140. }
  141. }
  142.  
  143. public boolean checkDepotName(String depotName) {
  144. return name.equals(depotName);
  145. }
  146.  
  147. // menu for editing workSchedules
  148. public static void workScheduleMenu() {
  149. Scanner in = new Scanner(System.in);
  150. int selection = 0;
  151.  
  152. do {
  153. try {
  154. //output the users choices and read in selection
  155. System.out.println("1. Create new schedule");
  156. System.out.println("2. Remove schedule");
  157. System.out.println("3. Exit");
  158. System.out.print("Please select an option: \n");
  159. selection = in.nextInt();
  160. } catch (InputMismatchException e) { //catches exception
  161. System.out.println("Enter a valid number from the list!\n");
  162. }
  163.  
  164. //switch statement for menu
  165. switch (selection) {
  166. case 1:
  167. //prompt user and save value to reg
  168. System.out.print("You selected option 1 \n");
  169. System.out.print("Please enter the reg no. of the vehicle \n");
  170. Scanner keyboard2 = new Scanner(System.in);
  171. String reg = keyboard2.nextLine();
  172.  
  173. if (getVehicle(reg).isTrue) { //if a vehicle with that reg exists
  174. if (vehicleArray.get(getVehicle(reg).num).IsAvailable()) { //if that vehicle is available
  175. System.out.print("Please enter the drivers username \n"); //prompt user for username
  176. Scanner keyboard3 = new Scanner(System.in);
  177. String username = keyboard2.nextLine();
  178.  
  179. if (getDriver(username).isTrue) { //if a driver with that username exists
  180. if (driverArray.get(getDriver(username).num).IsAvailable()) { // if driver is available
  181. try {
  182. driverArray.get(getDriver(username).num).SetSchedule(reg, username); // create work schedule
  183. } catch (IOException e) {
  184. // TODO Auto-generated catch block
  185. e.printStackTrace();
  186. }
  187. } else {
  188. System.out.println("Driver is unavailable");
  189. break;
  190. }
  191. }
  192. } else {
  193. System.out.println("Vehicle is unavailable");
  194. break;
  195. }
  196. }
  197. break;
  198. case 2:
  199. //prompt user for username
  200. System.out.print("You selected option 3 \n");
  201. System.out.print("REMOVE SCHEDULE \n");
  202. Scanner keyboard3 = new Scanner(System.in);
  203. System.out.println("Please enter the name of the driver who's schedule you wish to remove");
  204. String username = keyboard3.nextLine();
  205.  
  206. Iterator<WorkSchedule> it = workSArray.iterator(); //iterate through the workSchedules
  207. while (it.hasNext()) {
  208. WorkSchedule user = it.next();
  209. if (user.CheckUsername(username)) { // if usernames are equal, remove it
  210. it.remove();
  211. }
  212. }
  213. System.out.print("You have chosen to remove:" + username);
  214. break;
  215. case 3:
  216. //opens driver menu
  217. managerMenu();
  218. break;
  219. default:
  220. System.out.print("The selection is invalid.");
  221. break;
  222. }
  223. } while (selection != 4);
  224. }
  225.  
  226. //Depot constructor
  227. public Depot(String Name) {
  228. //set the name of the depot and add objects to their corresponding arrayList
  229. this.name = Name;
  230. driverArray.add(driver);
  231. driverArray.add(driver2);
  232. driverArray.add(driver3);
  233. driverArray.add(manager);
  234. vehicleArray.add(vehicle1);
  235. vehicleArray.add(vehicle2);
  236. vehicleArray.add(vehicle3);
  237. workSArray.add(sche1);
  238. workSArray.add(sche2);
  239. }
  240.  
  241. public Boolean checkDepotName1(String name) {
  242. return this.name.equals(name);
  243. }
  244.  
  245. //opens the driver menu
  246. public static void managerMenu() {
  247. Scanner in = new Scanner(System.in);
  248. int selection = 0;
  249. do {
  250. try {
  251. //output options to console and read in choice
  252. System.out.println("1. view work schedule");
  253. System.out.println("2. View drivers");
  254. System.out.println("3. Create schedule");
  255. System.out.println("4. Add vehicle");
  256. System.out.println("5. Remove vehicle");
  257. System.out.println("6. View vehicles");
  258. System.out.println("7. Add driver");
  259. System.out.println("8. remove driver");
  260. System.out.println("9. move vehicle");
  261. System.out.println("10. Exit");
  262. System.out.print("Please select an option:");
  263. selection = in.nextInt();
  264. } catch (InputMismatchException e) {
  265. System.out.println("Please enter a valid number from the list!!\n");
  266. }
  267.  
  268. switch (selection)
  269. {
  270. case 1:
  271. //prompt user for clients name and print the corresponding schedule
  272. System.out.print("VIEW EXISTING SCHEDULE \n Please enter the clients name");
  273. Scanner keyboard = new Scanner(System.in);
  274. String client = keyboard.nextLine();
  275. printSched(client);
  276. break;
  277. case 2:
  278. //iterate through drivers and print their usernames
  279. System.out.print("View Drivers \n");
  280. for (int i = 0; i < driverArray.size(); i++) {
  281. driverArray.get(i).printN();
  282. }
  283. break;
  284. case 3:
  285. //opens work schedule menu
  286. System.out.print("You selected create schedule \n");
  287. Depot.workScheduleMenu();
  288. break;
  289. case 4:
  290. //prompt user for vehicle type, make, model and wight recording each
  291. System.out.print("You selected add vehicle \n");
  292. System.out.print("Please enter the type of vehicle you want to add (truck or tanker) \n ");
  293. Scanner keyboard1 = new Scanner(System.in);
  294. String type = keyboard1.nextLine();
  295. System.out.print("Please enter the vehicle make: ");
  296. String make = keyboard1.nextLine();
  297. System.out.print("Please enter the vehicle model: ");
  298. String model = keyboard1.nextLine();
  299. System.out.print("Please enter the vehicle weight: ");
  300. int weight = 0;
  301.  
  302. //input validation
  303. try{
  304. weight = keyboard1.nextInt();
  305. } catch (InputMismatchException e) {
  306. System.out.println("Please enter a valid number for vehicle weight!!\n");
  307. }
  308.  
  309. //prompt user for reg
  310. String reg;
  311. System.out.print("Please enter the vehicle reg: ");
  312. Scanner keyboard6 = new Scanner(System.in);
  313. reg = keyboard6.nextLine();
  314.  
  315. if(type.equals("tanker")){ //if the vehicle is a tanker
  316. System.out.print("Please enter the max fluid capacity of the vehicle: \n");
  317. int weight1 = 0;
  318.  
  319. try{ //input validation
  320. weight1 = keyboard6.nextInt();
  321. } catch (InputMismatchException e) {
  322. System.out.println("Please enter a valid number for max fluid capacity!!\n");
  323. }
  324.  
  325. Tanker tanker = new Tanker(make, model, weight, reg, true,weight1); //instantiate a new tanker and add it to tankers
  326. vehicleArray.add(tanker);
  327. managerMenu();
  328.  
  329. }else if (type.equals("truck")){ //if the vehicle is a tanker
  330. System.out.print("Please enter the max cargo capacity of the vehicle: ");
  331. int weight2 = 0;
  332.  
  333. try{ //input validation
  334. weight2 = keyboard6.nextInt();
  335. } catch (InputMismatchException e) {
  336. System.out.println("Please enter a valid number for max cargo capacity!!\n");
  337. }
  338.  
  339. Truck truck = new Truck(make, model, weight, reg, true,weight2); //instantiate a new truck and add it to trucks
  340. vehicleArray.add(truck);
  341. managerMenu();
  342. }
  343. break;
  344. case 5:
  345. //prompt user for the reg
  346. System.out.print("You selected remove vehicle \n");
  347. Scanner keyboard3 = new Scanner(System.in);
  348. System.out.println("Please enter the Reg No. of the vehicle you wish to remove: \n");
  349. String reg1 = keyboard3.nextLine();
  350.  
  351. Iterator<Vehicle> it = vehicleArray.iterator(); //iterate through vehicles
  352. while (it.hasNext()) {
  353. Vehicle user = it.next();
  354. if (user.CheckRegNo(reg1)) { //if the reg matches remove the vehicle
  355. it.remove();
  356. }
  357. }
  358. System.out.print("You have chosen to remove:" + reg1);
  359. break;
  360. case 6:
  361. //iterate through vehicles and print each to console
  362. System.out.print("You selected View Drivers\n");
  363. for (int i = 0; i < vehicleArray.size(); i++) {
  364. vehicleArray.get(i).printN();
  365. }
  366. break;
  367. case 7:
  368. //prompt user for type, username and password
  369. System.out.print("You selected add Driver \n Is it a new driver or manager? \n");
  370. Scanner keyboard4 = new Scanner(System.in);
  371. String type1 = keyboard4.nextLine();
  372. System.out.print("Please enter the Drivers Username: ");
  373. String username = keyboard4.nextLine();
  374. System.out.print("Please enter the Drivers Password: ");
  375. String password = keyboard4.nextLine();
  376.  
  377. if(type1.equals("manager")){ //if type is equals to "manager"
  378. Manager manager = new Manager(username, password, false); //instantiate manager and add it to arrayList
  379. driverArray.add(manager);
  380. }else if(type1.equals("driver")){ //if type is equal to "driver"
  381. Driver driver = new Manager(username, password, false); //instantiate driver and add it to arrayList
  382. driverArray.add(driver);
  383. }
  384. break;
  385. case 8:
  386. //prompt user for username
  387. System.out.print("You selected remove Driver \n");
  388. Scanner keyboard7 = new Scanner(System.in);
  389. System.out.println("Please enter the Username of the driver you wish to remove: \n");
  390. String username2 = keyboard7.nextLine();
  391.  
  392. Iterator<Driver> it1 = driverArray.iterator(); //iterate through drivers
  393. while (it1.hasNext()) {
  394. Driver user = it1.next();
  395. if (user.CheckUsername(username2)) { //if usernames are equal, remove the driver
  396. it1.next();
  397. it1.remove();
  398. }
  399. }
  400. break;
  401. case 9:
  402. //prompt user for the reg
  403. System.out.print("You selected move Vehicle \n");
  404. Scanner keyboard8 = new Scanner(System.in);
  405. System.out.println("Please enter the Reg No. of the vehicle you wish to move: \n");
  406. String reg2 = keyboard8.nextLine();
  407.  
  408. Iterator<Vehicle> it3 = vehicleArray.iterator(); //iterate through vehicles
  409. while (it3.hasNext()) {
  410. Vehicle user = it3.next();
  411. if (user.CheckRegNo(reg2)) { //if the usernames are equal, remove the vehicle and prompt for new depot
  412. it3.remove();
  413. System.out.println("Please enter the Depot you wish to move the vehicle to: \n");
  414. String name = keyboard8.nextLine();
  415.  
  416. for (int i = 0; i < system.depotArray.size(); i++) { //iterate through Depots
  417. if (system.depotArray.get(i).checkDepotName(name)) { //if there is a depot with the same name add the vehicle to it
  418. //system.depotArray.get(i).remove();
  419. system.depotArray.get(i).vehicleArray.add(user);
  420. }
  421. }
  422. }
  423. }
  424. break;
  425. case 10:
  426. //close the system
  427. System.exit(0);
  428. break;
  429. default:
  430. System.out.print("The selection is invalid.\n");
  431. break;
  432. }
  433. } while (selection != 4);
  434. }
  435. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement