Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #404

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
66c1d22
py-shop-trip(1)
ostboiko Oct 4, 2023
2845fba
py-shop-trip
ostboiko Oct 5, 2023
ee880d7
py-shop-trip(1)
ostboiko Oct 7, 2023
58f736c
py-shop-trip(2)
ostboiko Oct 8, 2023
799c93c
fixed_file
ostboiko Oct 8, 2023
8286c63
fixed_file((car.py) and (customer.py))
ostboiko Oct 9, 2023
397e1d3
fixed_file((car.py) and (customer.py))
ostboiko Oct 11, 2023
66da35b
fixed_file((car.py) and (customer.py))
ostboiko Oct 11, 2023
c9fc16e
fixed_file((car.py) and (customer.py))
ostboiko Oct 11, 2023
2f1a854
py-shop-trip
ostboiko Oct 12, 2023
951193f
py-shop-trip(2)
ostboiko Oct 15, 2023
bdb77d1
py-shop-trip(3)
ostboiko Oct 18, 2023
8a86975
py-shop-trip
ostboiko Oct 30, 2023
780f88b
py-shop-trip
ostboiko Nov 2, 2023
9137b69
py-shop-trip
ostboiko Nov 3, 2023
4467b99
py-shop-trip(not fixed)
ostboiko Nov 4, 2023
6eae346
py-shop-trip(Endlich)
ostboiko Nov 5, 2023
8d421d0
py-shop-trip(Endlich)
ostboiko Nov 6, 2023
cefadac
py-shop-trip(1)
ostboiko Nov 8, 2023
a00f990
py-shop-trip(1 fixed)
ostboiko Nov 8, 2023
04df98b
py-shop-trip(finish())
ostboiko Nov 11, 2023
a1c3144
py-shop-trip(finish(1))
ostboiko Nov 11, 2023
ce7d06f
py-shop-trip(finish(2))
ostboiko Nov 11, 2023
ae1ddc5
py-shop-trip(finish(2))
ostboiko Nov 11, 2023
e627836
py-shop-trip(finish(fixed))
ostboiko Nov 11, 2023
606eb10
py-shop-trip(finish(fixed1))
ostboiko Nov 11, 2023
51c5e44
py-shop-trip(finish(fixed 1))
ostboiko Nov 11, 2023
fb41d1f
py-shop-trip(finish(fixed 2))
ostboiko Nov 11, 2023
573b1cf
py-shop-trip(finish(fixed 2))
ostboiko Nov 11, 2023
df6839a
py-shop-trip(finish(fixed 3))
ostboiko Nov 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions README.md

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file was not fixed

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restore changes on it

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please restore this file!

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

- Read [the guideline](https://github.com/mate-academy/py-task-guideline/blob/main/README.md) before starting.

You want to create an application that helps customers to choose the cheapest


#You want to create an application that helps customers to choose the cheapest

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you change this?

trip for the products.

There is `config.json` file that contains:
Expand Down Expand Up @@ -141,6 +143,3 @@ You design application architecture by yourself, but there are some rules:

Distance between customer and shop is a distance between their locations in km.
Round printed value to two decimal places.


### Note: Check your code using this [checklist](checklist.md) before pushing your solution.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restore this code

Empty file added app/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions app/car.py

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file is still not fixed. Try to recreate the file and rewrite the code.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File not fixed

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Car:
def __init__(
self,
brand: str,
fuel_consumption: float,
) -> None:
self.brand = brand
self.fuel_consumption = fuel_consumption

def calculate_trip_cost(
self,
distance: int,
fuel_price: float
) -> float:
cost_trip = (distance * (self.fuel_consumption / 100)) * fuel_price
return cost_trip
40 changes: 40 additions & 0 deletions app/castomer.py

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename this file into customer.py

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just rename this file.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from typing import Dict, Any
from app.shop import Shop
from app.car import Car


class Customer:
def __init__(
self,
info: Dict[str, Any],
fuel_price: float
) -> None:
self.name = info["name"]
self.product_cart = info["product_cart"]
self.location = info["location"]
self.home_location = info["location"]
self.money = info["money"]
self.car = Car(info.get("car", {}), fuel_price)

def calculate_trip_cost(
self,
fuel_price: float,
shop: Shop
) -> float:
x1, y1 = self.location
x2, y2 = shop.location
distance = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
fuel_cost = (distance / 100) * \
self.car.fuel_consumption * fuel_price * 2
products_cost = self.calculate_products_cost(shop)
total = round(products_cost + fuel_cost, 2)
return total

def calculate_products_cost(
self,
shop: Shop,
) -> float | int:
products_cost = sum(amount * shop.products[product] for
product, amount in self.product_cart.items()
if product in shop.products)
return round(products_cost, 2)
37 changes: 37 additions & 0 deletions app/customer.py

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file is still not fixed. Try to recreate the file and rewrite the code.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File not fixed

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image File broken

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this file BTW

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this file.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from typing import Dict, List
from app.car import Car
from app.shop import Shop


class Customer:
def __init__(
self,
name: str,
product_cart: List,
location: List,
car: Car,
money: int,
) -> None:
self.name = name
self.product_cart = product_cart
self.location = location
self.car = car
self.money = money
self.total_cost = None

def calculate_trip_cost(self, shop: Dict, fuel_price: float) -> float:
distance_to_shop = self.calculate_distance(shop.location)
fuel_cost = self.car.calculate_trip_cost(distance_to_shop, fuel_price)
shopping_cost = shop.shopping_cost(self.product_cart)
total_cost = fuel_cost + shopping_cost + fuel_cost
return round(total_cost, 2)

def calculate_distance(self, shop_location: List) -> float:
x1, y1 = self.location
x2, y2 = shop_location
distance = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
return distance

def calculate_money(self, shop: Shop) -> bool:
shopping_cost = shop.shopping_cost(self.product_cart)
return self.money >= shopping_cost
72 changes: 69 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,69 @@
def shop_trip():
# write your code here
pass
import json
import datetime
from app.customer import Customer
from app.car import Car
from app.shop import Shop


def shop_trip() -> None:
global shop
with open("app/config.json", "r") as config_file:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need this line?

config_data = json.load(config_file)

fuel_price = config_data["FUEL_PRICE"]
customers_data = config_data["customers"]
shops_data = config_data["shops"]

customers = []
cars = []
shops = []

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This list is redundant no need to store cars

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry can't delete it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Знімок екрана 2023-11-11 о 17 52 44


for customer_data in customers_data:
car_data = customer_data["car"]
car = Car(car_data["brand"], car_data["fuel_consumption"])
cars.append(car)

customer = Customer(
customer_data["name"],
customer_data["product_cart"],
customer_data["location"],
car,
customer_data["money"],
)
customers.append(customer)

for shop_data in shops_data:
shop = Shop(
shop_data["name"],
shop_data["location"],
shop_data["products"],
fuel_price
)
shops.append(shop)

for customer in customers:
print(f"{customer.name} has {customer.money} dollars")
cheapest_shop = None
cheapest_cost = float("inf")

for shop in shops:
trip_cost = customer.calculate_trip_cost(shop, fuel_price)
print(f"{customer.name}'s trip to the {shop.name}"
f" costs {trip_cost}")
if customer.calculate_money(shop):
if trip_cost < cheapest_cost:
cheapest_cost = trip_cost
cheapest_shop = shop

if cheapest_shop is not None:
print(f"{customer.name} rides to "
f"{cheapest_shop.name}\n")
current_time = datetime.datetime.\
now().strftime("%d/%m/%Y %H:%M:%S")
receipt = cheapest_shop.generate_receipt(customer, current_time)
print(receipt)
last_money = customer.money - cheapest_cost
print(f"{customer.name} now has {last_money} dollars\n")
else:
print(f"{customer.name} doesn't have enough money "
f"to make a purchase in any shop")
44 changes: 44 additions & 0 deletions app/shop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from typing import Dict, List


class Shop:
def __init__(
self,
name: str,
location: Dict,
product_cart: List,
fuel_price: float
) -> None:
self.name = name
self.location = location
self.product_cart = product_cart
self.fuel_price = fuel_price

def shopping_cost(self, product_cart: List) -> float:
total_cost = 0
for product in product_cart.keys():
total_cost += self.product_cart[product] * product_cart[product]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use sum() function
Also, use .items instead of .keys()

return round(total_cost, 2)

def generate_receipt(self, customer: str, current_time: str) -> str:
receipt = f"Date: {current_time}\nThanks, {customer.name}," \
f" for your purchase!\nYou have bought: \n"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
f" for your purchase!\nYou have bought: \n"
f" for your purchase!\nYou have bought:\n"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Знімок екрана 2023-11-11 о 13 48 03

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried your option, but then the test crashes, doesn't want to, a space is needed there

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the tests are fixed on git already. You have old version, so just remove the space here, please.

total_cost = 0
for product, quantity in customer.product_cart.items():
if product in self.product_cart:
product_cost = self.product_cart[product]
product_total_cost = product_cost * quantity
if product_total_cost % 1 == 0:
receipt += (
f"{quantity} {product}s for "
f"{int(product_total_cost)} dollars\n"
)
else:
receipt += (
f"{quantity} {product}s for "
f"{product_total_cost} dollars"
)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if product_total_cost % 1 == 0:
receipt += (
f"{quantity} {product}s for "
f"{int(product_total_cost)} dollars\n"
)
else:
receipt += (
f"{quantity} {product}s for "
f"{product_total_cost} dollars"
)
if product_total_cost.is_integer():
product_total_cost = int(product_total_cost)
receipt += (
f"{quantity} {product}s for "
f"{product_total_cost} dollars"
)

total_cost += product_total_cost
receipt += f"\nTotal cost is {total_cost} dollars\nSee " \
f"you again!\n\n{customer.name} rides home"
return receipt
Copy link

@EdAlekseiev EdAlekseiev Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def generate_receipt(self, customer: str, current_time: str) -> str:
receipt = f"Date: {current_time}\nThanks, {customer.name}," \
f" for your purchase!\nYou have bought: \n"
total_cost = 0
for product, quantity in customer.product_cart.items():
if product in self.product_cart:
product_cost = self.product_cart[product]
product_total_cost = product_cost * quantity
if product_total_cost % 1 == 0:
receipt += (
f"{quantity} {product}s for "
f"{int(product_total_cost)} dollars\n"
)
else:
receipt += (
f"{quantity} {product}s for "
f"{product_total_cost} dollars"
)
total_cost += product_total_cost
receipt += f"\nTotal cost is {total_cost} dollars\nSee " \
f"you again!\n\n{customer.name} rides home"
return receipt
def generate_receipt(self, customer: str, current_time: str, fuel_price: float) -> str:
receipt = f"Date: {current_time}\nThanks, {customer.name}," \
f" for your purchase!\nYou have bought: \n"
total_cost = 0
for product, quantity in customer.product_cart.items():
if product in self.product_cart:
product_cost = self.product_cart[product]
product_total_cost = product_cost * quantity
if product_total_cost % 1 == 0:
receipt += (
f"{quantity} {product}s for "
f"{int(product_total_cost)} dollars\n"
)
else:
receipt += (
f"{quantity} {product}s for "
f"{product_total_cost} dollars"
)
total_cost = customer.calculate_trip_cost(self, fuel_price)
receipt += f"\nTotal cost is {total_cost} dollars\nSee " \
f"you again!\n\n{customer.name} rides home"
return receipt

40 changes: 40 additions & 0 deletions castomer.py

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you also need duplicate of file?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this file.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete this file, please

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from typing import Dict, Any
from app.shop import Shop
from app.car import Car


class Customer:
def __init__(
self,
info: Dict[str, Any],
fuel_price: float
) -> None:
self.name = info["name"]
self.product_cart = info["product_cart"]
self.location = info["location"]
self.home_location = info["location"]
self.money = info["money"]
self.car = Car(info.get("car", {}), fuel_price)

def calculate_trip_cost(
self,
fuel_price: float,
shop: Shop
) -> int:
x1, y1 = self.location
x2, y2 = shop.location
distance = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
fuel_cost = (distance / 100) * \
self.car.fuel_consumption * fuel_price * 2
products_cost = self.calculate_products_cost(shop)
total = round(products_cost + fuel_cost, 2)
return total

def calculate_products_cost(
self,
shop: Shop,
) -> float | int:
products_cost = sum(amount * shop.products[product] for
product, amount in self.product_cart.items()
if product in shop.products)
return round(products_cost, 2)
Empty file added cer.py

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this file

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this file.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete this file, pleaase

Empty file.
Loading