简体   繁体   中英

If I have a number of Stacks that have an order, how can I best iterate through them to get the right spot?

I am modelling a car park in Java.

I have three stacks that represent free spots of different sizes in my Car Park Class.

 public class CarPark {
    Stack<Spot> smallSpots;
    Stack<Spot> mediumSpots;
    Stack<Spot> largeSpots;

The Spot class contains a global variable SpotSize, which I've defined as an enum:

public enum SpotSize {
    Small,
    Medium,
    Large,
}

Now I have a method with my CarPark class which takes a Vehicle and checks the right stack for a spot, if a spot is available take it.

Vehicles can be of three sizes, also small, medium or large. A vehicle of size small can fit into a spot of any size, a vehicle of size medium can fit into a spot of medium or large and a vehicle of size large can only fit into a spot of size large.

My question is what's the most efficient way (in terms of both performance and amount of code written) of checking each of the stacks given a vehicle?

I really don't want to do something like this:

if(vehicle.getSize().equals(VehicleSize.Small)){
//check small spots
//check medium spots
//check large spots
}

if(vehicle.getSize().equals(VehicleSize.Medium)){
//check medium spots
//check large spots
}


if(vehicle.getSize().equals(VehicleSize.Large)){
//check large spots
}

because this seems a little too hard coded and also will not scale if I decide to have more sizes of spots and vehicles.

You could implement this (particular) case using switch fall-through and return:

switch (vehicle.getSize()) {
case VehicleSize.Small:
  // check small spots, return if found
case VehicleSize.Medium:
  // check medium spots, return if found
case VehicleSize.Large:
  // check large spots, return if found
}

There should be no break statements, so if the small spot check doesn't succeed for a small car, the medium spots are checked next, and so on. After the switch , you can throw an error or return a null to indicate no spot was found.

Assumption: VehicleSize is defined in natural order:

public enum VehicleSize {
    SMALL, MEDIUM, LARGE;
}

Then there could be a List<Stack<Spot>> spotSizes where the index of each element refers to the ordinal value of a VehicleSize , eg, spotSizes.get(1) refers to the stack of VehicleSize.MEDIUM spots.

List<Stack<Spot>> = new ArrayList<>(VehicleSize.values().length);
// ... init each stack, and need something to indicate how many spots are available for that VehicleSize

public boolean isParkingSpotAvailable(Vehicle v) {
    for (int size = v.getSize().ordinal(); size < VehicleSize.values().length; size++) {
        if (spotSizes.get(size) available spots > 0) {
           return true;
        }
    }
        return false;
}

So if a SUB_COMPACT is added (must be in natural order):

public enum VehicleSize {
    SUB_COMPACT, SMALL, MEDIUM, LARGE;
}

then isParkingSpotAvailable() will not have to change. But there would still need to be some way to initialize the related stack, perhaps in a properties file:

 SUB_COMPACT.available.spots=12
 SMALL.available.spots=24
 MEDIUM.available.spots=100
 LARGE.available.spots=42

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM