-
Notifications
You must be signed in to change notification settings - Fork 14
Try maked OOP version #24
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
Open
Starowoitowa
wants to merge
4
commits into
Chudopal:main
Choose a base branch
from
Starowoitowa:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,117 +1,125 @@ | ||
| import json | ||
|
|
||
| from typing import List, Dict | ||
|
|
||
| SHOPPING_RESULT = [] | ||
| COAST_LIST= [] | ||
| DICT = {} | ||
| class With_File_Working(): | ||
|
|
||
| def __init__(self, path : str, name: str) -> None: | ||
| self._path = path | ||
| self.name = name | ||
|
|
||
| def read_file(adress, name) -> List: | ||
|
Owner
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. нужно передать параметр self, и через него получать _path, а не передавать его в функцию явно |
||
| with open(adress) as file: | ||
| data = json.load(file) | ||
| return data.get(name) | ||
|
|
||
| def user_menu() -> str: | ||
| def write_file(adress, data) -> None: | ||
|
Owner
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. тоже, прокинуть self и из него получать _path |
||
| with open(adress, "w") as file: | ||
| json.dump(data, file, indent=4) | ||
|
|
||
|
|
||
| def user_menu() -> str: | ||
| return ("Выберете действие: \n" + | ||
| "1. Посмотреть список туров \n" + | ||
| "2. Положить выбранный тур в корзину\n" + | ||
| "3. Проверить сумму товаров в корзине\n" + | ||
| "4. Проверить корзину\n" + | ||
| "5. Закончить работу\n" ) | ||
|
|
||
| def read_file(adress, name) -> List: | ||
| with open(adress) as file: | ||
| data = json.load(file) | ||
| return data.get(name) | ||
|
|
||
| def read_cart(adress) -> List: | ||
| with open(adress) as file: | ||
| data = json.load(file) | ||
| return data | ||
|
|
||
| def write_file(adress, data) -> None: | ||
| with open(adress, "w") as file: | ||
| json.dump(data, file) | ||
|
|
||
| def provide_data(data: List)-> Dict: | ||
| result = {} | ||
| for guide in data: | ||
| result[guide.get("id")] = guide.get("price") | ||
| return result | ||
| "4. Закончить работу\n" ) | ||
|
|
||
| def provide_file(data: Dict, item: str): | ||
| result = {"catalog":get_menu()} | ||
| items=[] | ||
| for guide_id, guide_price in data.items(): | ||
| items.append({"id": guide_id, "price":guide_price}) | ||
| result[item]=items | ||
| return result | ||
| def provide_data(data: List[Dict])-> Dict: | ||
| result = [] | ||
| for id in data: | ||
| id = list(id.values()) | ||
| result.append(id) | ||
| return result | ||
|
|
||
| def get_menu()-> List: | ||
| data = read_file("app\guide_offer.json", "catalog") | ||
| def provide_file(data): | ||
| return [ | ||
| { | ||
| "id": id, | ||
| "price": price | ||
| } | ||
| for id, price in data | ||
| ] | ||
|
|
||
| def get_whole_data() -> Dict[str, List]: | ||
| data = With_File_Working.read_file("Shops/travel_guide_store/app\guide_offer.json", "catalog") | ||
| return data | ||
|
|
||
| def get_menu()-> List: | ||
| data = get_whole_data() | ||
| return provide_data(data) | ||
|
|
||
| def take_guide()-> List: | ||
| data = read_file("app\shopping_cart.json", "cart") | ||
| return data | ||
|
|
||
| def format_catalog(guide_list: Dict)-> str: | ||
| return "\n".join([f"{guide_id} cost {guide} BYN" for guide_id, guide in guide_list.items()]) | ||
| def show_cart(): | ||
| data = get_whole_data() | ||
| return data.get("shopping_cart") | ||
|
|
||
| def format_cart(cart_list: Dict): | ||
| return "\n".join([f"Your choice guide: {guide_id} cost {guide} BYN and added in shopping_cart" for guide_id, guide in cart_list.items()]) | ||
| def format_catalog(catalog: any)-> str: | ||
| return "\n".join([f" Тур - {guide_id} - стоит {price} BYN" for guide_id, price in catalog]) | ||
|
|
||
| def add_cart(choice_id: str): | ||
| cart = get_menu() | ||
| part = format_catalog(cart) | ||
| def add_cart(catalog): | ||
| result = None | ||
| guide = input("Напишите выбранный тур: ") | ||
| for item in guide: | ||
| choice_id.append(str(guide)) | ||
| return "\n".join([f" Тур - {guide} - добавлен в Вашу корзину" for guide in choice_id]) | ||
| for item in catalog: | ||
| if item[0] == guide: | ||
| result = item | ||
| break | ||
| return result | ||
|
|
||
|
|
||
| def summ_guide(sum_list): | ||
| catalog = get_menu() | ||
| # coast_list= [] | ||
| for part in SHOPPING_RESULT: | ||
| for item in catalog.keys(): | ||
| if part == item: | ||
| price = catalog[item] | ||
| COAST_LIST.append(price) | ||
| sum_list = sum(COAST_LIST) | ||
| return "\n". join([f"Сумма покупок в корзине составляет: {sum_list} BYN"]) | ||
|
|
||
| def show_cart(): | ||
| list1 = SHOPPING_RESULT | ||
| list2 = COAST_LIST | ||
| for item in range(0, len(list1)): | ||
| DICT[list1[item]] = list2[item] | ||
| write_file("app\shopping_cart.json", DICT) | ||
| def summ_guide(sum_list: List): | ||
| total_sum = 0 | ||
| result = "" | ||
| for item in sum_list: | ||
| result += item[0] + "\n" | ||
| total_sum += item[1] | ||
| result += f"Сумма покупок в корзине составляет: {total_sum} BYN" + "\n" | ||
| return result | ||
|
|
||
| def show_menu(): | ||
| data = get_menu() | ||
| message = format_catalog(data) | ||
| return message | ||
|
|
||
|
|
||
| def make_choice (choice: int): | ||
| def choose_guide(): | ||
| cart = add_cart(get_menu()) | ||
| data = get_whole_data() | ||
| data["shopping_cart"] = provide_data(data["shopping_cart"]) | ||
| data["shopping_cart"].append(cart) | ||
| data["shopping_cart"] = provide_file(data["shopping_cart"]) | ||
| With_File_Working.write_file("Shops/travel_guide_store/app\guide_offer.json", data) | ||
| return "Товар добавлен в корзину" | ||
|
|
||
| def check_cart(): | ||
| return summ_guide(provide_data(show_cart())) | ||
|
|
||
| def make_choice (choice) -> str: | ||
| result = "" | ||
| if choice == 1: | ||
| guides = get_menu() | ||
| message = format_catalog(guides) | ||
| result = message | ||
| result = show_menu() | ||
| elif choice == 2: | ||
| result =add_cart(SHOPPING_RESULT) | ||
| result =choose_guide() | ||
| elif choice == 3: | ||
| guides= summ_guide(SHOPPING_RESULT) | ||
| result = guides | ||
|
|
||
| elif choice == 4: | ||
| guides = show_cart() | ||
| message = read_cart("app\shopping_cart.json") | ||
| result = message | ||
| elif choice ==5: | ||
| result= check_cart() | ||
| elif choice ==4: | ||
| pass | ||
|
|
||
| return result | ||
|
|
||
| else: | ||
| return user_menu() | ||
| return result | ||
|
|
||
| def save_make_choice(choice): | ||
| result = "" | ||
|
|
||
| try: | ||
| result = make_choice(choice = int(choice)) | ||
| except ValueError: | ||
| result = "Вводите только цифры!" | ||
| except FileNotFoundError: | ||
| result = "В магазине нет товаров"+ '\n' | ||
| return result | ||
|
|
||
| def run() -> None: | ||
| choice = None | ||
| while choice !=5: | ||
| while choice !="4": | ||
| print(user_menu()) | ||
| choice = int(input("Введите пункт меню: ")) | ||
| message = make_choice(choice) | ||
| print(message) | ||
| choice = input("Введите пункт меню: ") | ||
| print(save_make_choice(choice = choice)) | ||
|
|
||
| run() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Именование классов пишется без
_, то есть вместо With_File_Working нужно написать WithFileWorking