-
Notifications
You must be signed in to change notification settings - Fork 1
/
NEW* bank account
209 lines (169 loc) · 6.82 KB
/
NEW* bank account
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#
# This program creates a bank account object that has overdraft fees,
# interest rate, first and last name requirements, unique account numbers,
# transaction lists, overdraft counter, and a balance.
#
# Date Created: 3/18/2023
#
# Created by Kolby Gibson and Caleb Coleman
#
#
# import transaction class
from transaction import Transaction
# BankAccount class
class BankAccount :
# Define any class variables or constants
_nextAccount = 1000 # A class variable that hold the number of the next account
OVERDRAFT_FEE = 20.00
INTEREST_RATE = 0.075
# Constructs a bank account with a given balance.
# @param initialBalance the initial account balance (default = 0.0)
#
def __init__(self, firstName = "", lastName = "", initialBalance = 0.0) :
self._accountNum = BankAccount._nextAccount # The account number
BankAccount._nextAccount = BankAccount._nextAccount + 1
if(firstName == ""):
self.setFirst() # The first name associate with the account
else:
# checks if valid length of the name
if(len(firstName) > 25):
self.setFirst()
# checks for special characters in name
specChars = '[@_!#$%^&*()<>?/\|}{"~:]'
for char in specChars:
if(char in firstName):
self.setFirst()
self._firstName = firstName
if(lastName == ""):
self.setLast() # The last name associate with the account
else:
# checks if valid length of the name
if(len(lastName) > 40):
self.setLast()
# checks for special characters in name
specChars = '[@_!#$%^&*()<>?/\|}{"~:]'
for char in specChars:
if(char in lastName):
self.setLast()
self._lastName = lastName
self._tList = [] # The list of transactions
self._tList.append(Transaction(amount = initialBalance, tType = "Deposit"))
print("Initial account balance is $%.2f" % initialBalance)
# ***** handles the overdrawn count of the account *****
overdraw = 0
self._overdrawNum = overdraw
# Define the Accessor Methods
# Gets the current balance of this account.
# @return the current balance
#
def getBalance(self) :
return sum(self._tList)
# Gets the current first name of the account
def getFirstName(self):
return self._firstName
# Gets the current last name of the account
def getLastName(self):
return self._lastName
# Gets the current overdraft number for this account.
# @return current overdraft number
def getOverdraftNum(self):
return(self._overdrawNum)
# Method that prints the transactions in this account.
#
def printAccount(self) :
print("The account information this account: ")
print("Account Holder is %s %s" %(self._firstName, self._lastName))
print("Account number is %d" %(self._accountNum))
print("Balance is %.2f" %(self.getBalance()) )
return
# Method that prints the transactions in this account.
#
def printTransactions(self) :
print("The current transactions for this account: ")
for element in self._tList:
element.printTransaction()
return
# Define the Mutator Methods
# Deposits money into this account.
# @param amount the amount to deposit
#
def deposit(self, amount) :
self._tList.append(Transaction(amount = amount, tType = "Deposit"))
print("Deposit of $%.2f, Current account balance is $%.2f" % (amount, sum(self._tList)))
return
# Makes a withdrawal from this account, or charges a penalty if
# sufficient funds are not available.
# @param amount the amount of the withdrawal
#
def withdraw(self, amount) :
# if withdrawal is more than balance + 250, deny transaction
if amount > (sum(self._tList) + 250.0):
print("Transaction denied")
return False
# if withdrawal is more than balance, overdraft penalty
elif (amount > sum(self._tList)) :
self._overdrawNum = self._overdrawNum + 1
self._tList.append(Transaction(amount = -self.OVERDRAFT_FEE, tType = "Penalty"))
print("Attempt to overdraw account, penalty of %.2f " % self.OVERDRAFT_FEE, end = "")
return False
# withdrawal from account
else :
self._tList.append(Transaction(amount = -amount, tType = "Withdrawl"))
print("withdraw of $%.2f " % amount, end = "")
return True
# print("Current account balance is $%.2f" % sum(self._tList))
# Makes a transfer from one bank account to another
# @param otherAccount other bank account money will be transfered from
# @param amount the amount of the transfer
#
def transfer(self, otherAccount, amount):
# attempt withdrawal from other onject, transfer is made if true
if(otherAccount.withdraw(amount) == True):
self._tList.append(Transaction(amount = amount, tType = "Transfer"))
# Adds interest to this account.
# @param rate the interest rate as a percent (e.g., 5.5%)
#
def addInterest(self) :
amount = round(sum(self._tList) * self.INTEREST_RATE, 2)
self._tList.append(Transaction(amount = amount, tType = "Interest"))
print("Interest of $%.2f added, Current account balance is $%.2f" % (amount, sum(self._tList)))
# Mutator method that sets the first name for the account holder
def setFirst(self):
firstName = input("Enter the first name of the account: ")
if(firstName == ""):
self.setFirst()
# if it is given check the name for format
else:
# checks if valid length of the name
if(len(firstName) > 25):
self.setFirst()
# checks for special characters in name
specChars = '[@_!#$%^&*()<>?/\|}{"~:]'
for char in specChars:
if(char in firstName):
self.setFirst()
self._firstName = firstName
return
# Mutator method that sets the last name for the account holder
def setLast(self):
lastName = input("Enter the last name of the account: ")
if(lastName == ""):
self.setLast()
else:
# checks if valid length of the name
if(len(lastName) > 40):
self.setLast()
# checks for special characters in name
specChars = '[@_!#$%^&*()<>?/\|}{"~:]'
for char in specChars:
if(char in lastName):
self.setLast()
self._lastName = lastName
return
# Prints all of the transaction instance variables.
# @return: The formatted, human readable string of the transaction
def __str__ (self):
transaction = []
for item in self._tList:
transaction.append(item)
return transaction