- Notifications
You must be signed in to change notification settings - Fork 46
commit atm#140
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.
commit atm #140
Changes from all commits
File 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,112 @@ | ||
| """The script simulates ATM. | ||
| The script simulates ATM that preforms the following: | ||
| balance check, withdraw cash, deposit cash and | ||
| password change. | ||
| """ | ||
| import os | ||
| import sys | ||
| BALANCE = 1 | ||
| PASSWORD = 0 | ||
| ID_INDEX = 0 | ||
| PATH_TO_FILE = 1 | ||
| INDEX_START_OF_PASSWORD_AND_BALANCE = 1 | ||
| def file_to_dic(filename): | ||
| """Insert the ATM data from the file into the dictionary | ||
| Args: | ||
| filename (str): file name ending with '.txt' | ||
| Returns: | ||
| dictionary: key=id and value=[password, balance] | ||
| """ | ||
| atm = {} | ||
| with open(filename, 'r') as file: | ||
| for line in file: | ||
| details = line.strip().split(' ') | ||
| atm[details[ID_INDEX]] = details[INDEX_START_OF_PASSWORD_AND_BALANCE:] | ||
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. Saving the data as list isn't a good practice. | ||
| return atm | ||
| def operation_of_atm(id, atm_dic): | ||
kerenkay marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| """Performs the desired ATM operation for the inserted id. | ||
kerenkay marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Args: | ||
| id (str): the id for which you want the ATM to work | ||
kerenkay marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| atm_dic (dict): dictionary containing all ATM information | ||
kerenkay marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Returns: | ||
| dictionary: dictionary with the updated values | ||
| after performing the desired action | ||
| """ | ||
| if id in atm_dic: | ||
kerenkay marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| action = input("enter number of action:\n" | ||
| "1- for check the balance\n" | ||
| "2- for cash withdrawal\n" | ||
| "3- for cash deposit\n" | ||
| "4- for change password\n") | ||
| if action == '1': | ||
| print(f"your balance is: {atm_dic[id][BALANCE]}") | ||
| elif action == '2': | ||
| money = input("enter the amount you want to withdraw\n") | ||
| if int(atm_dic[id][BALANCE]) > int(money): | ||
| atm_dic[id][BALANCE] = str(int(atm_dic[id][BALANCE]) - int(money)) | ||
| else: | ||
| print('The account balance is less than the ' | ||
| 'then the amount you wanted to withdraw') | ||
| elif action == '3': | ||
| money = input("enter the amount you want to deposit\n") | ||
kerenkay marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| atm_dic[id][BALANCE] = str(int(atm_dic[id][BALANCE]) + int(money)) | ||
| elif action == '4': | ||
| password = input('enter new password\n') | ||
| atm_dic[id][PASSWORD] = password | ||
kerenkay marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| else: | ||
| print('action not detected') | ||
| else: | ||
| print('There is an error in the ID you entered') | ||
kerenkay marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| def main(): | ||
| """Do the ATM operation and save the changes | ||
| will do the desired ATM operation according to an | ||
| ID that has been accepted until receive -1 as an ID. | ||
| all the changes will be saved to the ATM. | ||
| Arg: | ||
| id (int): 9 digits ID (example: 322781145) | ||
kerenkay marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Raises: | ||
| Exception as e: No such file or directory | ||
| if the path to the file not exist will be an error. | ||
kerenkay marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| """ | ||
| try: | ||
| atm_dic = file_to_dic(sys.argv[PATH_TO_FILE]) | ||
| id = input('please enter your ID\n') | ||
| while id != '-1': | ||
| operation_of_atm(id, atm_dic) | ||
| id = input('please enter your ID\n') | ||
kerenkay marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| with open(sys.argv[PATH], 'w') as newfile: | ||
| for id in atm_dic: | ||
| newfile.writelines(f"{id} {atm_dic[id][PASSWORD]} " | ||
| f"{atm_dic[id][BALANCE]}\n") | ||
| except Exception as e: | ||
| print(e) | ||
kerenkay marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if __name__ == '__main__': | ||
| main() | ||
Uh oh!
There was an error while loading. Please reload this page.