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 all 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
16 changes: 10 additions & 6 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 @@ -131,16 +131,20 @@ You design application architecture by yourself, but there are some rules:
<summary>
Hint: modules structure example if you have problems with architecture.
</summary>
py-shop-trip/<br>
|-- car.py<br>
|-- customer.py<br>
|-- main.py<br>
└-- shop.py<br>

```
└── py-shop-trip
└── app
├── car.py
├── customer.py
├── main.py
└── shop.py
```
</details>


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.
### Note: Check your code using this [checklist](checklist.md) before pushing your solution.
Empty file added app/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions app/car.py
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
37 changes: 37 additions & 0 deletions app/customer.py
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
70 changes: 67 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,67 @@
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() -> str:
with open("app/config.json", "r") as config_file:
config_data = json.load(config_file)

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

customers = []
shops = []

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

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, fuel_price)
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 = sum(self.product_cart[product] * product_cart[product]
for product in product_cart.keys())
return round(total_cost, 2)

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:"
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 = float(product_cost * quantity)
if product_total_cost.is_integer():
product_total_cost = int(product_total_cost)
receipt += (
f"\n{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
Loading