Untitled
unknown
plain_text
2 years ago
3.8 kB
13
Indexable
import os
class Coffee:
def __init__(self, coffee_id, name, country_of_origin, price):
self.id = coffee_id
self.name = name
self.country_of_origin = country_of_origin
self.price = price
def __str__(self):
return f"{self.id},{self.name},{self.country_of_origin},{self.price}"
class CoffeeShop:
def __init__(self):
self.coffees = []
self.country_ranking = {}
def load_coffees_from_file(self, filename):
try:
with open(filename, 'r') as file:
for line in file:
print(line)
coffee_data = line.strip().split(',')
coffee = Coffee(*coffee_data)
self.coffees.append(coffee)
self.update_country_ranking(coffee.country_of_origin)
except FileNotFoundError:
pass # File not found, assume an empty list
def display_all_coffees(self):
for coffee in self.coffees:
print(coffee.__str__())
def display_coffees_by_country(self, country):
coffees_in_country = [coffee for coffee in self.coffees if coffee.country_of_origin == country]
if coffees_in_country:
for coffee in coffees_in_country:
print(coffee.__str__())
else:
print(f"No coffees found from {country}")
def add_coffee(self, coffee):
if not (3 <= len(coffee.name) <= 30 and coffee.name[0].isupper() and coffee.price > 0):
print("Invalid coffee data. Please check the requirements.")
return
if any(c.id == coffee.id or c.name == coffee.name for c in self.coffees):
print("Error: Coffee id or name already exists.")
return
self.coffees.append(coffee)
self.update_country_ranking(coffee.country_of_origin)
self.save_coffees_to_file("coffees.txt")
if self.check_country_ranking_change(coffee.country_of_origin):
print(f"New winner: {coffee.country_of_origin}")
def update_country_ranking(self, country):
self.country_ranking[country] = self.country_ranking.get(country, 0) + 1
def check_country_ranking_change(self, new_country):
sorted_ranking = sorted(self.country_ranking.items(), key=lambda x: x[1], reverse=True)
return sorted_ranking[0][0] == new_country
def save_coffees_to_file(self, filename):
with open(filename, 'w') as file:
for coffee in self.coffees:
print(coffee)
file.write(coffee.__str__() + '\n')
def main():
coffee_shop = CoffeeShop()
coffee_shop.load_coffees_from_file("coffees.txt")
while True:
print("\nMenu:")
print("1. Display all coffees")
print("2. Display coffees by country")
print("3. Add coffee")
print("4. Exit")
choice = input("Enter your choice (1-4): ")
if choice == '1':
coffee_shop.display_all_coffees()
elif choice == '2':
country = input("Enter country name: ")
coffee_shop.display_coffees_by_country(country)
elif choice == '3':
coffee_id = int(input("Enter coffee id: "))
name = input("Enter coffee name: ")
country = input("Enter country of origin: ")
price = float(input("Enter coffee price: "))
new_coffee = Coffee(coffee_id, name, country, price)
coffee_shop.add_coffee(new_coffee)
elif choice == '4':
print("Exiting the coffee shop application. Goodbye!")
break
else:
print("Invalid choice. Please enter a number between 1 and 4.")
if __name__ == "__main__":
main()
Editor is loading...
Leave a Comment