Thanks to the response of @ aeportugal, I have solved the inconvenience of checking if a car is available again to rent, now to do it with 2 or more cars, the level of complexity goes up.
This application simulates a system for renting vehicles, each day a customer can rent x amount of vehicles (this value is given by a #alternative), followed by a variable available that looks at the amount of cars available to rent (to have more cars than available vehicles, a fine is charged). If there are available vehicles, another #random will run to know how many days you will use them. and the rental price is calculated and totaled.
In the code is the Auto class, where the car is initialized and the rent price by the constructor. followed by three methods:
sumar_ganancia > is encartga of adding the profit that obtained every day.
subtract_dia_to_delete > subtract the days I rent the car to 0, to make the car available again.
verify_availability > check if there is a car available to use.
the output of this program is as follows.
DIA NOMBRE #AUTOS DISP ALEAT DIAS COBRO
1 autoA 2 1 0.024 0 -
1 autoB 1 1 0.990 4 -
2 autoA 3 1 0.721 4 -
2 autoB - 0 - - +$500
3 autoA - 0 - - +$100
3 autoB - 0 - - +$500
4 autoA - 0 - - +$100
4 autoB - 0 - - +$500
5 autoA - 0 - - +$100
5 autoB - 0 - - +$500
6 autoA - 0 - - +$100
6 autoB 1 1 0.762 4 -
7 autoA 1 1 0.507 3 -
7 autoB - 0 - - +$500
8 autoA - 0 - - +$100
8 autoB - 0 - - +$500
9 autoA - 0 - - +$100
9 autoB - 0 - - +$500
10 autoA - 0 - - +$100
10 autoB - 0 - - +$500
El autoA ganó $700
El autoB ganó $4000
--------------------------Fin del programa---------------------------
The code is this;
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':100,'autoB':500}
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\t#AUTOS\tDISP\tALEAT\tDIAS\tCOBRO')
while i <= dias_de_simulacion:
for autoObj in objetos:
if autoObj.verificar_disponibilidad() > 0:
aleatorio=random()
if aleatorio > 0.61:
nAutos = 4
elif aleatorio > 0.31:
nAutos = 3
elif aleatorio > 0.21:
nAutos = 2
elif aleatorio > 0.10:
nAutos = 1
else:
nAutos = 0
# Si el vehiculo esta disponible se genera un numero al azar para ese objeto
aleatorioD = random()
if aleatorioD > 0.61:
dias_de_renta = 4
elif aleatorioD > 0.31:
dias_de_renta = 3
elif aleatorioD > 0.21:
dias_de_renta = 2
elif aleatorioD > 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{}\t{}\t1\t{}\t{}\t-'.format(i, autoObj.nombre_auto,nAutos, str(aleatorioD)[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{}\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---------------------------")
I am trying to do it with more than one car available, I still have problems. Does anyone have any idea how to do it?