import json
from datetime import datetime, timedelta
from colorama import Fore, Style
parking = {
'entery_count':0
}
##=================================================
#اضاف کردن خودرو به پارکینگ
def add_parking():
parking['entery_count'] += 1
time_in = datetime.now().strftime("%H:%M")
number_plate = input("Enter the number plate: ")
alfa = input("Enter the alphabet: ")
if not number_plate .isdigit() or len(number_plate) !=7:
print (Fore.RED+'***length of number plate should be 7 and should contain only digits***'+ Style.RESET_ALL)
print ()
return
number_plate = (number_plate[3:] + alfa.upper() + number_plate[3:] + '--' + number_plate[-2:])
print(number_plate)
# time_out = input("Enter the time of exit: ")
status = input("Enter the status '1 for occupied, 0 for free:' ")
parking[number_plate] = {
'time_in': time_in,
# 'time_out': time_out,
'status': status
}
print(f'Car successfully added to the parking slot: {number_plate},\n time of entry: {time_in}')
with open('parking.json', 'w', encoding='utf-8')as f:
json.dump(parking, f, ensure_ascii=False, indent=4)
print("Parking slot added successfully")
#=================================================
#خروجی خودرو از پارکینگ
def out_parking():
number_plate = input("Enter the number plate: ")
time_out = datetime.now().strftime("%H:%M")
status = input("Enter the status '1 for occupied, 0 for free:' ")
try:
with open('parking.json', 'r', encoding='utf-8')as f:
parking = json.load(f)
except (FileNotFoundError, json.decoder.JSONDecodeError ):
parking = {}
#بررسی اینکه شماره پلاک وجود داشته باشه
if number_plate in parking:
parking[number_plate] ['time_out']= time_out
parking[number_plate] ['status']= status
else:
parking[number_plate] = {
'time_in':None,
'time_out' :time_out,
'status': status
}
with open('parking.json', 'w', encoding='utf-8')as f:
json.dump(parking, f, ensure_ascii=False, indent=4)
print("Parking slot added successfully")
#===============================================
#وضعیت خودرو
def status_parking():
number_plate = input("Enter the number plate: ")
try:
with open ('parking.json', 'r', encoding='utf-8')as f:
parking = json.load(f)
except FileNotFoundError:
print ('Parking file not found')
except json.decoder.JSONDecodeError:
print ('the parking.json file is not properly formatted')
return
#===========================================
#بررسی شماره پلاک وجود داشته باشه
if number_plate in parking:
status = parking[number_plate].get('status')
time_in =parking[number_plate].get('time_in')
time_out = parking[number_plate].get('time_out')
if time_out is None:
time_out = parking[number_plate].get('Not quite parked yet')
print (f'Parking slot number === {number_plate}=== is { 'occupied: True' if status == '1' else 'free'}')
else:
print (f'No parking slot found for number plate ===={number_plate}===')
#===========================================
#نمایش تعداد پارکینگ ها
def show_number_parking():
with open('parking.json', 'r', encoding='utf-8')as f:
parking = json.load(f)
for index,(key, value) in (enumerate(parking.items(),1)):
print (f'{index}: {key} --> {value}')
print(50*'=')
# print (f'Number of parking slots available: {parking['entery_count']}')
print ()
#============================================
#محاسبه هزینه پارک خودرو در پارکینک
def price_parking():
try:
with open ('parking.json', 'r', encoding='utf-8')as f:
parking = json.load(f)
except (FileNotFoundError, json.decoder.JSONDecodeError):
print ('Parking file not found')
return
price = 5000
print('Fixed entry 5000 Tomans:"(For more than 6 hours, an additional 1000 Tomans will be added for each additional hour.)"')
number_plate = input("Enter the number plate: ")
if number_plate in parking:
time_in =parking[number_plate]=['time_in']
time_out = parking[number_plate]=['time_out']
if time_out is None:
time_out = datetime.now().strftime("%H:%M")
time_diff = datetime.strptime(time_out, '%H:%M') - datetime.strptime(time_in, '%H:%M')
duration = time_diff.total_seconds() / 3600
if duration > 6:
price += (duration - 6) * 1000
print(f'The price for the parking slot {number_plate} is {price} Tomans')
else:
print(f'The parking slot {number_plate} is already occupied')
#==============================================
from colorama import Fore, Style, init
init()
def show_menu():
print(Fore.CYAN + "="*50)
print(Fore.YELLOW + "