Professional Documents
Culture Documents
Лабораторна робота №6 Програмування Мохонько Дмитро
Лабораторна робота №6 Програмування Мохонько Дмитро
І сам код:
class Smartphone:
def __init__(self, name, release_year, price, company):
self.name = name
self.release_year = release_year
self.price = price
self.company = company
class Store:
def __init__(self):
self.smartphones = []
def add_smartphone(self, name, release_year, price, company):
smartphone = Smartphone(name, release_year, price, company)
self.smartphones.append(smartphone)
def read_data_from_file(filename):
store = Store()
with open(filename, 'r') as smartphone_file:
for line in smartphone_file.readlines():
name, release_year, price, company = line.split(',')
store.add_smartphone(name, int(release_year), float(price),
company)
return store
def sort_smartphones_by_choice():
valid_sort_methods = {
1: "За роком випуску (старші)",
2: "За роком випуску (молдодші)",
3: "За ціною (дорожче)",
4: "За ціною (дешевше)",
"q": "Вихід",
}
while True:
try:
user_choice = input("Виберіть метод сортування (введіть
номер): ")
if user_choice == "q":
break
user_choice = int(user_choice)
if user_choice in valid_sort_methods:
chose_type_of_sort(user_choice)
else:
print("Виберіть дійсний номер методу сортування.")
except ValueError:
print("Введіть дійсний номер методу сортування.")
def chose_type_of_sort(sort_method):
while True:
if sort_method == 1:
a = input("Введіть рік для порівняння:")
if a.isdigit():
my_store.list_smartphones_older_than(int(a))
return
elif a == "q":
return
else:
print("Введіть коректні данні")
elif sort_method == 2:
a = input("Введіть рік для порівняння:")
if a.isdigit():
my_store.list_smartphones_younger_than(int(a))
break
elif a == "q":
return
else:
print("Введіть коректні данні")
elif sort_method == 3:
a = input("Введіть ціну для порівняння:")
if a.isdigit():
my_store.list_smartphones_cheaper_than(int(a))
break
elif a == "q":
return
else:
print("Введіть коректні данні")
elif sort_method == 4:
a = input("Введіть ціну для порівняння:")
if a.isdigit():
my_store.list_smartphones_pricier_than(int(a))
break
elif a == "q":
return
else:
print("Введіть коректні данні")
my_store = read_data_from_file('phone.txt')
current_year = 2023
sort_smartphones_by_choice()