Ejemplo n.º 1
0
class Controller:
	def __init__(self):
		self.mymenu=Views()
		self.myclientcsv=CSV('clientlist')
		self.mybankercsv=CSV('bankerlist')

	def checkownnameandpass(self):
		self.mymenu.firstmenu()
		if (self.mymenu.neworexuser=='n'):
			return self.createnewuser()
		elif (self.mymenu.neworexuser=='e'):

			countfound=0
			countwrongpass=0
			if (self.mymenu.userrole=='b'):
				self.mybankercsv.openreader()
				with self.mybankercsv.file:
					self.mybankercsv.dictreader()
					for row in self.mybankercsv.reader:
						print (row)
						if (self.mymenu.ownname==row['Username'] and self.mymenu.ownpassword==row['Password']):
							countfound+=1
						elif (self.mymenu.ownname==row['Username'] and self.mymenu.ownpassword!=row['Password']):
							countwrongpass+=1
					if (countfound==0 and countwrongpass==0 ):
						print ("you are not an existing user in our bank, please create a new banker account first!")
						return self.createnewuser()

					elif (countfound==0 and countwrongpass >0 ):
						print ("you entered the wrong banker account password, please reenter your banker account username and password!")
						return self.checkownnameandpass()
					elif (countfound>0 and countwrongpass==0):
						print ("you are now logged into your banker account!")
						return self.actions()
			elif (self.mymenu.userrole=='c'):
				self.myclientcsv.openreader()
				with self.myclientcsv.file:
					self.myclientcsv.dictreader()
					for row in self.myclientcsv.reader:
						if (self.mymenu.ownname==row['Username'] and self.mymenu.ownpassword==row['Password']):
							countfound+=1
						elif (self.mymenu.ownname==row['Username'] and self.mymenu.ownpassword!=row['Password']):
							countwrongpass+=1
					if (countfound==0 and countwrongpass==0 ):
						print ("you are not an existing user in our bank, please have a banker create an account for youn first!")
						return self.checkownnameandpass()

					elif (countfound==0 and countwrongpass >0 ):
						print ("you entered the wrong client account password, please reenter your client account username and password!")
						return self.checkownnameandpass()
					elif (countfound>0 and countwrongpass==0):
						print ("you are now logged into your client account!")
						return self.actions()

		


	def checkclientname(self):
		if (self.mymenu.action=='1c' or self.mymenu.action=='2c'  or self.mymenu.action=='4c' or self.mymenu.action=='1b'):
			return True
		elif (self.mymenu.action=='3c'):
			return self.checkothername()
		else:
			countfound=0
			self.myclientcsv.openreader()
			with self.myclientcsv.file:
				self.myclientcsv.dictreader()
				for row in self.myclientcsv.reader:
					if (self.mymenu.clientname==row['Username'] ):
						countfound+=1
			
				if (countfound==0):
					print ("the client username is not an existing client in our bank!")
					return False

				elif (countfound>0):
					print ("your client is found in our database!")
					if (self.mymenu.action=='4b'):
						return self.checkothername()
					elif (self.mymenu.action!='4b' and self.mymenu.action!='3c' and self.mymenu.action!='1c' and self.mymenu.action!='2c'):
						return True


		
	def checkothername(self):
		countfound=0
		self.myclientcsv.openreader()
		with self.myclientcsv.file:
			self.myclientcsv.dictreader()
			for row in self.myclientcsv.reader:
				if (self.mymenu.otherclientname==row['Username'] ):
					countfound+=1
			
			if (countfound==0):
				print ("the client username is not an existing client in our bank!")
				return False

			elif (countfound>0):
				print ("your client is found in our database!")
				return True

		
	def createnewuser(self):

		if (self.mymenu.newuserchoice=='b'):
			countfound=0
			self.mybankercsv.openreader()
			with self.mybankercsv.file:
				self.mybankercsv.dictreader()
				for row in self.mybankercsv.reader:
					
					if (self.mymenu.newusername==row['Username'] ):
						countfound+=1
				if (countfound>0):
					print ("this banker name already exists in our system, try to log in as an existing user!")
					return self.checkownnameandpass()
				elif (countfound==0):
					bankerlist.append(Banker(self.mymenu.newusername, self.mymenu.newuserpassword))
					global bankernum
					bankernamedic[self.mymenu.newusername]=bankerlist[bankernum]
					bankernum+=1
					self.mybankercsv.openwriter()
					with self.mybankercsv.file:
						self.mybankercsv.dictwriter()
						self.mybankercsv.writer.writerow({'Username':self.mymenu.newusername, 'Password':self.mymenu.newuserpassword})
						self.mybankercsv.close()
					mydatabase.connect()
					mydatabase.cursor()
					mydatabase.curs.execute("INSERT INTO Bankers(Username, Password, Time_Account_Created)"" VALUES(?,?,?)", (bankernamedic[self.mymenu.newusername].username, bankernamedic[self.mymenu.newusername].password, bankernamedic[self.mymenu.newusername].time_created))
					mydatabase.commit()
					mydatabase.close_connection()

					print ("your new banker account has been successfully created, please log in as an existing user now. your username is "+self.mymenu.newusername)	
					return self.checkownnameandpass()


	def actions(self):
		self.mymenu.whattodo()
		mycheckclient=self.checkclientname()
		while(mycheckclient==False):
			self.mymenu.getinfo()

		if (self.mymenu.action=='1b'):
			
			bankernamedic[self.mymenu.ownname].createaccount(self.mymenu.accounttype, self.mymenu.clientbalance, self.mymenu.clientname, self.mymenu.clientpassword, self.mymenu.newcreateaccountnum)
			mydatabase.connect()
			mydatabase.cursor()
			mydatabase.curs.execute("INSERT INTO Clients(Username, Password, Time_Account_Created)"" VALUES(?,?,?)", (self.mymenu.clientname, self.mymenu.clientpassword, usernamedic[self.mymenu.clientname].time_created))
			mydatabase.curs.execute("INSERT INTO Client_Accounts(Accountnumber, Balance, Account_Type, Client_Username)"" VALUES(?,?,?,?)", (self.mymenu.newcreateaccountnum, self.mymenu.clientbalance, self.mymenu.accounttype, self.mymenu.clientname))
			mydatabase.commit()
			mydatabase.close_connection()
			mylogout=self.mymenu.logout()
			if (mylogout==False):
				return self.actions()
			elif (mylogout==True):
				return self.checkownnameandpass()
		elif (self.mymenu.action=='2b'):
			
			clientnewbal=bankernamedic[self.mymenu.ownname].deposit(float(self.mymenu.clientamount),self.mymenu.clientname ,int(self.mymenu.clientaccountnum) )
			
			mydatabase.connect()
			mydatabase.cursor()
			mydatabase.curs.execute("UPDATE Client_Accounts SET Balance= ? WHERE Accountnumber=?",(clientnewbal, int(self.mymenu.clientaccountnum)))
			mydatabase.commit()
			mydatabase.close_connection()
			mylogout=self.mymenu.logout()
			if (mylogout==False):
				return self.actions()
			elif (mylogout==True):
				return self.checkownnameandpass()

		elif (self.mymenu.action=='3b'):
		
			clientnewbal=bankernamedic[self.mymenu.ownname].withdraw(float(self.mymenu.clientamount),self.mymenu.clientname ,int(self.mymenu.clientaccountnum))
			mydatabase.connect()
			mydatabase.cursor()
			mydatabase.curs.execute("UPDATE Client_Accounts SET Balance= ? WHERE Accountnumber=?",(clientnewbal, int(self.mymenu.clientaccountnum)))
			mydatabase.commit()
			mydatabase.close_connection()

			mylogout=self.mymenu.logout()
			if (mylogout==False):
				return self.actions()
			elif (mylogout==True):
				return self.checkownnameandpass()
		elif (self.mymenu.action=='4b'):
			iuser=bankernamedic[self.mymenu.ownname]
			clientnewbal=iuser.transfer(float(self.mymenu.clientamount), self.mymenu.clientname, self.mymenu.otherclientname, int(self.mymenu.clientaccountnum), int(self.mymenu.clientaccountnum2))
			otherusernewbal=usernamedic[self.mymenu.otherclientname].accountsdic[int(self.mymenu.clientaccountnum2)].balance
			mydatabase.connect()
			mydatabase.cursor()
			mydatabase.curs.execute("UPDATE Client_Accounts SET Balance= ? WHERE Accountnumber=?",(clientnewbal, int(self.mymenu.clientaccountnum)))
			mydatabase.curs.execute("UPDATE Client_Accounts SET Balance= ? WHERE Accountnumber=?",(otherusernewbal, int(self.mymenu.clientaccountnum2)))
			mydatabase.commit()
			mydatabase.close_connection()

			mylogout=self.mymenu.logout()
			if (mylogout==False):
				return self.actions()
			elif (mylogout==True):
				return self.checkownnameandpass()
		elif (self.mymenu.action=='5b'):
			iuser=bankernamedic[self.mymenu.ownname]
			iuser.displayuseraccounts(self.mymenu.clientname)
			mylogout=self.mymenu.logout()
			if (mylogout==False):
				return self.actions()
			elif (mylogout==True):
				return self.checkownnameandpass()
		elif (self.mymenu.action=='1c'):
			iuser=usernamedic[self.mymenu.ownname]
			mynewbal=iuser.deposit(float(self.mymenu.amount), int(self.mymenu.accountnum))
			mydatabase.connect()
			mydatabase.cursor()
			mydatabase.curs.execute("UPDATE Client_Accounts SET Balance= ? WHERE Accountnumber=?",(mynewbal, int(self.mymenu.accountnum)))
			mydatabase.commit()
			mydatabase.close_connection()

			mylogout=self.mymenu.logout()
			if (mylogout==False):
				return self.actions()
			elif (mylogout==True):
				return self.checkownnameandpass()
		elif(self.mymenu.action=='2c'):
		 	iuser=usernamedic[self.mymenu.ownname]
		 	mynewbal=iuser.withdraw(float(self.mymenu.amount), int(self.mymenu.accountnum))
		 	mydatabase.connect()
		 	mydatabase.cursor()
		 	mydatabase.curs.execute("UPDATE Client_Accounts SET Balance=? WHERE Accountnumber=?",(mynewbal, int(self.mymenu.accountnum)))
		 	mydatabase.commit()
		 	mydatabase.close_connection()
		 	mylogout=self.mymenu.logout()
		 	if (mylogout==False):
		 		return self.actions()
		 	elif (mylogout==True):
		 		return self.checkownnameandpass()
			
		elif(self.mymenu.action=='3c'):
			iuser=usernamedic[self.mymenu.ownname]
			mynewbal=iuser.transfer( float(self.mymenu.amount),self.mymenu.otherclientname , int(self.mymenu.accountnum),int(self.mymenu.accountnum2))
			otherusernewbal=usernamedic[self.mymenu.otherclientname].accountsdic[int(self.mymenu.accountnum2)].balance
			mydatabase.connect()
			mydatabase.cursor()
			mydatabase.curs.execute("UPDATE Client_Accounts SET Balance= ? WHERE Accountnumber=?",(mynewbal, int(self.mymenu.accountnum)))
			mydatabase.curs.execute("UPDATE Client_Accounts SET Balance= ? WHERE Accountnumber=?",(otherusernewbal, int(self.mymenu.accountnum2)))
			mydatabase.commit()
			mydatabase.close_connection()

			mylogout=self.mymenu.logout()
			if (mylogout==False):
				return self.actions()
			elif (mylogout==True):
				return self.checkownnameandpass()

		elif (self.mymenu.action=='4c'):
			iuser=usernamedic[self.mymenu.ownname]
			iuser.displayaccounts()
			mylogout=self.mymenu.logout()
			if (mylogout==False):
				return self.actions()
			elif (mylogout==True):
				return self.checkownnameandpass()
Ejemplo n.º 2
0
class Banker(User):

	def __init__(self, username, password):
		self.permitlev="high"
		self.username=username
		
		self.password=password
		self.time_created=datetime.datetime.now()
		self.mycsv=CSV('clientlist')



	def createaccount(self, atype,begbalance, myusername, mypassword, myaccountnum):
		countfound=0
		self.mycsv.openreader()
		with self.mycsv.file:
			self.mycsv.dictreader()
			for row in self.mycsv.reader:
				if (myusername==row['Username'] ):
					countfound+=1
			if (countfound>0):
				print ("this client name already exists in our system, please try to log in as an existing client!")
				
			elif (countfound==0):
				userlist.append(Client(myusername, mypassword))
				global usernum
				usernamedic[myusername]=userlist[usernum]
				
				self.mycsv.openwriter()
				with self.mycsv.file:
					self.mycsv.dictwriter()
					self.mycsv.writer.writerow({'Username':myusername, 'Password':mypassword})
					self.mycsv.close()
					usernamedic[myusername].accountslist.append(Account(begbalance,atype, myaccountnum))
					myaccountindex=usernamedic[myusername].accountindexnum

					usernamedic[myusername].accountsdic[myaccountnum]=userlist[usernum].accountslist[myaccountindex]
					usernum+=1
					usernamedic[myusername].accountindexnum+=1
				
				print ("your new client account has been successfully created, please log in as an existing user now. your username is "+myusername)	
					

		


	def deposit(self, amount, username, myaccountnum):
	
		usernamedic[username].accountsdic[myaccountnum].balance+=amount
		print ("deposit completed- your new balance for this account is : "+str(usernamedic[username].accountsdic[myaccountnum].balance))
		return usernamedic[username].accountsdic[myaccountnum].balance
	

	def withdraw(self, amount, username, myaccountnum):
		
		if (amount>usernamedic[username].accountsdic[myaccountnum].balance):
			print ("withdraw cannot be completed- not enough balance")
		elif (amount<=usernamedic[username].accountsdic[myaccountnum].balance):
			usernamedic[username].accountsdic[myaccountnum].balance-=amount
			print ("withdraw completed- your new balance for this account is : "+str(usernamedic[username].accountsdic[myaccountnum].balance))
			return usernamedic[username].accountsdic[myaccountnum].balance
	def transfer(self, amount, fromusername, tousername, fromaccountnum, toaccountnum):
	
		
		if (amount>usernamedic[fromusername].accountsdic[fromaccountnum].balance):
			print ("transfer cannot be completed- not enough balance")
		elif (amount<=usernamedic[fromusername].accountsdic[fromaccountnum].balance):
			usernamedic[fromusername].accountsdic[fromaccountnum].balance-=amount
			usernamedic[tousername].accountsdic[toaccountnum].balance+=amount
			print ("transfer completed- your new balance for the account number "+str(fromaccountnum)+ " is : "+str(usernamedic[fromusername].accountsdic[fromaccountnum].balance))
			print ("your new balance for the account number "+str(toaccountnum)+ " is : "+str(usernamedic[tousername].accountsdic[toaccountnum].balance))
			return usernamedic[fromusername].accountsdic[fromaccountnum].balance
	def displayuseraccounts(self, username):
		for key in usernamedic[username].accountsdic:
			print ("for client username "+usernamedic[username].username+" account type " +usernamedic[username].accountsdic[key].type+" account with account number : "+str(usernamedic[username].accountsdic[key].accountnum))
			print ("the balance for this account is : " + str(usernamedic[username].accountsdic[key].balance))