Riju’s Vault System (using Python)

I am a lazy person. I don’t code much. When I get an idea, I spend time pondering whether it’ll be possible to implement it in a code within the limits of my programming knowledge. If yes, then it’s as good as done. If no, then there’s nothing to be done since my ideas are never worth learning new things.

But today’s an exception. I wrote a program! Very basic, though. No groundbreaking concept, just a product of loops and objects and boredom.

It can be done in a simpler way probably.

The program creates a system of vaults. To create a vault, a person needs to enter his name and set a password. A vault can be accessed using its serial number and password. Once access is granted to a vault, one can check its status, and make deposits and withdrawals. It is necessary to sign out in order to close a vault.

Part of the code is mere decoration (time delays and stuff). I’ve checked almost all possible scenarios and found it to be error free, so do notify me in case you find some bugs.

import time
class Vault:
    name=""
    password=""
    tname=""
    tpass=""
    tsno=0
    tamount=0
    sno=0
    balance=0
    vaultlist=[]
    choice=0

    def create(self,name,passw):
        self.name=name
        self.password=passw
        Vault.sno+=1
        self.sno=Vault.sno
        self.balance=0
        Vault.vaultlist.append(self)
        print("Creating vault...")
        time.sleep(1)
        print("Vault number %d created in the name of %s." %(self.sno,self.name))

    def check(self):
        print("Fetching balance info...")
        time.sleep(1)
        print("Your current balance is Rs.%d"%self.balance)
        print " "

    def authenticate(self,passw):
        print("Verifying...")
        time.sleep(1)
        if self.password == passw:
            print("Access granted!")
            return 1
        else:
            print("Access denied! Invalid password.")
            print " "
            return 0

    def deposit(self,a):
        self.balance+=a
        print("Depositing...")
        time.sleep(1)
        print("Rs.%d has been deposited to your vault. Your current balance is Rs.%d."%(a,self.balance))
        print(" ")

    def withdraw(self,a):
        print("Withdrawing...")
        time.sleep(1)
        if self.balance>=a:
            self.balance-=a
            print("Withdrawal successful! New balance is Rs.%d." %self.balance)
            print(" ")
        else:
            print("Insufficient balance.")
            print(" ")

print "Hello! Welcome to Riju's vault system."
while 1:
    time.sleep(1)
    print "1. Create a vault"
    print "2. Sign in"
    print "3. Exit"
    choice = input("Enter choice: ")
    if choice == 1:
        v = Vault()
        tname = raw_input("Enter your name: ")
        tpass = raw_input("Set your password: ")
        v.create(tname,tpass)
        tamount = input("Enter starting deposit: ")
        v.deposit(tamount)
    elif choice == 2:
        tsno = input("Enter vault number: ")
        if len(Vault.vaultlist)<tsno:
            print("Vault does not exist.")
            print " "
            continue
        tpass = raw_input("Enter password: ")
        if Vault.vaultlist[(tsno-1)].authenticate(tpass) == 1:
            time.sleep(2)
            print("Welcome %s." %Vault.vaultlist[(tsno-1)].name)
            while 1:
                time.sleep(2)
                print("1. Check balance")
                print("2. Deposit")
                print("3. Withdraw")
                print("4. Sign out")
                choice = input("Enter choice: ")
                if choice == 1:
                    Vault.vaultlist[(tsno-1)].check()
                elif choice == 2:
                    tamount=input("Enter amount to deposit: ")
                    Vault.vaultlist[(tsno-1)].deposit(tamount)
                elif choice == 3:
                    tamount=input("Enter amount to withdraw: ")
                    Vault.vaultlist[(tsno-1)].withdraw(tamount)
                elif choice == 4:
                    print "Signing out..."
                    time.sleep(2)
                    print "You have been successfully signed out."
                    print " "
                    break
                else:
                    print "Invalid choice"
                    print " "
    elif choice == 3:
        print "Exiting..."
        time.sleep(1)
        print("Thank you for choosing Riju's vault system. You suck!")
        time.sleep(5)
        break
    else:
        print "Invalid choice"

Sample output:

Hello! Welcome to Riju’s vault system.
1. Create a vault
2. Sign in
3. Exit
Enter choice: 1
Enter your name: Debapriyo
Set your password: iloveghibli
Creating vault…
Vault number 1 created in the name of Debapriyo.
Enter starting deposit: 9000
Depositing…
Rs.9000 has been deposited to your vault. Your current balance is Rs.9000.

1. Create a vault
2. Sign in
3. Exit
Enter choice: 2
Enter vault number: 1
Enter password: iloveghibli
Verifying…
Access granted!
Welcome Debapriyo.
1. Check balance
2. Deposit
3. Withdraw
4. Sign out
Enter choice: 1
Fetching balance info…
Your current balance is Rs.9000

1. Check balance
2. Deposit
3. Withdraw
4. Sign out
Enter choice: 2
Enter amount to deposit: 2000
Depositing…
Rs.2000 has been deposited to your vault. Your current balance is Rs.11000.

1. Check balance
2. Deposit
3. Withdraw
4. Sign out
Enter choice: 3
Enter amount to withdraw: 5000
Withdrawing…
Withdrawal successful! New balance is Rs.6000.

1. Check balance
2. Deposit
3. Withdraw
4. Sign out
Enter choice: 4
Signing out…
You have been successfully signed out.

1. Create a vault
2. Sign in
3. Exit
Enter choice: 3
Exiting…
Thank you for choosing Riju’s vault system. You suck!

6 Replies to “Riju’s Vault System (using Python)”

  1. Pretty neat coding. All the information stored and transacted would be volatile, one-session variables though, unless you write them to a file or something. But that I guess is another project.

    1. Thanks. It’ll be easy to make an administrator account through which all the vaults’ serial number, name, password and balance can be viewed. But yes, everything will die on exiting. I don’t know much about files yet, so didn’t think about that.

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: