public
Last active

I apologize if this is a larger program than usual, I just need some feedback on efficiency, advice, and stuff like that. Thanks!

  • Download Gist
Elevator.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
import java.util.ArrayList;
 
public class Elevator
{
private int totalNumPeople;
private int currentFloor;
private int targetFloor;
private ArrayList<Group> onBoard;
public Elevator()
{
totalNumPeople = 0;
currentFloor = 5;
targetFloor = 5;
onBoard = new ArrayList<Group>();
}
public int getTotalNumPeople() { return totalNumPeople; }
public int getCurrentFloor() { return currentFloor; }
public void move()
{
if(currentFloor > targetFloor)
currentFloor--;
else if(currentFloor < targetFloor)
currentFloor++;
}
// We might be able to pick groups up on the way as well, but they shouldn't affect our current path
public void board(ArrayList<Group> guests, int timePassed)
{
for(Group g: guests)
{
if(g.isAvailable(timePassed) && canBoard(g) && g.getStartFloor() == currentFloor)
{
onBoard.add(g);
g.setElevator(this);
targetFloor = g.getEndFloor();
totalNumPeople += g.getNumPeople();
}
}
}
public void unboard()
{
for(int i = 0; i < onBoard.size(); i++)
{
if(currentFloor == onBoard.get(i).getEndFloor())
{
onBoard.get(i).setArrived(true); // This group has reached their destination! Time to remove them and forget about them.
onBoard.get(i).setElevator(null);
totalNumPeople -= onBoard.get(i).getNumPeople();
onBoard.remove(i);
}
}
}
public void getClosestGroup(ArrayList<Group> guests, int timePassed)
{
int min = 11;
for(Group g: guests)
{
if(g.isAvailable(timePassed) && canBoard(g) && Math.abs(g.getStartFloor() - currentFloor) < min)
targetFloor = g.getStartFloor();
}
}
// checks this groups number of people against the total amount of people in the elevator
public boolean canBoard(Group g)
{
if(g.getNumPeople() + totalNumPeople > 6)
return false;
return true;
}
}
Group.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
 
public class Group
{
private int numPeople;
private int delay;
private int startFloor;
private int endFloor;
private boolean arrived;
private Elevator elevator;
public Group(int numPeople, int delay, int startFloor, int endFloor)
{
this.numPeople = numPeople;
this.delay = delay;
this.startFloor = startFloor;
this.endFloor = endFloor;
this.elevator = null;
this.arrived = false;
}
public boolean isAvailable(int timePassed)
{
return isPresent(timePassed) && !hasElevator() && !hasArrived();
}
public void setElevator(Elevator e) { this.elevator = e; }
public void setArrived(boolean b)
{
arrived = b;
}
public boolean isPresent(int timePassed)
{
if(timePassed >= delay)
return true;
return false;
}
public boolean hasArrived()
{
return arrived;
}
public int getNumPeople() { return numPeople; }
public int getStartFloor() { return startFloor; }
public int getEndFloor() { return endFloor; }
public boolean hasElevator()
{
if(elevator != null)
return true;
return false;
}
}
Hotel.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
import java.applet.Applet;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.Timer;
 
public class Hotel implements ActionListener
{
public ArrayList<Group> guests = new ArrayList<Group>(); // All of the guests in the hotel
public ArrayList<Elevator> elevators = new ArrayList<Elevator>();
public Scanner s;
public Timer t;
public int timePassed;
public static void main(String[] args)
{
new Hotel().init();
}
public void init()
{
t = new Timer(1000, this); // Elevators will move one floor every second
t.setActionCommand("timer");
for(int i = 0; i < 3; i++)
elevators.add(new Elevator()); // There will be three elevators working at the same time in this hotel
s = new Scanner(System.in);
int numGroups = 0, numPeople = 0, delay = 0, startFloor = 0, endFloor = 0; // Info for the each group, when/where they'll arrive/need to go
System.out.println("How many groups will be in the simulation?");
numGroups = s.nextInt();
for(int i = 0; i < numGroups; i++)
{
System.out.println("How many people are in group " + i + "?");
numPeople = s.nextInt();
System.out.println("How many milliseconds until they enter the simulation?");
delay = s.nextInt();
System.out.println("What floor will they start on? (1 - 10)");
startFloor = s.nextInt();
System.out.println("What floor will be their destination? (1 - 10)");
endFloor = s.nextInt();
guests.add(new Group(numPeople, delay, startFloor, endFloor));
}
timePassed = 0;
run(); // Begin firing the timer
}
public void run()
{
t.start();
while(!allArrived())
{
}
t.stop();
}
// check if all of the groups have arrived at their intended destination
public boolean allArrived()
{
for(Group g: guests)
{
if(!g.hasArrived())
return false;
}
return true;
}
 
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("timer"))
{
for(Elevator el: elevators) // Initially, the first elevator in the array will get dibs on the absolute closest group
{
// main algorithm, most actual code is in the Elevator class
el.getClosestGroup(guests, timePassed);
el.move();
el.unboard();
el.board(guests, timePassed);
}
timePassed += 1000; // Increment global time by a second
for(int i = 0; i < elevators.size(); i++) // Print some useful info to the console at every step
{
System.out.println("Elevator " + i + ": Passengers - " + elevators.get(i).getTotalNumPeople() + ", " +
"Floor - " + elevators.get(i).getCurrentFloor());
}
System.out.println(allArrived());
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.