Exemplo n.º 1
0
    def test_valid_parking_lot(self):
        service = ParkingService()
        service.create_parking_lot(4)

        # Testing a decorator, by mocking the function to be decorated and checking if the mock was called or not
        test_service_function = Mock()
        decorated_function = parking_lot_validity_check(test_service_function)
        decorated_function(service)

        test_service_function.assert_called_once()
Exemplo n.º 2
0
    def test_get_slot_number_for_age_with_non_empty_result(self):
        service = ParkingService()
        vehicle_1 = Vehicle('mock_reg_num_1', 22)
        vehicle_2 = Vehicle('mock_reg_num_2', 25)
        vehicle_3 = Vehicle('mock_reg_num_3', 22)

        service.create_parking_lot(3)
        service.park(vehicle_1)
        service.park(vehicle_2)
        service.park(vehicle_3)

        expected = ['1', '3']
        self.assertEqual(service.get_slot_number_for_age(22), expected)
Exemplo n.º 3
0
    def test_get_slot_number_for_age_with_empty_result(self):
        service = ParkingService()
        vehicle_1 = Vehicle('mock_reg_num_1', 22)
        vehicle_2 = Vehicle('mock_reg_num_2', 25)

        service.create_parking_lot(3)
        service.park(vehicle_1)
        service.park(vehicle_2)

        self.assertEqual(service.get_slot_number_for_age(18), [])
Exemplo n.º 4
0
    def test_get_slot_number_for_reg_number_invalid_vehicle(self):
        service = ParkingService()
        vehicle_1 = Vehicle('mock_reg_num_1', 22)
        vehicle_2 = Vehicle('mock_reg_num_2', 25)

        service.create_parking_lot(3)
        service.park(vehicle_1)
        service.park(vehicle_2)

        self.assertIsNone(
            service.get_slot_number_for_reg_number('mock_reg_num_3'))
Exemplo n.º 5
0
 def __init__(self):
     self.parking_lots = {}
     self.vehicles = {}
     self.tickets = {}
     self.current_ticket_id = 1
     self.service = ParkingService()
Exemplo n.º 6
0
class ParkingManager(object):
    def __init__(self):
        self.parking_lots = {}
        self.vehicles = {}
        self.tickets = {}
        self.current_ticket_id = 1
        self.service = ParkingService()

    def add_vehicle(self, vehicle_id, vehicle_type):
        if vehicle_type == VehicleType.CAR:
            self.vehicles[vehicle_id] = FourWheeler(vehicle_id)

        elif vehicle_type == VehicleType.BIKE:
            self.vehicles[vehicle_id] = TwoWheeler(vehicle_id)

        else:
            raise Exception("VehicleType not supported.")

    def add_parking_lot(self, parking_lot_id, two_wheeler_capacity, four_wheeler_capacity, two_wheeler_rate, four_wheeler_rate):
        spots = {}
        spot_id = 1
        two_spots = []
        four_spots = []
        rates = {}
        for capacity in range(two_wheeler_capacity):
            two_spots.append(TwoWheelerSpot(spot_id))
            spot_id += 1
        for capacity in range(four_wheeler_capacity):
            four_spots.append(FourWheelerSpot(spot_id))
            spot_id += 1
        rates[ParkingSpotType.BIKE] = two_wheeler_rate
        rates[ParkingSpotType.CAR] = four_wheeler_rate



        self.parking_lots[parking_lot_id] = ParkingLot(parking_lot_id, two_spots, four_spots, rates)

    def check_free_parking(self, vehicle):
        for parking_lot_id, parking_lot in self.parking_lots.items():
            if parking_lot.check_free(vehicle.vehicle_type):
                return parking_lot
        return None

    def park_vehicle(self, vehicle_id):
        parking_lot = self.check_free_parking(self.vehicles[vehicle_id])
        if not parking_lot:
            print("No space")
            return None
        ticket_id = self.current_ticket_id
        self.current_ticket_id += 1
        ticket = self.service.park_vehicle(self.vehicles[vehicle_id], parking_lot, ticket_id)
        self.tickets[ticket.ticket_number] = ticket 
        return ticket.ticket_number

    def exit_vehicle(self, ticket_number):
        ticket = self.tickets[ticket_number]
        amount = self.service.exit_vehicle(ticket, self.parking_lots[ticket.parking_lot_id], self.vehicles[ticket.vehicle_id])
        return amount

    def show_history(self, vehicle_id):
        self.service.show_history(self.vehicles[vehicle_id])
Exemplo n.º 7
0
    def test_create_parking_lot(self):
        service = ParkingService()
        capacity = service.create_parking_lot(5)

        self.assertIsInstance(service.parking_lot, ParkingLot)
        self.assertEqual(capacity, 5)
Exemplo n.º 8
0
class RequestParser:
    """Parser class for parsing and executing user commands.

    Attributes:
        parking_service (ParkingService): Instance of the parking service, will be used to execute user commands
    """
    def __init__(self):
        self.parking_service = ParkingService()

    def parse(self, params):
        """Method to parse (command to execution process) a single line of commands, broken into tokens

        Args:
            params (List[str]): List of tokens obtained from single user command

        Raises:
            CommandNotFoundException: Raised when the user command is invalid.
            InvalidInputException: Raised when the command is valid, but the number of arguments don't match the required number
        """
        command_str = params[0].upper()
        if command_str not in CommandUtils.COMMAND_LIST:
            raise CommandNotFoundException(command_str)

        command = Commands[command_str]
        if len(params) != CommandUtils.VALIDATION_MAP[command]:
            raise InvalidInputException(command, params)

        self.execute_command(command, params[1:])

    def execute_command(self, command, params):
        """Method to execute a valid command

        Args:
            command (Commands): Command enum, denoting the command to be executed
            params (List[str]): List of string parameters required to execute the command
        """
        if command == Commands.CREATE_PARKING_LOT:
            capacity = self.parking_service.create_parking_lot(
                capacity=int(params[0]))
            print(f'Created parking of {capacity} slots')
        elif command == Commands.PARK:
            vehicle = Vehicle(reg_num=params[0], age=int(params[2]))
            slot_id = self.parking_service.park(vehicle=vehicle)
            if slot_id:
                print(
                    f'Car with vehicle registration number "{vehicle.get_reg_num()}" has been parked at slot number {slot_id}'
                )
        elif command == Commands.LEAVE:
            vehicle = self.parking_service.leave(slot_id=int(params[0]))
            if vehicle:
                print(
                    f'Slot number {params[0]} vacated, the car with vehicle registration number "{vehicle.get_reg_num()}" left the space, the driver of the car was of age {vehicle.get_age()}'
                )
        elif command == Commands.SLOT_NUMBER_FOR_CAR_WITH_NUMBER:
            slot_id = self.parking_service.get_slot_number_for_reg_number(
                reg_num=params[0])
            if slot_id:
                print(slot_id)
            else:
                print('Vehicle not found.')
        elif command == Commands.SLOT_NUMBERS_FOR_DRIVER_OF_AGE:
            slot_ids = self.parking_service.get_slot_number_for_age(
                age=int(params[0]))
            if slot_ids:
                print(','.join(slot_ids))
            else:
                print('null')
        elif command == Commands.VEHICLE_REGISTRATION_NUMBER_FOR_DRIVER_OF_AGE:
            reg_nums = self.parking_service.get_reg_number_for_age(
                age=int(params[0]))
            if reg_nums:
                print(','.join(reg_nums))
            else:
                print('null')
Exemplo n.º 9
0
 def __init__(self):
     self.parking_service = ParkingService()
Exemplo n.º 10
0
from config import APP_NAME
from db import lots, entries
from models.parking_lot import ParkingLot
from models.parking_strategy import ParkingStrategy
from models.slot_type import SlotType
from models.vehicle_type import VehicleType
from services.parking_service import ParkingService

print('Starting Parking Lot Demo')

imagine = ParkingLot('Imagine Parking', ParkingStrategy.NATURAL_NUMBER)
lots.append(imagine)

ParkingService.add_slot(imagine, SlotType.LARGE, '001')
# ParkingService.add_slot(imagine, SlotType.MEDIUM, '002')
# ParkingService.add_slot(imagine, SlotType.SMALL, '003')
# ParkingService.add_slot(imagine, SlotType.LARGE, '004')
# ParkingService.add_slot(imagine, SlotType.XLARGE, '005')
# ParkingService.add_slot(imagine, SlotType.SMALL, '006')
# ParkingService.add_slot(imagine, SlotType.XLARGE, '007')
# ParkingService.add_slot(imagine, SlotType.SMALL, '008')

swift = ParkingService.add_vehicle('ABC123', VehicleType.HATCHBACK)
print(swift)
duke = ParkingService.add_vehicle('XYZ321', VehicleType.TWO_WHEELER)
# ecosport = ParkingService.add_vehicle('ZZRE433', VehicleType.SUV)
entry1 = ParkingService.assign_slot(imagine, duke,
                                    ParkingStrategy.NATURAL_NUMBER)
print(entry1)

# entry2 = ParkingService.assign_slot(imagine, duke,