From b8727deca195ddc71343831c9e9e17610e54a195 Mon Sep 17 00:00:00 2001 From: Yarne Coppens Date: Sat, 24 Aug 2024 13:07:15 +0200 Subject: [PATCH] Added endpoint to convert price to cash --- src/classes/cash_classes.py | 12 ++++++++++++ src/main.py | 11 +++++++++-- src/modules/price_to_cash_calculator.py | 25 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/classes/cash_classes.py create mode 100644 src/modules/price_to_cash_calculator.py diff --git a/src/classes/cash_classes.py b/src/classes/cash_classes.py new file mode 100644 index 0000000..8c2a70f --- /dev/null +++ b/src/classes/cash_classes.py @@ -0,0 +1,12 @@ +from pydantic import BaseModel + +class AmountOfBills(BaseModel): + one_euro: int + two_euro: int + five_euro: int + ten_euro: int + twenty_euro: int + fifty_euro: int + hundred_euro: int + twohundred_euro: int + fivehundred_euro: int \ No newline at end of file diff --git a/src/main.py b/src/main.py index 61230f6..c1b0568 100644 --- a/src/main.py +++ b/src/main.py @@ -3,8 +3,9 @@ from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import FileResponse import os -from src.classes import product_classes +from src.classes import product_classes, cash_classes from src.config import definitions +from src.modules import price_to_cash_calculator from shop_validators import image_validator from src import data_connection @@ -41,4 +42,10 @@ def get_icon(icon_filename: str): assert os.path.exists(full_filepath), f"File {full_filepath} not found" assert image_validator.is_valid_svg(file_name=full_filepath), f"File {full_filepath} is not a valid image" - return full_filepath \ No newline at end of file + return full_filepath + +@app.get("/price_to_cash/{price}", response_model=cash_classes.AmountOfBills) +def price_to_cash(price: int): + cash_model = price_to_cash_calculator.price_to_cash_model(price) + + return cash_model \ No newline at end of file diff --git a/src/modules/price_to_cash_calculator.py b/src/modules/price_to_cash_calculator.py new file mode 100644 index 0000000..465e781 --- /dev/null +++ b/src/modules/price_to_cash_calculator.py @@ -0,0 +1,25 @@ +from src.classes import cash_classes +import math + +def price_to_cash_model(price: int) -> cash_classes.AmountOfBills: + cash_types = [500,200,100,50,20,10,5,2,1] + + cash_model = {} + + for cash_type in cash_types: + cash_fits_in_price = math.floor(price / cash_type) + print(cash_type, price, cash_fits_in_price) + cash_model[str(cash_type)] = cash_fits_in_price + price -= cash_type * cash_fits_in_price + + return cash_classes.AmountOfBills( + one_euro=cash_model['1'], + two_euro=cash_model['2'], + five_euro=cash_model['5'], + ten_euro=cash_model['10'], + twenty_euro=cash_model['20'], + fifty_euro=cash_model['50'], + hundred_euro=cash_model['100'], + twohundred_euro=cash_model['200'], + fivehundred_euro=cash_model['500'] + ) \ No newline at end of file