Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help modifying the doSimulation ( ) method below I've included the rest of the assingment for context. Here is a class diagram showing

I need help modifying the doSimulation() method below I've included the rest of the assingment for context. Here is a class diagram showing the attributes and operations of your system. Following the diagram, is a table describing the purpose of the operations listed. You should understand the class diagram and operations descriptions before starting to code. Class/Enum Method Name Description FlightOps initializeFlightList Reads in a sample flight list into a LinkedList from a CSV file. ReadCSVWithScanner LinkedListgetFlightListFromCSV(String filePath) Reads in a sample flight list into a LinkedList from a CSV file. FlightOps void doSimuluation() Driver method for airport operations simulation - initializes the LinkedList, modifies statuses randomly, and then processes the status changes, modifying the LinkedList as necessary FlightsOps void changeStatuses() Changes statuses from default Scheduled to random values FlightOps void removeCancelledFlights() Removes flights with statuses causes cancellation of flight FlightOps void printFlights() Prints a list of flights in the LinkedList FlightOps void moveQueuedFlights(){ Flights with status Queued should be moved to the end of the LinkedList FlightOps void presidentAndCroniesJumpTheQueue() President and cronies jump to the front of the list on demand. Sample of Linked List Methods you will likely find useful. Use as many of these as you can: addLast(); addFirst(); add(Flight e) indexOf(Object o) getFirst() getLast(); peek(); peekLast(); poll() or remove() pollLast(); peekLast(); add(index, element) Preliminary Instructions: Download and extract the provided stubbed out project zip file Download Download and extract the provided stubbed out project zip fileinto your IdeaProjects home directory. Open the project. This project will compile but is incomplete. Your job will be to correct it. Note that a provided Flights.csv (Comma-Delimited-Value) file is located in the resources directory under your src directory in IntelliJ. Take a look at this file. There is also a file called Employees.csv in this directory, which is provided so you can practice using an external file in a program. ReadCSVFromScannerEmployee has a main method and you can run ReadCSVFromScannerEmployee from your project. Just open this source file and select the second Run menu and you will be able to pick which Runnable to execute. This will load and print out the data in the Employees.csv file. See the Tutorial on CSV files in Unit 9 so that you understand how to use CSV files. If this works correctly, you should not have trouble with the Flights.csv file. Examine the Flight class in the project. Modify the class ReadCSVWithScanner patterning your code from the ReadCSVFromScannerEmployee class. Your modified class should be able to read and process the Flights.CSV file without errors. Test it successfully before proceeding. Modify the doSimulation() method in the provided FlightOps class file where indicated: Most of the code for this assignment is already provided for you. Several methods from the FlightOps class called below are stubbed out in the project file provided and will require you to supply needed code where indicated. You will need to complete the printFlights() method, and add the appropriate LinkedList methods addFirst(), addLast(), add(), and remove(), where appropriate. What happens in DoSimulation() The method initializeFlightList() builds a LinkedList of flights with a default status of Scheduled. The method changeStatuses() is then called to set the statuses of the status to a random status to make our operations more interesting. Certain statuses make sense only for arrivals or departures. Examine the setOperationStatus() and understand how it works. OperationStatuses that will result in a flight cancellation are identified and those flights are removed from the list. The operation uses the remove() method. The president and his cronies can jump the queue, ThreeVIP flights are created and added to the list. You are welcome to add more. Finally, flights with the status of Queued are moved to the bottom of the LinkedList using the addLast method. Note that we cannot simply use for-next loops to remove or move items within the loop, since that would affect the the list that we are looping through and create a concurrency exception. We, therefore, use a Stack to keep track of items to be removed or moved while going through the for loop, then in a separate loop popping them off the stack to do the remove operation. This is the whole FlightOps.java file: import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.LinkedList; import java.util.Stack; public class FlightOps { LinkedList flts = new LinkedList<>(); private void printFlights(){ for (Flight f : flts) System.out.println(toString()+ f.toString()); } private void removeCancelledFlights(){ Stack removeStack = new Stack(); // Remove flights with cancellation statuses, use stack to avoid concurrency problem with for-next for (Flight flt: flts){ if (flt.operationStatus == Flight.OperationStatus.CancelDueCrash || flt.operationStatus == Flight.OperationStatus.CancelDueDrunkPilot || flt.operationStatus == Flight.OperationStatus.CancelDueMaintenance || flt.operationStatus == Flight.OperationStatus.CancelDuePassengerDisturbance || flt.operationStatus == Flight.OperationStatus.NavigationError || flt.operationStatus == Flight.OperationStatus.CancelNoPlane){ removeStack.push(flt); }} while (!removeStack.isEmpty()){ flts.remove(removeStack.pop()); }}// Change statuses from default Scheduled to a random status. Some statuses make sense only for Arrival or Departure so // FlightType is checked before setting private void changeStatuses(){ for (Flight flt : flts){ Flight.OperationStatus status =(Flight.OperationStatus) Flight.OperationStatus.getRandomStatus(); if (flt.flightType == Flight.FlightType.Arrival){ if (status == Flight.OperationStatus.CancelDueCrash || status == Flight.OperationStatus.NavigationError || status == Flight.OperationStatus.Scheduled || status == Flight.OperationStatus.Queued){ flt.setOperationStatus(status); }// if status doesn't make sense, don't set it } else if ((flt.flightType == Flight.FlightType.Arrival && status == Flight.OperationStatus.CancelDueMaintenance)||(flt.flightType == Flight.FlightType.Departure && status == Flight.OperationStatus.NavigationError)){ continue; } else { flt.setOperationStatus(status); }}}// Move flights with Queued status to end of LinkedList private void moveQueuedFlights(){ Stack moveStack = new Stack(); for (Flight flt : flts){// Use a stack to move items to be removed // Items can't be removed from within for-next because loop will hit a concurrency exception if (flt.operationStatus == Flight.OperationStatus.Queued){ moveStack.push(flt); }}// Now we use the stack to move the items to end while (!moveStack.isEmpty()){// Save this item before removing Flight flight =(Flight) moveStack.peek(); // Now remove it flts.remove(moveStack.pop()); // Add your code here // add flight to bottom of LinkedList -- see addLast() method }} private void presidentAndCroniesJumpTheQueue(){// President and his cronies jump the line SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy HH:mm"); try { Date date1= sdf.parse("10/15/2007:30"); Date date2= sdf.parse("10/15/2007:30"); Date date3= sdf.parse("10/15/2007:30"); Flight vipFlight1= new Flight("Vip001","AF-01","CDG", date1, Flight.FlightType.Departure); Flight vipFlight2= new Flight("Vip002","AF-01","CDG", date2, Flight.FlightType.Departure); Flight vipFlight3= new Flight("Vip003","AF-01","CDG", date3, Flight.FlightType.Arrival); // Add your code here to add the above three flights to the top of the LinkedList -- See addFirst() method // Add your code here to add the above three flights to the top of the LinkedList -- See addFirst() method } catch (ParseException e){ System.out.println("Date Parse Exception"); }} public void doSimuluation(String filePath){ flts = initializeFlightList(filePath); changeStatuses(); System.out.println("Changed statuses"); printFlights(); System.out.println("Remove cancelled flights"); removeCancelledFlights(); printFlights(); presidentAndCroniesJumpTheQueue(); System.out.println("Cronies jump queue"); printFlights(); moveQueuedFlights(); System.out.println("Moved queued flights"); printFlights(); } public static void main(String[] args){ FlightOps fltOPs = new FlightOps(); String filePath ="./resources/Flights.csv"; fltOPs.doSimuluation(filePath); } public LinkedList initializeFlightList(String filePath){// Load flights from external file ReadCSVWithScanner csvReader = new ReadCSVWithScanner(); LinkedList fltList = csvReader.getFlightListFromCSV(filePath); return fltList; }}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions