- Notifications
You must be signed in to change notification settings - Fork 46
Feature/solved atm#129
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
base:master
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Feature/solved atm #129
Changes from all commits
a245ceec445527b4f91d343fcb6b0481167b1625b1be16d074a6a2b5a03f92aa5191c665fdc3a54f11613bbe8a84466c4be0276136a35c172488c3c5acd2ca3d0100e48b77acfd8e08556f7dd0609ac344a96367f064d951cd915bd5b8211File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| import os | ||
| import sys | ||
| import json | ||
| ATM_FILE_PLACE = 1 | ||
| BALANCE_OF_COSTUMER = 'balance' | ||
| PASSWORD_OF_COSTUMER = 'password' | ||
| COSTUMER_CHOICE_OPTIONS = "1 - View balance \n" \ | ||
| "2 - Withdraw \n" \ | ||
| "3 - Deposit \n" \ | ||
| "4 - Change password \n" \ | ||
| "5 - Exit\n" | ||
| SWITCHER = { | ||
| '1': lambda data, identifier: print(data[identifier][ | ||
| BALANCE_OF_COSTUMER]), | ||
| '2': lambda data, identifier: cash_withdraw(data, identifier), | ||
| '3': lambda data, identifier: cash_deposit(data, identifier), | ||
| '4': lambda data, identifier: change_password(data, identifier) | ||
| } | ||
Comment on lines
+13
to
+19
Collaborator 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. In general we don't like lambdas and prefer you just use helper functions. Its easier to read the code that way. for 2-4 the lambdas are completely unnecessary as you can simply use the function without any arguments as the value. Here is an example of what I mean: def f_2(a,b): MATCHER = {1:f_1, 2:f_2} result_2 = MATCHER2 #equals -1 print(result_1, result_2) | ||
| def check_convert_to_float(amount): | ||
| """Check if variable can be converted to float. | ||
| Args: | ||
| amount (str): Costumer's input. | ||
| Returns: | ||
| float: Change the type of the variable 'amount' and return it, | ||
| else if couldn't- return None. | ||
| """ | ||
| try: | ||
| return float(amount) | ||
| except ValueError: | ||
| return None | ||
| def cash_withdraw(costumers_data, costumer_id): | ||
| """Withdraw amount of money from the ATM . | ||
lielya marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Args: | ||
| costumers_data (dict): Bank costumer data. | ||
| costumer_id (str): Costumer's ID. | ||
| """ | ||
lielya marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| amount_to_withdraw = input("Amount to withdraw: ") | ||
| check_amount = check_convert_to_float(amount_to_withdraw) | ||
| balance_now = float(costumers_data[costumer_id][BALANCE_OF_COSTUMER]) | ||
| if check_amount and 0 < check_amount <= balance_now: | ||
| new_balance = balance_now - check_amount | ||
| costumers_data[costumer_id][BALANCE_OF_COSTUMER] = new_balance | ||
| update_changes_in_dictionary(costumers_data) | ||
| else: | ||
| print("Failed - check amount\n") | ||
| def cash_deposit(costumers_data, costumer_id): | ||
| """Deposit money to the bank. | ||
| Args: | ||
| costumers_data (dict): Bank costumer data. | ||
| costumer_id (str): Costumer's ID. | ||
| """ | ||
| amount_to_deposit = input("Amount to deposit: ") | ||
| check_amount = check_convert_to_float(amount_to_deposit) | ||
| if check_amount and check_amount > 0: | ||
| balance_now = float(costumers_data[costumer_id][ | ||
| BALANCE_OF_COSTUMER]) | ||
| new_balance = balance_now + check_amount | ||
| costumers_data[costumer_id][BALANCE_OF_COSTUMER] = new_balance | ||
| update_changes_in_dictionary(costumers_data) | ||
| else: | ||
| print("Failed - check amount\n") | ||
| def change_password(costumers_data, costumer_id): | ||
| """Change costumer's password. | ||
| Args: | ||
| costumers_data (dict): Bank costumer data. | ||
| costumer_id (str): Costumer's ID. | ||
| """ | ||
| new_password = input("New password: ") | ||
| if len(new_password) == 4 and new_password.isdigit(): | ||
| costumers_data[costumer_id][PASSWORD_OF_COSTUMER] = new_password | ||
Collaborator 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 is a source for bugs. | ||
| update_changes_in_dictionary(costumers_data) | ||
Collaborator 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 is a bad way to do it. | ||
| else: | ||
| print("Should be 4 digits.") | ||
| def read_costumers_information_from_file(path_of_file): | ||
| """Read costumer's information from file to dictionary. | ||
| Args: | ||
| path_of_file (str): Path of file that contains all bank costumer | ||
| data. | ||
| Returns: | ||
| dict: A nested dictionary of bank costumer data, or None. | ||
| """ | ||
| if os.path.isfile(path_of_file): | ||
| with open(path_of_file, 'r') as file_j: | ||
| data = json.load(file_j) | ||
| costumers_data = data | ||
| return costumers_data | ||
Collaborator 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 not just: | ||
| print("File not found") | ||
| return None | ||
| def update_changes_in_dictionary(costumers_data): | ||
| """Update changes in the dictionary after costumer's actions. | ||
| Args: | ||
| costumers_data (dict): Bank costumer data. | ||
| """ | ||
| with open(sys.argv[ATM_FILE_PLACE], 'w') as atm_file: | ||
| atm_file.write(json.dumps(costumers_data)) | ||
lielya marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. Collaborator 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. add a description of the available choices | ||
| def manage_costumer_actions(costumers_data, costumer_id, costumer_password): | ||
| """Manage costumer actions. | ||
| Args: | ||
| costumers_data (dict): Bank costumer data. | ||
| costumer_id (str): Costumer's ID. | ||
| costumer_password (str): Costumer's password. | ||
| """ | ||
| if costumer_id in costumers_data and costumers_data[ | ||
| costumer_id][PASSWORD_OF_COSTUMER] == costumer_password: | ||
Collaborator 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 | ||
| costumer_choice = input(COSTUMER_CHOICE_OPTIONS) | ||
| while costumer_choice != '5': | ||
| SWITCHER.get(costumer_choice, lambda data, identifier: print( | ||
Collaborator 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. if you don;t use the lambda args, you don't need to give them names, you can do one of the two: | ||
| "Invalid option"))(costumers_data, costumer_id) | ||
| costumer_choice = input(COSTUMER_CHOICE_OPTIONS) | ||
| print("Thank you.") | ||
| else: | ||
| print("ID or password is not correct. Try again:\n") | ||
| def manage_new_costumer(costumers_data): | ||
Collaborator 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 think that this function does something more like a login. Rename. | ||
| """Get a new costumer and act accordingly. | ||
| When the customer ID will be (-1), the ATM will be turned off. | ||
| Args: | ||
| costumers_data (dict): Bank costumer data. | ||
| """ | ||
| costumer_id = input("ID: ") | ||
| while costumer_id != '-1': | ||
| costumer_password = input("Password: ") | ||
| manage_costumer_actions(costumers_data, costumer_id, | ||
| costumer_password) | ||
| costumer_id = input("ID: ") | ||
| def main(): | ||
Collaborator 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 function too long. | ||
| costumers_data = read_costumers_information_from_file( | ||
| path_of_file=sys.argv[ATM_FILE_PLACE]) | ||
Collaborator 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. leave spaces before and after the equal sign. | ||
| if costumers_data: | ||
| manage_new_costumer(costumers_data) | ||
| if __name__ == '__main__': | ||
| main() | ||
Collaborator 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 code looks much better, good job | ||
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.
What about what we talked about?
both lines do exactly the same:
res1 = lambda x: func(x)res2 = funcBoth lines equal - res1 = res2