Read in your tongue here..

Tuesday, January 19, 2016

ATM Program in TCL Linux.

proc writetofile { record data } {
    set i [expr [lindex $record 0]-1]
    set data [lreplace $data $i $i $record]
    set fp [open "data.txt" w]
    foreach x $data {
        puts $fp $x
    }
    close $fp
    puts "Transaction Successful."
}

proc depo { record data } {
    puts "**********\t Deposit Menu \t**********"
    puts "Enter amount to deposit:"
    gets stdin amount
    if {[lindex $record 3]=="SC"} then {
        puts "1.Savings Account\n2.Current Account\n3.Exit\nEnter Choice:"
        gets stdin choice       
        switch $choice {
            1 {
                puts "Current Balance is: Rs.[lindex $record 4]/="
                set record [lreplace $record 4 4 [expr [lindex $record 4]+$amount]]
                puts "New Modified Balance is: Rs.[lindex $record 4]/="
            }
            2 {
                puts "Current Balance is: Rs.[lindex $record 5]/="               
                set record [lreplace $record 5 5 [expr [lindex $record 5]+$amount]]
                puts "New Modified Balance is: Rs.[lindex $record 5]/="
            }
            3 {
                exit
            }
            default {
                puts "Invalid Input!!! Exiting Now..."
                exit
            }
        }
    } else {
        puts "Current Balance is: Rs.[lindex $record 4]/="       
        set record [lreplace $record 4 4 [expr [lindex $record 4]+$amount]]
        puts "New Modified Balance is: Rs.[lindex $record 4]/="
    }
    writetofile $record $data
}
proc withd { record data } {
    puts "**********\t Withdrawal Menu \t**********"
    puts "Enter amount to withdraw:"
    gets stdin amount
    if {[lindex $record 3]=="SC"} then {
        puts "1.Savings Account\n2.Current Account\n3.Exit\nEnter Choice:"
        gets stdin choice       
        switch $choice {
            1 {
                puts "Current Balance is: Rs.[lindex $record 4]/="
                if {[lindex $record 4]<$amount} then {
                    puts "Insufficient Funds!!!"
                    exit
                } else {               
                    set record [lreplace $record 4 4 [expr [lindex $record 4]-$amount]]
                }
                puts "New Modified Balance is: Rs.[lindex $record 4]/="
            }
            2 {
                puts "Current Balance is: Rs.[lindex $record 5]/="
                if {[lindex $record 5]<$amount} then {
                    puts "Insufficient Funds!!!"
                    exit
                } else {               
                    set record [lreplace $record 5 5 [expr [lindex $record 5]-$amount]]
                }
                puts "New Modified Balance is: Rs.[lindex $record 5]/="
            }
            3 {
                exit
            }
            default {
                puts "Invalid Input!!! Exiting Now..."
                exit
            }
        }
    } else {
        puts "Current Balance is: Rs.[lindex $record 4]/="
        if {[lindex $record 4]<$amount} then {
            puts "Insufficient Funds!!!"
            exit
        } else {               
            set record [lreplace $record 4 4 [expr [lindex $record 4]-$amount]]
        }
        puts "New Modified Balance is: Rs.[lindex $record 4]/="
    }
    writetofile $record $data
}

proc bal { record } {
    puts "**********\t Balance Enquiry \t**********"
    if {[lindex $record 3]=="SC"} then {
        puts "1.Savings Account\n2.Current Account\n3.Both\n4.Exit\nEnter Choice:"
        gets stdin choice       
        switch $choice {
            1 {
                puts "Savings Account Balance is: Rs.[lindex $record 4]/="
            }
            2 {
                puts "Current Account Balance is: Rs.[lindex $record 5]/="
            }
            3 {
                puts "Savings Account Balance is: Rs.[lindex $record 4]/="
                puts "Current Account Balance is: Rs.[lindex $record 5]/="
            }
            4 {
                exit
            }
            default {
                puts "Invalid Input!!! Exiting Now..."
                exit
            }
        }
    } elseif {[lindex $data $index 3]=="S"} then {
        puts "Savings Account Balance is: Rs.[lindex $record 4]/="   
    } else {
        puts "Current Account Balance is: Rs.[lindex $record 4]/="
    }
}

proc menu { record data } {
    puts "**********\tMain Menu\t**********"
    puts "1.Deposit\n2.Withdrawal\n3.Balance Enquiry\n4.Exit\nEnter Choice:"
    gets stdin choice
    switch $choice {
        1 {
            depo $record $data
        }
        2 {
            withd $record $data
        }
        3 {
            bal $record
        }
        4 {
            exit
        }
        default {
            puts "Invalid Input!!! Exiting Now..."
            exit
        }
    }
}
set fp [open "data.txt" r]
set file_data [read $fp]
close $fp
set data [split $file_data "\n"]
puts "**********/tWelcome To ATM Services./t**********"
set attempts 0
while {$attempts<3 br="">    incr attempts
    puts "Enter your user id:"
    gets stdin userid
    puts "Enter your pin:"
    gets stdin pin
    if {[string length $pin]!=4} then {
        puts "PIN should be of 4 digits only!!! Please try again. [expr 3-$attempts] attempts left!!!"
    continue
    }
    set index 0
    foreach record $data {
        if {$userid==[lindex $record 1] && $pin==[lindex $record 2]} then {
        set index [lindex $record 0]
        }
    }
    if {$index!=0} then {
        puts "Welcome, $userid."
        menu [lindex $data [expr $index-1]] $data
        break
    } else {
        puts "Wrong Credentials!!! Please try again. [expr 3-$attempts] attempts left!!!"
        continue
    }
}