Overview of Java Logistics Simulation Controller
The Java Logistics Simulation Controller is a pivotal piece of code that manages a logistics simulation, efficiently distributing packages between drones and trucks based on predefined parameters. This controller reads a train schedule from a file, initializing and executing a simulation while tracking key statistics. It demonstrates proficiency in file handling, mathematical calculations, and object-oriented programming principles. This code can be instrumental for those seeking insight into logistics simulations or individuals looking for assistance with their Java assignment, providing a solid foundation for understanding logistics system orchestration and simulation dynamics.
Block 1: Imports and Class Declaration
import Train.TrainAtCrossingEvent;
import Train.TrainEvent;
import api.Simulator;
import api.Statistics;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MainController {
Discussion:
- Import statements are used to include necessary classes and interfaces.
- The MainController class is declared, which serves as the entry point for the simulation.
Block 2: Constants Declaration
private static final float PERCENT_BY_DRONE = 0.25f;
private static final int TOTAL_PACKAGES = 1500;
Discussion:
- Constants are defined for the percentage of packages to be handled by drones and the total number of packages.
Block 3: Calculation of Drones and Trucks
int numDrones = Math.round(PERCENT_BY_DRONE * TOTAL_PACKAGES);
int byTrucks = TOTAL_PACKAGES - numDrones;
int numTruck = (int)Math.ceil(byTrucks/10.0);
Discussion:
- The number of drones is calculated based on the percentage.
- The number of packages to be handled by trucks is determined.
- The number of trucks needed is calculated, considering that each truck can handle 10 packages.
Block 4: Output of Drones and Trucks Information
System.out.println("With " + PERCENT_BY_DRONE*100 + "% drones and " + TOTAL_PACKAGES + " packages, There will be:\n-" + numDrones + " drones\n-" + numTruck + " trucks\n");
Discussion:
- Outputs information about the percentage of drones, total packages, and the number of drones and trucks required for the simulation.
Block 5: Train Schedule Reading
System.out.println("TRAIN SCHEDULE\n--------------");
List
eventList = new ArrayList<>();
try {
Scanner scanner = new Scanner(new File("train_schedule.txt"));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
eventList.add(new TrainAtCrossingEvent(Double.parseDouble(line.split(" ")[0]), Double.parseDouble(line.split(" ")[1])));
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println();
Discussion:
- Outputs a header for the train schedule.
- Reads a train schedule from a file ("train_schedule.txt") and prints each line.
- Each line is used to create a TrainAtCrossingEvent and added to the eventList.
Block 6: Statistics and Simulator Initialization
Statistics statistics = new Statistics(numTruck, numDrones);
Simulator simulator = new Simulator(numTruck, eventList, statistics);
Discussion:
- Initializes a Statistics object with the number of trucks and drones.
- Initializes a Simulator object with the number of trucks, the train schedule events, and the statistics object.
Block 7: Simulation Execution and Statistics Printing
simulator.doAllEvents();
statistics.printStatistics();
Discussion:
- Executes all events in the simulation using the doAllEvents method of the simulator.
- Prints the statistics of the simulation using the printStatistics method of the statistics object.
Conclusion
In conclusion, the MainController class orchestrates a logistics simulation by determining the distribution of packages between drones and trucks, reading a train schedule, and executing the simulation through a Simulator object. The code emphasizes modularity and encapsulation by employing distinct classes for events, statistics, and simulation. The file handling aspect ensures flexibility in adapting to different train schedules. Overall, the code exemplifies a systematic approach to logistics optimization, leveraging object-oriented principles and encapsulating functionalities within well-defined classes. The simulation results, encapsulated in the Statistics object, are then presented, offering insights into the efficiency and performance of the proposed logistics strategy.