main.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import misc_class
  2. import pickle
  3. def coin_test():
  4. my_coin = misc_class.Coin()
  5. print('Эта сторона обращена вверх:', my_coin.get_sideup())
  6. # Подбросить монету
  7. print('Сибираюсь подбросить монету 10 раз:')
  8. for _ in range(10):
  9. my_coin.toss()
  10. print(my_coin.get_sideup())
  11. def bankaccount_test():
  12. start_bal = 100.5
  13. savings = misc_class.BankAccaunt(start_bal)
  14. print(f'Остаток составляет ${savings.get_balance():,.2f}')
  15. savings.deposit(50.0)
  16. # Здесь будет вызван метод __str__
  17. print(savings)
  18. savings.withdraw(75.4)
  19. # Здесь тоже будет вызван метод __str__, но через str()
  20. print(str(savings))
  21. # Создает 4 объектов CellPhone и сохраняет из в списке
  22. def cellphone_test():
  23. phone_list = []
  24. for _ in range(5):
  25. phone = misc_class.CellPhone()
  26. phone_list.append(phone)
  27. for item in phone_list:
  28. print(item.get_manufact())
  29. print(item.get_model())
  30. print(item.get_retail_price())
  31. # Сериализация/десериализация объектов
  32. def cellphone_serial():
  33. FILE_NAME = 'cellphones.dat'
  34. file = open(FILE_NAME, 'wb')
  35. for _ in range(5):
  36. phone = misc_class.CellPhone()
  37. pickle.dump(phone, file)
  38. file.close()
  39. print(f'Данные записаны в {FILE_NAME}')
  40. file = open(FILE_NAME, 'rb')
  41. end_of_file = False
  42. while not end_of_file:
  43. try:
  44. phone = pickle.load(file)
  45. print(f'Производитель: {phone.get_manufact()}')
  46. print(f'Номер моделы: {phone.get_model()}')
  47. print(f'Розничная цена: {phone.get_retail_price()}')
  48. print()
  49. except Exception as e:
  50. end_of_file = True
  51. file.close()
  52. def main():
  53. #
  54. # coin_test()
  55. # Методы __str__
  56. # bankaccount_test()
  57. # Создание списка объектов CellPhone
  58. # cellphone_test()
  59. # Сериализация объектов
  60. # cellphone_serial()
  61. pass
  62. if __name__ == '__main__':
  63. main()