Completed transactions on banking

master
Matt Theissen 4 years ago
parent 7abf7d5f3c
commit d6f3e2efdd

@ -25,28 +25,33 @@ new_account() {
clear
read -p "Enter the name of your account: " user1
echo ""
echo "-----------------------------------"
echo "Your account name will be $user1."
echo "-----------------------------------"
echo ""
read -n1 -p "Is that correct? [y/N] " yn
case $yn in
# If user selects yes. Insert their username and a timestamp into the database.
[Yy] ) $db_conn "insert into account (username,timestamp) values ('$user1','now');" &>/dev/null || echo "Username already taken.";
[Yy] ) echo ""
$db_conn "insert into account (username,timestamp) values ('$user1','now');"
# Sets the user's id as the variable.
user1_id=$($db_conn "select id from account where username='$user1';" | awk 'FNR == 3 {print}')
# Gives the user the starting balance.
$db_conn "insert into balance (id,balance) values ('$user1_id',$bal);" &>/dev/null
$db_conn "insert into balance (id,balance) values ('$user1_id',$bal);"
# Pulls the user's account and ID for awk'ing
user_account=$($db_conn "select id,username from account;" | grep $user1_id)
echo ""
acc_id=$(echo "$user_account" | awk -F\| '{print $1}')
acc_name=$(echo "$user_account" | awk -F\| '{print $2}')
echo "Account name: $acc_name"
echo "Account ID: $acc_id"
echo "-----------------------------------"
echo "Account name: $user1"
echo "Account ID: $user1_id"
echo "-----------------------------------"
echo ""
# Transactioning
$db_conn "insert into transaction (id,username,amount,timestamp) values ('$user1_id','$user1','+$bal','now');"
;;
# If the user selects anything other than [Yy]
@ -115,38 +120,42 @@ do
## Not sure why this was here...
#echo "$with_user" | awk 'FNR == 1 {print}'
# Grabs the current amount the user has stored in the database.
with_1_amt=$($db_conn "select id,username,balance from account join balance using (id) where username='$with_user';"| awk 'FNR == 3' | awk -F\| '{print $3}')
with_1_amt=$($db_conn "select id,username,balance from account join balance using (id) where username='$with_user';"| awk 'FNR == 3' | awk -F\| '{print $3}')
# This grabs the user's ID to use on the balance table.
with_user_id=$($db_conn "select id,username,balance from account join balance using (id) where username='$with_user';" | awk 'FNR == 3' | awk -F\| '{print $1}')
with_user_id=$($db_conn "select id,username,balance from account join balance using (id) where username='$with_user';" | awk 'FNR == 3' | awk -F\| '{print $1}')
# Does the math to minus the withdraw.
with_new_amt=$($db_conn "select balance - '$with_amt' from balance where id = '$with_user_id';" | awk 'FNR ==3')
# Updates the database with the new amount.
$db_conn "update balance set balance = '$with_new_amt' where id = '$with_user_id';"
;;
$db_conn "update balance set balance = '$with_new_amt' where id = '$with_user_id';"
# Transactioning
$db_conn "insert into transaction (id,username,amount,timestamp) values ('$with_user_id','$with_user','-$with_amt','now');" ;;
2 ) read -p "Deposit to user: " depo_user
read -p "Amount to deposit: " depo_amt
LAST_USER=$depo_user
depo_1_amt=$($db_conn "select id,username,balance from account join balance using (id) where username='$depo_user';"| awk 'FNR == 3' | awk -F\| '{print $3}')
depo_1_amt=$($db_conn "select id,username,balance from account join balance using (id) where username='$depo_user';"| awk 'FNR == 3' | awk -F\| '{print $3}')
# This grabs the user's ID to use on the balance table.
depo_user_id=$($db_conn "select id,username,balance from account join balance using (id) where username='$depo_user';" | awk 'FNR == 3' | awk -F\| '{print $1}')
depo_user_id=$($db_conn "select id,username,balance from account join balance using (id) where username='$depo_user';" | awk 'FNR == 3' | awk -F\| '{print $1}')
# Does the math to add the withdraw.
depo_new_amt=$($db_conn "select balance + '$depo_amt' from balance where id = '$depo_user_id';" | awk 'FNR ==3')
# Updates the database with the new amount.
$db_conn "update balance set balance = '$depo_new_amt' where id = '$depo_user_id';" ;;
$db_conn "update balance set balance = '$depo_new_amt' where id = '$depo_user_id';"
# Transactioning
$db_conn "insert into transaction (id,username,amount,timestamp) values ('$depo_user_id','$depo_user','+$depo_amt','now');" ;;
3 ) read -p "Transfer from user: " tran_user1
read -p "Transer to user: " tran_user2
read -p "Amount to transfer: " tran_amt
# Grabs user IDs.
tran_user1_id=$($db_conn "select id,username,balance from account join balance using (id) where username='$tran_user1';" | awk 'FNR == 3' | awk -F\| '{print $1}') || echo "Invalid user!";
tran_user2_id=$($db_conn "select id,username,balance from account join balance using (id) where username='$tran_user2';" | awk 'FNR == 3' | awk -F\| '{print $1}') || echo "Invalid user!";
tran_user1_id=$($db_conn "select id,username,balance from account join balance using (id) where username='$tran_user1';" | awk 'FNR == 3' | awk -F\| '{print $1}')
tran_user2_id=$($db_conn "select id,username,balance from account join balance using (id) where username='$tran_user2';" | awk 'FNR == 3' | awk -F\| '{print $1}')
# Does the math to complete the transfer.
tran1_new_amt=$($db_conn "select balance - '$tran_amt' from balance where id = '$tran_user1_id';" | awk 'FNR ==3')
tran2_new_amt=$($db_conn "select balance + '$tran_amt' from balance where id = '$tran_user2_id';" | awk 'FNR ==3')
tran1_new_amt=$($db_conn "select balance - '$tran_amt' from balance where id = '$tran_user1_id';" | awk 'FNR ==3')
tran2_new_amt=$($db_conn "select balance + '$tran_amt' from balance where id = '$tran_user2_id';" | awk 'FNR ==3')
# Updates the database with the new amounts.
$db_conn "update balance set balance = '$tran1_new_amt' where id = '$tran_user1_id';"
$db_conn "update balance set balance = '$tran2_new_amt' where id = '$tran_user2_id';"
$db_conn "update balance set balance = '$tran1_new_amt' where id = '$tran_user1_id';"
$db_conn "update balance set balance = '$tran2_new_amt' where id = '$tran_user2_id';"
# Transactioning
$db_conn "insert into transaction (id,username,amount,timestamp) values ('$tran_user1_id','$tran_user1','-$tran_amt','now');"
$db_conn "insert into transaction (id,username,amount,timestamp) values ('$tran_user2_id','$tran_user2','+$tran_amt','now');"
LAST_USER=$tran_user1 ;;
4 ) $db_conn "select id,username,balance from account join balance using (id) where username='$LAST_USER';" ;;