In: Computer Science
In this problem, you will need to create a state machine that stimulates a simple bank account in Python 3.6. Any withdrawal when the balance is less than $100 incurs a $5 charge. The state machine should fulfill the following:
• The starting balance is specified when instantiating the object.
• The output of the state machine is the current balance after the transaction.
Sample interaction:
>>> acct = SimpleAccount (110)
>>> acct . start ()
>>> acct . step (10)
120
>>> acct . step ( -25)
95
>>> acct . step ( -10)
80
>>> acct . step ( -5)
70
>>> acct . step (20)
90
>>> acct . step (20) 110
Code with indenatation:
Output:
____________________________________________________________________________________
Code:
import threading
class SimpleAccount(threading.Thread):
balance=0
def __init__(self,b):
threading.Thread.__init__(self) #handling thread
self.balance = b #setting value
def step(self,val):
if(val<0): #val is negative
if(self.balance >100): #if bal greater than 100
self.balance=self.balance+val
print(self.balance)
else:
self.balance=self.balance+(val-5) #bal is less than 100 then -5
extra
print(self.balance)
else: #if val is postive
self.balance=self.balance+val #adding value
print(self.balance)
acct=SimpleAccount(110)
acct.start()
acct.step(10)
acct.step(-25)
acct.step(-10)
acct.step(-5)
acct.step(20)
acct.step(20)
_________________________________________________________________________________________
NOTE: You can add many threads simultaneously..