Elevator simulation. I need help fixing this class The Simulation Class initializes and holds objects for Passengers, Elevators, and Floors. It reads and parses input files to create the necessary objects and sets the number of simulation iterations. The class also has methods to move the elevators up and down the building, as well as to pick up and drop off passengers. public class Simulation { private int numFloors; private ArrayList passengers; private ArrayList elevators; private ArrayList floors; private int numIterations; public Simulation(String inputFile) throws FileNotFoundException { passengers = new ArrayList<>(); elevators = new ArrayList<>(); floors = new ArrayList<>(); // Read input file and initialize simulation parameters Scanner scanner = new Scanner(new File(inputFile)); while (scanner.hasNextLine()) { String line = scanner.nextLine().trim(); if (line.startsWith("floors=")) { numFloors = Integer.parseInt(line.substring(line.indexOf('=') + 1)); } else if (line.startsWith("add_passenger")) { String[] parts = line.split("\\s+"); int startFloor = Integer.parseInt(parts[1]); int destFloor = Integer.parseInt(parts[2]); PassengerType passengerType = PassengerType.valueOf(parts[3].toUpperCase()); int percentage = Integer.parseInt(parts[4]); passengers.add(new Passenger(startFloor, destFloor, passengerType, percentage)); } else if (line.startsWith("elevator_type")) {
Elevator simulation. I need help fixing this class The Simulation Class initializes and holds objects for Passengers, Elevators, and Floors. It reads and parses input files to create the necessary objects and sets the number of simulation iterations. The class also has methods to move the elevators up and down the building, as well as to pick up and drop off passengers.
public class Simulation {
private int numFloors;
private ArrayList<Passenger> passengers;
private ArrayList<Elevator> elevators;
private ArrayList<Floor> floors;
private int numIterations;
public Simulation(String inputFile) throws FileNotFoundException {
passengers = new ArrayList<>();
elevators = new ArrayList<>();
floors = new ArrayList<>();
// Read input file and initialize simulation parameters
Scanner scanner = new Scanner(new File(inputFile));
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
if (line.startsWith("floors=")) {
numFloors = Integer.parseInt(line.substring(line.indexOf('=') + 1));
} else if (line.startsWith("add_passenger")) {
String[] parts = line.split("\\s+");
int startFloor = Integer.parseInt(parts[1]);
int destFloor = Integer.parseInt(parts[2]);
PassengerType passengerType = PassengerType.valueOf(parts[3].toUpperCase());
int percentage = Integer.parseInt(parts[4]);
passengers.add(new Passenger(startFloor, destFloor, passengerType, percentage));
} else if (line.startsWith("elevator_type")) {
String[] parts = line.split("\\s+");
String elevatorType = parts[1];
int maxCapacity = Integer.parseInt(parts[2]);
int speed = Integer.parseInt(parts[3]);
elevators.add(new Elevator(elevatorType, maxCapacity, speed));
} else if (line.startsWith("request_percentage")) {
String[] parts = line.split("\\s+");
String elevatorType = parts[1];
int percentage = Integer.parseInt(parts[2]);
for (Elevator elevator : elevators) {
if (elevator.getType().equals(elevatorType)) {
elevator.setPercentage(percentage);
break;
}
}
} else if (line.startsWith("passenger_request_percentage")) {
String[] parts = line.split("\\s+");
PassengerType passengerType = PassengerType.valueOf(parts[1].toUpperCase());
int percentage = Integer.parseInt(parts[2]);
for (Passenger passenger : passengers) {
if (passenger.getType() == passengerType) {
passenger.setPercentage(percentage);
}
}
} else if (line.startsWith("number_of_elevators")) {
int numElevators = Integer.parseInt(line.substring(line.indexOf('=') + 1));
for (int i = 0; i < numElevators; i++) {
elevators.add(new Elevator());
}
} else if (line.startsWith("run_simulation")) {
numIterations = Integer.parseInt(line.substring(line.indexOf('=') + 1));
}
}
// Initialize floors
for (int i = 1; i <= numFloors; i++) {
floors.add(new Floor(i));
}
// Add passengers to floors
for (Passenger passenger : passengers) {
floors.get(passenger.getStartFloor() - 1).addPassenger(passenger);
}
}
public void runSimulation() {
for (int i = 0; i < numIterations; i++) {
// Move elevators up and down floors
for (Elevator elevator : elevators) {
if (!elevator.isBroken()) {
Floor currentFloor = elevator.getCurrentFloor();
ArrayList<Passenger> passengers = elevator.getPassengers();
int numPassengers = passengers.size();
// Drop off passengers
for (int j = numPassengers - 1; j >= 0; j--) {
Passenger passenger = passengers.get(j);
if (passenger.getDestFloor() == currentFloor.getFloorNumber()) {
passengers.remove(j)
elevator.removePassenger(passenger);
}
}
// Pick up passengers
ArrayList<Passenger> passengersToPickUp = currentFloor.getPassengersToPickUp(elevator.getType(), elevator.getPercentage());
for (Passenger passenger : passengersToPickUp) {
if (elevator.addPassenger(passenger)) {
currentFloor.removePassenger(passenger);}}}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps