Guest User

Untitled

a guest
Jan 22nd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. import json
  2.  
  3. class Database:
  4. def __init__(self, path_file):
  5. self.path_file=path_file
  6.  
  7. def write(self, data):
  8. with open(self.path_file, 'w') as f:
  9. json.dump(data, f)
  10.  
  11. def read(self):
  12. with open(self.path_file, 'r') as f:
  13. data = json.load(f)
  14. return data
  15.  
  16. class AddUser:
  17. def __init__(self, firs_name, last_name, phone_number, city):
  18. self.firs_name=firs_name
  19. self.last_name=last_name
  20. self.phone_number=phone_number
  21. self.city=city
  22. self.db = Database(path_file='file.json')
  23.  
  24. def lisl_user(self):
  25. return {'users':[]}
  26.  
  27. def create_user(self):
  28. return {"first name":self.firs_name,
  29. "last name":self.last_name,
  30. "phone number":self.phone_number,
  31. "city":self.city,
  32. "product":[]}
  33.  
  34. def save_user(self):
  35. data=self.db.read()
  36. data["users"].append(self.create_user())
  37. self.db.write(data)
  38.  
  39.  
  40.  
  41. class AddProduct:
  42. def __init__(self):
  43. self.db = Database(path_file='file.json')
  44.  
  45. def add_product(self):
  46. data = self.db.read()
  47. users=data['users']
  48. if data['users'] == []:
  49. print('sorry no user in database')
  50. exit(1)
  51. phone=int(input('number'))
  52. phone_book=[]
  53. total=[]
  54. total_cost=[]
  55. for user in users:
  56. phone_book.append(user['phone number'])
  57. if phone in phone_book:
  58. for price in user['product']:
  59. total.append(price['total'])
  60. total_cost.append(price['price'])
  61. total_price=[x * y for x, y in zip(total_cost, total)]
  62. print(sum(total_price))
  63. if sum(total_price) <= 100000:
  64. discount = int(input('price'))
  65.  
  66. else:
  67. print('you have -10proc')
  68. discount = int(input('price'))
  69. discount = discount - discount * 0.1
  70. user['product'].append({'type':input('type'),
  71. 'price':discount,
  72. 'total':int(input('total'))})
  73. self.db.write(data)
  74. break
  75. else:
  76. print('no user phone number', phone, 'first you have to create user')
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. add_product=AddProduct()
  84. add_product.add_product()
  85.  
  86.  
  87. #firs_name=input('input first name')
  88. #last_name=input('input last name')
  89. #phone_number=int(input('input phone number'))
  90. #city=input('input city')
  91. #
  92. #user = AddUser(firs_name=firs_name,
  93. # last_name=last_name,
  94. # phone_number=phone_number,
  95. # city=city)
  96. #
  97. #user.save_user()
Add Comment
Please, Sign In to add comment