停车场收费系统
这个是学习的一个作业!!! 学习小白在线学习!!!
文章目录
- 停车场收费系统
- 项目需求
- 源码1.0(逻辑写法没眼看)
- 源码2.0(稍微看的舒服点了)
- 源码(偷师老师的技术)
- 总结
项目需求
编写程序制作一个停车场的收费系统,程序至少包括:添加月租车、删除月租车,查询、收费,把相关数据保存在文件里面,程序中数据建议使用字典处理。
收费标准:4元/小时,2元/半小时,不足30分钟,按半小时计算,24小时最高50元(这里需计算50元对应的最大时长);
如果有优惠券,两小时内免费;月租卡300元/月(没有意义的数据,因为系统不收月租钱),没有按月缴费的月租卡,超过一天不收费,超过2天按5折收费,超过3天按小时收费;如果车牌号是“粤A888**”,永久免费,如果是车牌号是“粤A88**”,停车费7折。
提示:以下是本篇文章正文内容,下面案例可供参考
源码1.0(逻辑写法没眼看)
仅供参考
代码如下(示例):
import os
filename = 'information.txt'
def menm():
print('===========================停车场收费系统=================================')
print('----------------------------功能菜单----------------------------------------')
print('\t\t\t\t\t\t0.退出系统')
print('\t\t\t\t\t\t1.添加月租车')
print('\t\t\t\t\t\t2.删除月租车')
print('\t\t\t\t\t\t3.查询')
print('\t\t\t\t\t\t4.收费')
print('----------------------------------------------------------------------------')
def insert():
car_list = []
while True:
id = input('请输入车牌号:\n')
if not id:
break
time = input('请输入停车时间(小时)\n')
if not time:
break
card = input('请输入是否有月租卡(是/否)\n')
if not card:
break
yhq = input('请输入是否有优惠券(是/否)\n')
if not yhq:
break
car = {'id': id, 'time': time, 'card': card, 'yhq': yhq}
car_list.append(car)
answer = input('是否继续添加?y/n\n')
if answer == 'y':
continue
else:
break
save(car_list)
print('车辆信息录入完毕!!!')
def save(list):
try:
car_txt = open(filename, 'a', encoding='utf-8') # a是有的时候追加,没有就创建
except:
car_txt = open(filename, 'w', encoding='utf-8') # w是以写入的方式打开
for item in list:
car_txt.write(str(item) + '\n') # \n换行
car_txt.close()
def delete():
while True:
car_id = input('请输入要删除的车牌号')
if car_id != '':
if os.path.exists(filename): # 判断磁盘上的文件是否存在
with open(filename, 'r', encoding='utf-8') as file: # 模式是r,编码格式是utf-8
car_old = file.readlines() # 读取文件
else: # 信息不存在
car_old = [] # 定义一个列表为空
flag = False # 标记是否删除(默认没有删除)
if car_old: # 判断列表(删除完的将原有的进行覆盖)空列表布尔值为False
with open(filename, 'w', encoding='utf-8') as wfile: # w:以只写模式打开文件,如果文件存在,则覆盖原有内容
d = {} # 定义空字典
for item in car_old: # 遍历一遍
d = dict(eval(item)) # eval()函数将字符串转成字典
if d['id'] != car_id: # 判断删除的车辆在字典中存不存在
wfile.write(str(d) + '\n') # 如果不存在,将学生信息先写入文件(这里写入的是原有的信息)
else: # ID相等的情况下
flag = True # 标记已删除
if flag: # 判断flag的值
print(f'id为{car_id}的车辆信息已被删除') # 格式化字符串f
else:
print(f'没有找到id为{car_id}的车辆信息')
else: # 不存在车辆信息
print('无车辆信息')
break
# show() # 删除之后要重新显示所有学生信息
answer = input('是否继续删除?y/n\n')
if answer == 'y':
continue
else:
break
pass
def search(): # 是一个查询过程
car_query = [] # 定义一个列表,防止同名
while True:
id = input('请输入查询的 车牌号')
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as rfile:
car = rfile.readlines()
for item in car:
d = dict(eval(item))
if id != '':
if d['id'] == id:
car_query.append(d)
# 显示查询结果
show_car(car_query)
# 情况列表
car_query.clear()
answer = input('是否要继续查询?y/n\n')
if answer == 'y':
continue
else:
break
else:
print('暂未保存车辆信息')
return
pass
def show_car(lst):
if len(lst) == 0:
print("没有查询到车辆信息,无法显示!")
return
format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}'
print(format_title.format('车牌号', '停车时间(/小时)', '有无月租卡', '有无优惠券', '停车费用'))
format_data = '{:^6}\t\t{:^14}\t\t{:^10}\t\t{:^14}\t\t'
for item in lst:
print(
format_data.format(
item.get('id'),
item.get('time'),
item.get('card'),
item.get('yhq'),
), end=' ')
total(str(item.get('id')),eval(item.get('time')), str(item.get('card')), str(item.get('yhq')))
def show_shoufei(lst2):
if len(lst2) == 0:
print("没有查询到车辆信息,无法显示!")
return
format_title = '{:^6}\t{:^12}'
print(format_title.format('车牌号', '停车费用'))
format_data = '{:^6}\t\t'
for item2 in lst2:
print(
format_data.format(
item2.get('id'),
), end=' ')
total(str(item2.get('id')),eval(item2.get('time')), str(item2.get('card')), str(item2.get('yhq')))
def total(id,num, str1, str2):
if id[0:5]=='粤A888':
print(0)
elif id[0:4] == '粤A88':
if str2 == '否':
int1 = num // 0.5
int2 = num % 0.5
if int1 >= 48:
print(50*0.7)
else:
if int2 > 0:
int1 += 1
print((int1 * 2)*0.7)
else:
print((int1 * 2)*0.7)
else:
num = num - 2
int1 = num // 0.5
int2 = num % 0.5
if int1 >= 48:
print(50)
else:
if int2 > 0:
int1 += 1
print(int1 * 2)
else:
print(int1 * 2)
else:
if str1 == '否':
if str2 == '否':
int1 = num // 0.5
int2 = num % 0.5
if int1 >= 48:
print(50)
else:
if int2 > 0:
int1 += 1
print(int1 * 2)
else:
print(int1 * 2)
else:
num = num - 2
int1 = num // 0.5
int2 = num % 0.5
if int1 >= 48:
print(50)
else:
if int2 > 0:
int1 += 1
print(int1 * 2)
else:
print(int1 * 2)
else:
if num <= 24:
print(0)
elif 24 < num <= 48:
int1 = num // 0.5
int2 = num % 0.5
if int2 > 0:
int1 += 1
print((int1 * 2) * 0.5)
else:
print((int1 * 2) * 0.5)
else:
int1 = num // 0.5
int2 = num % 0.5
if int2 > 0:
int1 += 1
print((int1 * 2) * 0.5)
else:
print((int1 * 2) * 0.5)
def shoufei():
car_query2 = [] # 定义一个列表,防止同名
while True:
id = input('请输入查询的 车牌号')
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as rfile:
car2 = rfile.readlines()
for item in car2:
d = dict(eval(item))
if id != '':
if d['id'] == id:
car_query2.append(d)
# 显示查询结果
show_shoufei(car_query2)
# 情况列表
car_query2.clear()
answer = input('是否要继续查询?y/n\n')
if answer == 'y':
continue
else:
break
else:
print('暂未保存车辆信息')
return
pass
def main():
while True:
menm()
try:
choice = int(input('请选择\n'))
if choice in [0, 1, 2, 3, 4]:
if choice == 0:
answer = input('您确定要退出系统吗?y/n\n')
if answer == 'y' or answer == 'Y':
print('谢谢您的使用!!!')
break # 退出系统
else:
continue
elif choice == 1:
insert()
elif choice == 2:
delete()
elif choice == 3:
search()
elif choice == 4:
shoufei()
except:
print("请输入有效数字")
main()
if __name__ == '__main__':
main()
源码2.0(稍微看的舒服点了)
提示:这里对文章进行计算逻辑修改:
import os
filename = 'hw.txt'
def menm():
print('===========================停车场收费系统=================================')
print('----------------------------功能菜单----------------------------------------')
print('\t\t\t\t\t\t0.退出系统')
print('\t\t\t\t\t\t1.添加月租车')
print('\t\t\t\t\t\t2.删除月租车')
print('\t\t\t\t\t\t3.查询')
print('\t\t\t\t\t\t4.收费')
print('----------------------------------------------------------------------------')
def insert():
car_list = []
while True:
id = input('请输入车牌号:\n')
if not id:
break
time = input('请输入停车时间(小时)\n')
if not time:
break
card = input('请输入是否有月租卡(是/否)\n')
if not card:
break
yhq = input('请输入是否有优惠券(是/否)\n')
if not yhq:
break
car = {'id': id, 'time': time, 'card': card, 'yhq': yhq}
car_list.append(car)
answer = input('是否继续添加?y/n\n')
if answer == 'y' or answer == 'Y':
continue
else:
break
save(car_list)
print('车辆信息录入完毕!!!')
def save(list):
try:
car_txt = open(filename, 'a', encoding='utf-8') # a是有的时候追加,没有就创建
except:
car_txt = open(filename, 'w', encoding='utf-8') # w是以写入的方式打开
for item in list:
car_txt.write(str(item) + '\n') # \n换行
car_txt.close()
def delete():
while True:
car_id = input('请输入要删除的车牌号')
if car_id != '':
if os.path.exists(filename): # 判断磁盘上的文件是否存在
with open(filename, 'r', encoding='utf-8') as file: # 模式是r,编码格式是utf-8
car_old = file.readlines() # 读取文件
else: # 信息不存在
car_old = [] # 定义一个列表为空
flag = False # 标记是否删除(默认没有删除)
if car_old: # 判断列表(删除完的将原有的进行覆盖)空列表布尔值为False
with open(filename, 'w', encoding='utf-8') as wfile: # w:以只写模式打开文件,如果文件存在,则覆盖原有内容
d = {} # 定义空字典
for item in car_old: # 遍历一遍
d = dict(eval(item)) # eval()函数将字符串转成字典
if d['id'] != car_id: # 判断删除的车辆在字典中存不存在
wfile.write(str(d) + '\n') # 如果不存在,将车辆信息先写入文件(这里写入的是原有的信息)
else: # ID相等的情况下
flag = True # 标记已删除
if flag: # 判断flag的值
print(f'id为{car_id}的车辆信息已被删除') # 格式化字符串f
else:
print(f'没有找到id为{car_id}的车辆信息')
else: # 不存在车辆信息
print('无车辆信息')
break
# show() # 删除之后要重新显示所有学生信息
answer = input('是否继续删除?y/n\n')
if answer == 'y' or answer == 'Y':
continue
else:
break
pass
def search(): # 是一个查询过程
car_query = [] # 定义一个列表,防止同名
while True:
id = input('请输入查询的 车牌号')
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as rfile:
car = rfile.readlines()
for item in car:
d = dict(eval(item))
if id != '':
if d['id'] == id:
car_query.append(d)
# 显示查询结果
show_car(car_query)
# 情况列表
car_query.clear()
answer = input('是否要继续查询?y/n\n')
if answer == 'y':
continue
else:
break
else:
print('暂未保存车辆信息')
return
pass
def show_car(lst):
if len(lst) == 0:
print("没有查询到车辆信息,无法显示!")
return
format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}'
print(format_title.format('车牌号', '停车时间(/小时)', '是/否有月租卡', '是/否有优惠券'))
format_data = '{:^6}\t\t{:^14}\t\t{:^10}\t\t{:^14}'
for item in lst:
print(
format_data.format(
item.get('id'),
item.get('time'),
item.get('card'),
item.get('yhq'),
))
# 计算一天的价格
def oneday(timenum):
# 计算一天的最大时间有没有超过
if timenum > 50 / 4: # 50 是一天最大停车收费50元
re = 50
return re # 一天收费值
else:
timeint = timenum / 1
# 不足半小时按半小时计算
if timeint % 0.5 != 0:
timeint += 0.5
re = timeint * 4
return re
def total(id, num, str1, str2):
day = num // 24 # 停了整数多少天
result = day * 50 + oneday(num - day * 24)
if str1 == '是': # 有无优惠券
if day == 1:
result1 = 0 # result1,result2,result3 都是每种条件计算后的值,最后再返回给result
elif day == 2:
result1 = result * 0.5
else:
result1 = result
result = result1
if str1 == '否':
if str2 == '是':
if num<2:
result2 = 0
else:
result2 = result
result = result2
if id[0:4] == '粤A88':
if id[0:5] == '粤A888':
result = 0
else:
result = result * 0.7
result = result
print(result)
def show(lst2):
if len(lst2) == 0:
print("没有查询到车辆信息,无法显示!")
return
format_title = '{:^6}\t{:^12}'
print(format_title.format('车牌号', '停车费用'))
format_data = '{:^6}\t\t'
for item2 in lst2:
print(
format_data.format(
item2.get('id'),
), end=' ')
total(str(item2.get('id')), eval(item2.get('time')), str(item2.get('card')), str(item2.get('yhq')))
def show_money():
car_query2 = [] # 定义一个列表,防止同名
while True:
id = input('请输入查询的 车牌号')
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as rfile:
car2 = rfile.readlines()
for item in car2:
d = dict(eval(item))
if id != '':
if d['id'] == id:
car_query2.append(d)
# 显示查询结果
show(car_query2)
# 情况列表
car_query2.clear()
answer = input('是否要继续查询?y/n\n')
if answer == 'y' or answer == 'Y':
continue
else:
break
else:
print('暂未保存车辆信息')
return
pass
def main():
while True:
menm()
try:
choice = int(input('请选择\n'))
if choice in [0, 1, 2, 3, 4]:
if choice == 0:
answer = input('您确定要退出系统吗?y/n\n')
if answer == 'y' or answer == 'Y':
print('谢谢您的使用!!!')
break # 退出系统
else:
continue
elif choice == 1:
insert()
elif choice == 2:
delete()
elif choice == 3:
search()
elif choice == 4:
show_money()
except:
print("请输入有效数字")
main()
if __name__ == '__main__':
main()
源码(偷师老师的技术)
提示:老师才是永远滴神,写的仅仅是逻辑:
'''
停车场收费问题。
收费标准:4元/小时,2元/半小时,不足30分钟,按半小时计算,24小时最高50元(这里需计算50元对应的最大时长);
如果有优惠券,两小时内免费;月租卡300元/月(没有意义的数据,因为系统不收月租钱),
没有按月缴费的月租卡,超过一天不收费,超过2天按5折收费,超过3天按小时收费;
如果车牌号是“粤A888**”,永久免费,如果是车牌号是“粤A88**”,停车费7折。
设置一个附加功能:即特定用户可以参与抽奖,中奖金额从停车费中减免,最高不超过停车费,中奖金额以随机数产生。
'''
import random
import math
yuezu = {'粤A543G1': 123, '粤A789G1': 5, '粤A456G1': -1, '粤A123G1': -2, '粤A123G2': -5,'粤A88454': -2,'粤A88888':5}
carId = ''
rate = 1
totalmoney = 0
price = 2
fullmoney = 50
stilltime = 0
daynum = 0
flag = 'y'
print('系统启动,祝您工作愉快!')
while flag not in ['n', 'N']:
rate = 1
carId = input('请输入车牌号: ').upper()
# print(carId)
yuqi = yuezu.get(carId, -3)
if (carId[0:5] == '粤A888') | (yuqi > -2):
rate = 0
elif yuqi == -2:
rate = 0.5
if carId[0:4] == '粤A88':
rate *= 0.7
if rate != 0:
stilltime = eval(input('请输入停车时长(分钟): '))
# print(stilltime)
daynum = int(stilltime / (24* 60))
# print('超时天数:' , daynum)
totalmoney = daynum * fullmoney + math.ceil(stilltime % (24 * 60) / 30) * price
totalmoney = float(totalmoney * rate)
# print('未抽奖金额' , totalmoney)
if input('用户是否有抽奖资格,请输入 Y/N ?') in ['Y', 'y']:
lott = random.randint(0, totalmoney)
print('恭喜您,抽中金额: ', lott)
totalmoney -= lott
print('请支付停车费: ', totalmoney)
input('单击任意键继续!')
print('祝您一路顺风! ')
print('')
flag = input('按回车继续!输入 N 退出系统。')
总结
过程中不断就校验算法逻辑的思路,看来平常还是得多多谢谢逻辑
此文章就是直接学习的一个记录
有发现问题求指出来