Parking class in Python

0

I'm learning to program in Python.

At this point I need to implement a class that has the following characteristics:

A class called parking, which simulates the operation of a parking lot.

- The class receives an integer and determines the park's capacity

- The class returns an object with the following methods: Enter (): corresponds to the input of a car

-Sai (): corresponds to the output of a car

-Links (): indicates the number of free seats in the parking lot.

I'm having trouble creating the code.

I can not get The class gets an integer and determines the park's stocking

Code

    
asked by anonymous 09.06.2018 / 17:35

1 answer

1

Your parking control class can be something like:

class Estacionamento:

    def __init__( self, max_vagas ):
        self.max_vagas = max_vagas
        self.vagas_ocupadas = 0

    def maximo( self ):
        return self.max_vagas

    def disponiveis( self ):
        return self.max_vagas - self.vagas_ocupadas

    def ocupadas( self ):
        return self.vagas_ocupadas

    def entra( self ):
        if( self.vagas_ocupadas == self.max_vagas ):
            raise Exception('Estacionamento estah lotado!')
        self.vagas_ocupadas += 1

    def sai( self ):
        if( self.vagas_ocupadas == 0 ):
            raise Exception('Estacionamento estah vazio!')
        self.vagas_ocupadas -= 1

Testing:

n = int(input('Entre com a lotacao maxima do estacionamento: '))

e = Estacionamento(n)

e.entra()
e.entra()
e.entra()
e.entra()
e.sai()

print( e.maximo() )
print( e.disponiveis() )
print( e.ocupadas() )

Output:

Entre com a lotacao maxima do estacionamento: 20
20
17
3
    
10.06.2018 / 01:23