program tip

Amazon 인터뷰 질문 : OO 주차장 설계

radiobox 2020. 7. 30. 10:17
반응형

Amazon 인터뷰 질문 : OO 주차장 설계


OO 주차장을 설계하십시오. 어떤 클래스와 함수가 있을까요? 비어 있고 비어 있으며 주차 대행 장소를 찾을 수 있어야합니다. 주차장에는 일반, 장애인 및 소형의 3 가지 주차 유형이 있습니다.

감사!


기어를 돌리는 빠른 시작은 다음과 같습니다.

ParkingLot는 클래스입니다.

ParkingSpace는 수업입니다.

ParkingSpace에는 입구가 있습니다.

입구는 입구와 거리가 있습니다.

ParkingLotSign은 클래스입니다.

ParkingLot에는 ParkingLotSign이 있습니다.

ParkingLot에는 한정된 수의 ParkingSpace가 있습니다.

HandicappedParkingSpace는 ParkingSpace의 서브 클래스입니다.

RegularParkingSpace는 ParkingSpace의 서브 클래스입니다.

CompactParkingSpace는 ParkingSpace의 서브 클래스입니다.

ParkingLot는 ParkingSpaces와 별도의 빈 ParkingSpaces를 입구와의 거리 순서대로 유지합니다.

ParkingLotSign은 .Full (), .Empty () 또는 .Normal ()을 호출하여 "full"또는 "empty"또는 "blank / normal / partedally occupied"를 표시하도록 지시 할 수 있습니다.

Parker는 수업입니다.

Parker는 Park ()를 할 수 있습니다.

Parker는 Unpark ()를 해제 할 수 있습니다.

Valet는 ParkingLot.FindVacantSpaceNearestEntrance ()를 호출 할 수있는 Parker의 하위 클래스이며 ParkingSpace를 반환합니다.

Parker에는 ParkingSpace가 있습니다.

Parker는 ParkingSpace.Take () 및 ParkingSpace.Vacate ()를 호출 할 수 있습니다.

Parker는 Entrance.Entering () 및 Entrance.Exiting ()을 호출하고 ParkingSpace는 ParkingLot가 꽉 찼는 지 여부를 확인할 수 있도록 ParkingLot에 알립니다. 새로 채워지거나 새로 비워 지거나 새로 채워지거나 비어 있지 않으면 ParkingLotSign.Full () 또는 ParkingLotSign.Empty () 또는 ParkingLotSign.Normal ()을 변경해야합니다.

HandicappedParker는 Parker의 하위 클래스이고 Parker의 하위 클래스 인 CompactParker이고 Parker의 하위 클래스 인 RegularParker 일 수 있습니다. (실제로 과잉 일 수 있습니다.)

이 솔루션에서 Parker의 이름을 Car로 바꾸어야합니다.


public class ParkingLot 
{
    Vector<ParkingSpace> vacantParkingSpaces = null;
    Vector<ParkingSpace> fullParkingSpaces = null;

    int parkingSpaceCount = 0;

    boolean isFull;
    boolean isEmpty;

    ParkingSpace findNearestVacant(ParkingType type)
    {
        Iterator<ParkingSpace> itr = vacantParkingSpaces.iterator();

        while(itr.hasNext())
        {
            ParkingSpace parkingSpace = itr.next();

            if(parkingSpace.parkingType == type)
            {
                return parkingSpace;
            }
        }
        return null;
    }

    void parkVehicle(ParkingType type, Vehicle vehicle)
    {
        if(!isFull())
        {
            ParkingSpace parkingSpace = findNearestVacant(type);

            if(parkingSpace != null)
            {
                parkingSpace.vehicle = vehicle;
                parkingSpace.isVacant = false;

                vacantParkingSpaces.remove(parkingSpace);
                fullParkingSpaces.add(parkingSpace);

                if(fullParkingSpaces.size() == parkingSpaceCount)
                    isFull = true;

                isEmpty = false;
            }
        }
    }

    void releaseVehicle(Vehicle vehicle)
    {
        if(!isEmpty())
        {
            Iterator<ParkingSpace> itr = fullParkingSpaces.iterator();

            while(itr.hasNext())
            {
                ParkingSpace parkingSpace = itr.next();

                if(parkingSpace.vehicle.equals(vehicle))
                {
                    fullParkingSpaces.remove(parkingSpace);
                    vacantParkingSpaces.add(parkingSpace);

                    parkingSpace.isVacant = true;
                    parkingSpace.vehicle = null;

                    if(vacantParkingSpaces.size() == parkingSpaceCount)
                        isEmpty = true;

                    isFull = false;
                }
            }
        }
    }

    boolean isFull()
    {
        return isFull;
    }

    boolean isEmpty()
    {
        return isEmpty;
    }
}

public class ParkingSpace 
{
    boolean isVacant;
    Vehicle vehicle;
    ParkingType parkingType;
    int distance;
}

public class Vehicle 
{
    int num;
}

public enum ParkingType
{
    REGULAR,
    HANDICAPPED,
    COMPACT,
    MAX_PARKING_TYPE,
}

모델은 분리되어 존재하지 않습니다. 주차장에 들어가는 차량 시뮬레이션, 빈 공간으로 안내하는 내장 시스템, 주차장 청구 시스템 또는 주차장에서 일반적으로 사용되는 자동화 된 게이트 / 티켓 시스템에 대해 정의한 구조는 모두 다릅니다.


In an Object Oriented parking lot, there will be no need for attendants because the cars will "know how to park".

Finding a usable car on the lot will be difficult; the most common models will either have all their moving parts exposed as public member variables, or they will be "fully encapsulated" cars with no windows or doors.

The parking spaces in our OO parking lot will not match the size and shape of the cars (an "impediance mismatch" between the spaces and the cars)

License tags on our lot will have a dot between each letter and digit. Handicaped parking will only be available for licenses beginning with "_", and licenses beginning with "m_" will be towed.


you would need a parking lot, that holds a multi-dimensional array (specified in the constructor) of a type "space". The parking lot can keep track of how many spaces are taken via calls to functions that fill and empty spaces.Space can hold an enumerated type that tells what kind of space it is. Space also has a method taken(). for the valet parking, just find the first space thats open and put the car there. You will also need a Car object to put in the space, that holds whether it is a handicapped, compact, or regular vehicle.


class ParkingLot
{
    Space[][] spaces;

    ParkingLot(wide, long); // constructor

    FindOpenSpace(TypeOfCar); // find first open space where type matches
}

enum TypeOfSpace = {compact, handicapped, regular };
enum TypeOfCar = {compact, handicapped, regular };

class Space
{
    TypeOfSpace type;
    bool empty;
    // gets and sets here
    // make sure car type
}

class car
{
    TypeOfCar type;
}

참고URL : https://stackoverflow.com/questions/764933/amazon-interview-question-design-an-oo-parking-lot

반응형