Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

week 3 submission #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions week0003/vietthaotran/python/week0003.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import sys

class Client (object):
def __init__(self, name, amount):
self.name = name
self.amount = amount

def deposit(self, amount):
if amount > 0:
self.amount += amount
return 'True'
else:
return 'False'

def withdraw(self, amount):
if 0 < amount <= self.amount:
self.amount -= amount
return 'True'
else:
return 'False'

class FinTech (object):
def __init__(self):
self.clients = {}

def addClient (self, name, amount):
if name in self.clients:
return 'False'
else:
client = Client(name, amount)
self.clients[name] = client
return 'True'

def deposit(self, name, amount):
if name in self.clients:
client = self.clients[name]
return client.deposit(amount)
else:
return 'False'

def withdraw(self, name, amount):
if name in self.clients:
client = self.clients[name]
return client.withdraw(amount)
else:
return 'False'

def processRequest(self, input):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should split this method from the class and treat it as an independent function/method in another class. This will increase reusability of both the class & the function itself. The FinTech class should know nothing about STDIN/input. It should have only single responsibility of managing clients and talking to Client objects

for line in input:
request = line.split(" ")
action = request[0]
name = request[1].lower()
amount = float(request[2])
if amount > 0:
if action == 'A':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid magic strings. Use ACTION_ADD_CLIENT = 'A' instead

print self.addClient(name, amount)
elif action == 'D':
print self.deposit(name, amount)
elif action == 'W':
print self.withdraw(name, amount)
else:
print 'Invalid'
else:
print 'Invalid'


if __name__ == '__main__':
finTech = FinTech()
finTech.processRequest(sys.stdin)