-
Notifications
You must be signed in to change notification settings - Fork 725
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
base: master
Are you sure you want to change the base?
Develop #404
Changes from 20 commits
66c1d22
2845fba
ee880d7
58f736c
799c93c
8286c63
397e1d3
66da35b
c9fc16e
2f1a854
951193f
bdb77d1
8a86975
780f88b
9137b69
4467b99
6eae346
8d421d0
cefadac
a00f990
04df98b
a1c3144
ce7d06f
ae1ddc5
e627836
606eb10
51c5e44
fb41d1f
573b1cf
df6839a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Restore this code |
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename this file into customer.py There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. File not fixed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this file BTW There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This list is redundant no need to store cars There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry can't delete it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
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") |
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] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use sum() function |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why you also need duplicate of file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this file. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Delete this file, pleaase |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restore changes on it