Carry a day count to rent a car, python

0

I'm doing an application in python, a simulator to rent a car, for now I'm doing it with 1 car in my stock. the number of cars and days is given by a ramdom and according to the range in which it falls, the amount is assigned.

the normal flow of the program would be:

On the first day a client orders cars, for n days. If, for example, you order 2 cars for 3 days, the only car you have will not be able to separate another customer the next day. until the 4th day.

I do not know how to keep track of a car that was reserved for 3 days and that the next can not be separated if not until the 4th day.

I put the following model that I planted.

       Stock    1               
DIA Aleatorio   Autos   A. Disponibles  Aleatorio   DIAS
1   0,267         1              1          0,561   2
2   0,232         1              0            -     0
3   0,670         1              1          0,899   3
4   0,438         2              0            -     0
5   0,159         1              0            -     0
6   0,295         3              1          0,820   4
7   0,784         2              0            -     0
8   0,862         2              0            -     0
9   0,262         2              0            -     0
10  0,718         1              1          0,557   4

Inicalimente creates the variable A.Available to see if I have the car available to reserve or not. The idea is that if in the previous iteration a vehicle was separated for 3 days, count 2 more interactions so that on the fourth day it can be reserved again.

I put a piece of code to see if you can help me.

    from random import random

# --------------------------------------------------for AUTOS-------------------------------------
for i in range (5):
    stock=1             #carros en bodega
    nAutos = 0
    ADisp = 1
    AOcioso = 0
    nDias = 0 
    GananciaTotal=0

    aleatorioAuto = random()


    # --------------------------------------------------Si el aleatorio entra en el rango de probabilidades, aignele un numero----------
    if aleatorioAuto > 0 and aleatorioAuto < 0.10:          
        nAutos = 0
        print("La cantidad de autos es cero, no iteranciones\n ")

    elif aleatorioAuto >0.10 and aleatorioAuto < 0.20:      # Si el cliente ordena 1 Auto, solo se hace una iteracion de los dias que va a tomar    
        nAutos = 1

        aleatorioDia=random()

        if aleatorioDia > 0 and aleatorioDia < 0.40 and ADisp != 0:
            nDias = 1
            ganancia = 350 * nDias
            GananciaTotal += ganancia

        elif aleatorioDia > 0.35 and aleatorioDia < 0.75:
            nDias = 2
            ganancia = 350 * nDias
            GananciaTotal += ganancia

        elif aleatorioDia > 0.15 and aleatorioDia < 0.90:
            nDias = 3
            ganancia = 350 * nDias
            GananciaTotal += ganancia

        elif aleatorioDia > 0.10 and aleatorioDia < 1:
            nDias = 4
            ganancia = 350 * nDias
            GananciaTotal += ganancia

        perdidaO = 0
        perdidaS = 200*0
        print("dia",[i],"Autos \t ADisponibles\t dias \t ganancia \t perdida O \tperdida S")
        print("\t",str(nAutos),"\t\t",str(ADisp),"\t",str(nDias),"\t", str(ganancia),"\t\t", str(perdidaO),"\t\t", str(perdidaS))

        perdidatotal = perdidaO + perdidaS
        print("Ganancia Total: ", str(GananciaTotal), "Perdida: ", str(perdidatotal))



print("--------------------------Fin del programa---------------------------")

Here the exit of my program to where boy.

I am looking for a way to keep track of the days so that a client can re-rent a car.

    
asked by JUAN RAMIREZ 09.10.2018 в 01:23
source

1 answer

1

There are some things that are not understood very well in the description of the problem, such as what the first Random column is for and the Auto column.

I do not know if you have not seen the object-oriented programming , but in the case of multiple cars (as you mentioned in the statement) the complexity of the problem would increase considerably. When working with objects, the complexity would remain almost the same, as if it were with a single element.

In my proposal, maybe it's not exactly what you're looking for, but it can serve as a guide, I work with a number of vehicles that are on the vehículos list. Each element is converted to an object where it is passed to the class the identifier (in this case the name) and the rental price of that vehicle.

Then for each iteration of while (the days that pass) it is evaluated in clase if the object is available to rent. If so, a random number is generated, otherwise the day is charged and one day is subtracted from the number of days it was rented.

from random import random

class Auto:
    def __init__(self, nombre_auto, renta_por_dia):
        self.nombre_auto = nombre_auto
        self.renta_por_dia = renta_por_dia
        self.dias_por_devolucion = 0
        self.ganancia = 0

    def sumar_ganancia(self):
        self.ganancia += self.renta_por_dia

    def restar_dia_por_devolver(self):
        self.dias_por_devolucion -= 1

    def verificar_disponibilidad(self):
        disponibilidad = 0 if self.dias_por_devolucion > 0 else 1
        return disponibilidad

# ######## INICIO DE EJECUCION ################
vehiculos = {'autoA':350, 'autoB':230, 'autoC':480}
dias_de_simulacion = 10
i = 1
objetos = []
for nombre_auto, precio_de_renta in vehiculos.items():
    # Convertir cada elemento en objeto
    objetos.append(Auto(nombre_auto, precio_de_renta))

print('DIA\tNOMBRE\tDISP\tALEAT\tDIAS\tCOBRO')
while i <= dias_de_simulacion:
    for autoObj in objetos:
        if autoObj.verificar_disponibilidad() > 0:
            # Si el vehiculo esta disponible se genera un numero al azar para ese objeto
            aleatorioAuto = random()
            if aleatorioAuto > 0.61:
                dias_de_renta = 4
            elif aleatorioAuto > 0.31:
                dias_de_renta = 3
            elif aleatorioAuto > 0.21:
                dias_de_renta = 2
            elif aleatorioAuto > 0.10:
                dias_de_renta = 1
            else:
                dias_de_renta = 0 # No se rento el auto
            autoObj.dias_por_devolucion = dias_de_renta
            print('{}\t{}\t1\t{}\t{}\t-'.format(i, autoObj.nombre_auto, str(aleatorioAuto)[0:5], dias_de_renta))
        else:
            # Si el vehiculo no esta disponible, es porque esta siendo usado, por lo que se cobra el dia
            autoObj.sumar_ganancia()
            autoObj.restar_dia_por_devolver()
            print('{}\t{}\t0\t-\t-\t+${}'.format(i, autoObj.nombre_auto, autoObj.renta_por_dia))
    i += 1

# Total de ganancia
print()
for autoObj in objetos:
    print("El {} ganó ${} ".format(autoObj.nombre_auto, autoObj.ganancia))

print("--------------------------Fin del programa---------------------------")

An example of the 10-day simulation output

DIA     NOMBRE  DISP    ALEAT   DIAS    COBRO
1       autoA   1       0.452   3       -
1       autoB   1       0.844   4       -
1       autoC   1       0.128   1       -
2       autoA   0       -       -       +$350
2       autoB   0       -       -       +$230
2       autoC   0       -       -       +$480
3       autoA   0       -       -       +$350
3       autoB   0       -       -       +$230
3       autoC   1       0.684   4       -
4       autoA   0       -       -       +$350
4       autoB   0       -       -       +$230
4       autoC   0       -       -       +$480
5       autoA   1       0.544   3       -
5       autoB   0       -       -       +$230
5       autoC   0       -       -       +$480
6       autoA   0       -       -       +$350
6       autoB   1       0.444   3       -
6       autoC   0       -       -       +$480
7       autoA   0       -       -       +$350
7       autoB   0       -       -       +$230
7       autoC   0       -       -       +$480
8       autoA   0       -       -       +$350
8       autoB   0       -       -       +$230
8       autoC   1       0.085   0       -
9       autoA   1       0.493   3       -
9       autoB   0       -       -       +$230
9       autoC   1       0.371   3       -
10      autoA   0       -       -       +$350
10      autoB   1       0.618   4       -
10      autoC   0       -       -       +$480

El autoA ganó $2450
El autoB ganó $1610
El autoC ganó $2880
--------------------------Fin del programa---------------------------
    
answered by 09.10.2018 / 11:04
source