From 966cf867dc9cef013e3f087108bb7333689e58e3 Mon Sep 17 00:00:00 2001
From: ys554 <ys554@cs.drexel.edu>
Date: Thu, 14 Mar 2019 22:06:31 -0400
Subject: [PATCH] assn3
---
assn3/README.MD | 5 ++
assn3/accounts | 188 ++++++++++++++++++++++++++++++++++++++++++++++++
assn3/makefile | 15 ++++
assn3/sample.db | 7 ++
4 files changed, 215 insertions(+)
create mode 100644 assn3/README.MD
create mode 100755 assn3/accounts
create mode 100644 assn3/makefile
create mode 100644 assn3/sample.db
diff --git a/assn3/README.MD b/assn3/README.MD
new file mode 100644
index 0000000..2d1714c
--- /dev/null
+++ b/assn3/README.MD
@@ -0,0 +1,5 @@
+Written in python 3
+
+I used sample.db to test my codes. My code currentlt reads in exported environment variable, so I export sample.db as environment variable.
+
+Thank you!
diff --git a/assn3/accounts b/assn3/accounts
new file mode 100755
index 0000000..141117e
--- /dev/null
+++ b/assn3/accounts
@@ -0,0 +1,188 @@
+#!/usr/bin/env python3
+#Yegeon Seo
+#Assignment 3
+
+import sys, time, os, math, random
+
+def display(data):
+ arg = sys.argv[1]
+
+ while True:
+ if arg == "-i":
+ print("\nInfo")
+ elif arg == "-h":
+ print("\nHistory")
+ elif arg == "-t":
+ print("\nTransaction")
+
+ print("-------")
+
+ count = 1
+ isValid = []
+
+ for line in data:
+ accData = line.split(":")
+ accID = int(accData[0])
+ accName = accData[1]
+
+ accInfo = [accName, accID]
+
+ if accInfo not in isValid:
+ isValid.append(accInfo)
+
+ for person in isValid:
+ print("%s) %s %s" % (count, person[0], person[1]))
+ count += 1
+
+ if arg == "-t":
+ print("(c)reate new account")
+
+ print("(q)uit\n")
+ choice = input("Enter choice : ")
+
+ if choice == "q":
+ return False
+ elif choice == "c" and arg == "-t":
+ createAccount(isValid)
+ else:
+ try:
+ idNum = isValid[int(choice) -1][1]
+ except (IndexError, ValueError):
+ print("Invalid Choice: Exiting")
+ exit()
+
+ personInfo = getPersonal(idNum, data)
+
+ if arg == "-i":
+ info(personInfo, idNum)
+ elif arg == "-h":
+ history(personInfo, idNum)
+ elif arg == "-t":
+ transaction(personInfo, idNum)
+
+def createAccount(isValid):
+ random.seed(time.time())
+ name = input("\n\tEnter Name: ")
+
+ date = (time.strftime("%y.%m.%d"))
+ ids = []
+
+ newID = random.randint(1000, 9999)
+
+ for person in isValid:
+ ids.append(person[1])
+ while newID in ids:
+ newID = random.randint(1000,9999)
+
+ print("=t%s %s: created" %(name, newID))
+
+ temp = open("temp.db", "a")
+ temp.write("{}:{}:{}:{}:{}\n".format(newID, name, date, "D", 0))
+ temp.close()
+
+def getPersonal(idNum, data):
+ personInfo = []
+
+ for line in data:
+ accInfo = line.split(":")
+ accountID = int(accInfo[0])
+
+ if idNum == accountID:
+ personInfo.append(line)
+
+ return personInfo
+
+def info(personInfo, idNum):
+ balance = 0
+
+ for log in personInfo:
+ accInfo = log.split(":")
+ name = accInfo[1]
+
+ if accInfo[3] == "D" or accInfo[3] == "d":
+ balance += float(accInfo[4])
+ else:
+ balance -= float(accInfo[4])
+
+
+ print("\n\tAccount#: %s" % idNum)
+ print("\tName: %s" % name)
+
+ if balance >= 0:
+ print("\tBalance: $%0.2f\n" % balance)
+ else:
+ print("\tBalance: -$%0.2f\n" % math.fabs(balance))
+
+def history(personInfo, idNum):
+ print("")
+
+ for log in personInfo:
+ accInfo = log.split(":")
+
+ if accInfo[3] == "D":
+ print('\t%sDeposit $%0.2f' % (accInfo[2], float(accInfo[4])))
+ else:
+ print('\t%sWthdrawl $%0.2f' % (accInfo[2], float(accInfo[4])))
+
+ print("")
+
+def transaction(personInfo, idNum):
+ date = (time.strftime("%y.%m.%d"))
+
+ trans = input("\n\t(W)ithdraw or (D)eposit: ")
+ amount = input("\tAmount: ")
+
+ for log in personInfo :
+ accInfo = log.split(":")
+ name = accInfo[1]
+
+ temp = open("temp.db", "a")
+ temp.write("{}:{}:{}:{}:{}\n".format(idNum, name, date, trans, amount))
+ temp.close()
+
+ print("\n\tTransaction Confirmed.")
+
+def testArg():
+ if len(sys.argv) < 2:
+ print("No arguments passed. Exiting")
+ exit()
+
+ valid = ["-i", "-h", "-t"]
+
+ if sys.argv[1] == "-?":
+ print('usage: accounts [-iht] ...')
+ exit()
+
+ if sys.argv[1] not in valid:
+ print("Accounts: illegal option {}".format(sys.argv[1]))
+ print('usage: accounts [-iht] ...')
+ exit()
+
+if __name__ == '__main__':
+ testArg()
+
+ accFile = open(os.environ["ACCT_LIST"], "r")
+ data = accFile.read().splitlines()
+ accFile.close()
+
+ display(data)
+
+ try:
+ tempFile = open("temp.db", "r")
+ dataFile = open(os.environ["ACCT_LIST"], "a")
+
+ exportData = tempFile.readlines()
+
+ for line in exportData:
+ dataFile.write(line)
+
+ tempFile.close()
+ dataFile.close()
+
+ os.remove("temp.db")
+
+ except IOError:
+ pass
+
+
+
diff --git a/assn3/makefile b/assn3/makefile
new file mode 100644
index 0000000..1eedde8
--- /dev/null
+++ b/assn3/makefile
@@ -0,0 +1,15 @@
+###### Python ######
+#Yegeon Seo
+#Assn 3 makefile
+
+.PHONY : build view clean
+
+build :
+ @# "Python Makefile"
+ chmod +x accounts
+
+view :
+ @\less accounts
+
+clean :
+ @\rm $.pyc
diff --git a/assn3/sample.db b/assn3/sample.db
new file mode 100644
index 0000000..9732a3e
--- /dev/null
+++ b/assn3/sample.db
@@ -0,0 +1,7 @@
+8273:Rocky Raccoon:08.10.17:D:520.00
+1902:Nancy Magill:08.10.29:D:770
+8273:Rocky Raccoon:08.11.01:W:20.00
+1902:Nancy Magill:08.11.14:W:51
+8273:Rocky Raccoon:08.11.17:W:60.00
+5883:Rocky Raccoon:08.11.17:D:60.00
+4224:Penny Lane:08.11.28:W:20.00
--
GitLab