My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
Simulating a Banking System with Python

Simulating a Banking System with Python

Anderson Osayerie's photo
Anderson Osayerie
·May 18, 2020

Herein, we would be simulating a banking system. This would help us get familiar with some aspects of the python file system. This was a task given during the start.ng program for aspiring developers. Our system would have the following features.

  1. There would be a file named “staff.txt” and another named “customer.txt”. The staff.txt file would contain predefined staff details of two fictional staffs. The details would include username, password, email and full name.
  2. When our program runs, it should present two options: Staff Login and Close App. When the first option is chosen, the user should be asked for their username and password. The program checks our file to verify if the user is a registered staff.
  3. After successful login, our staff is presented with a set of options: a) Create new bank account, b) Check account details, c) Logout. If staff selects Create new bank account, the staff should be made to supply the following details of the new customer: Account name, Opening balance, Account Type, Account Email. The details should be saved in the “customer.txt” file and a random 10 digits account number would be created for the new customer. If staff selects Check account details, the program should request for the customer’s account number and fetch the details of the customer and display it to the staff. If staff selects logout, the program should log the staff out and display the initial two options, where staff can decide to login again or close the program.

Below are our fictional predefined staff details stored in a ‘dictionary’. staff.png

We start of by creating a function that accesses the file and checks if user details match any of our staff details. We make use of the in-built python “open” function to access the file, then use the “json” package to read the contents of the file. login.png

Next we create a function that allows our staff to create new account details. “Never Trust User Input” is a common concept in programming, so we would be carrying out some basic validation on the inputs entered. We would ensure that the name entered is more than four characters and contains alphabets. The opening balance entered has to be a number. The account type has to be either “Savings” or “Current”. The first part of the function is shown below. create-first.png

The email has to follow the pattern for email address — . We would be using the “re” package to verify that. We use the “search” function of the “re” package. This function takes two arguments, the pattern we are using as standard, and the pattern we are comparing to the standard. Using the “randint” function of the “random” module we generate a random ten digit number for the new customer. We then save our user details as a “json” string with the aid of the “json” package into the “customer.txt” file. The remaining part of the create function is shown below. Quite a long function, I tell you. create--final.png

Now we are going to create a function that checks through our customer details for the presence of an account, and returns the details of the account. We call this account function. account.png

Combining our create and account functions, we now create a function that allows our staff select what action they want to perform at initial login. We call this the staff function. Finally we create a welcome function, which is the starting point for our program. welcome (1).png

This was a long ride. But finally we have been able to use some basic python knowledge to simulate a bank app. Your comments and questions are welcomed. Thanks

The source code can be found here — github.com/ema-mimus/snbank/blob/master/snb..