funcs.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import json
  2. import os
  3. def writeTrans(sender, recipient, amount):
  4. bcDir = os.curdir + '/Trans/'
  5. files = os.listdir(bcDir)
  6. files = sorted([int(i) for i in files])
  7. lastFile = files[-1]
  8. fileName = str(lastFile + 1)
  9. data = {'sender': sender,
  10. 'recipient': recipient,
  11. 'amount': amount
  12. }
  13. if sender == 'bank':
  14. score = 0
  15. with open('bank', 'r') as file:
  16. text = json.load(file)
  17. score = float(text["score"]) - amount
  18. with open('bank', 'w') as file:
  19. json.dump({'score': score}, file, indent=4)
  20. if recipient == 'bank':
  21. score = 0
  22. with open('bank', 'r') as file:
  23. text = json.load(file)
  24. score = float(text["score"]) + amount
  25. with open('bank', 'w') as file:
  26. json.dump({'score': score}, file, indent=4)
  27. if sender == 'agency':
  28. score = 0
  29. with open('agency', 'r') as file:
  30. text = json.load(file)
  31. score = float(text["score"]) - amount
  32. with open('agency', 'w') as file:
  33. json.dump({'score': score}, file, indent=4)
  34. if recipient == 'agency':
  35. score = 0
  36. with open('agency', 'r') as file:
  37. text = json.load(file)
  38. score = float(text["score"]) + amount
  39. with open('agency', 'w') as file:
  40. json.dump({'score': score}, file, indent=4)
  41. with open(bcDir + fileName, 'w') as file:
  42. json.dump(data, file, indent=4)
  43. def writeDriver(fullName, number, deadline, category, exp, numbersac, fines, premium, balance):
  44. bcDir = os.curdir + '/drivers/'
  45. files = os.listdir(bcDir)
  46. files = sorted([int(i) for i in files])
  47. lastFile = files[-1]
  48. fileName = str(lastFile + 1)
  49. data = {'fullName': fullName,
  50. 'cardNumber': number,
  51. 'cardDeadline': deadline,
  52. 'category': category,
  53. 'exp': exp,
  54. 'number of accidents': numbersac,
  55. 'fines': fines,
  56. 'premium': premium,
  57. 'balance': balance
  58. }
  59. with open(bcDir + fileName, 'w') as file:
  60. json.dump(data, file, indent=4)
  61. def checkBank():
  62. with open('bank', 'r') as file:
  63. text = json.load(file)
  64. score = float(text["score"])
  65. return score