ATM Socket Program in Python EDIT Code two separate programs
ATM Socket Program in Python EDIT:
Code two separate programs: a client and a server. The server balances and does all updates, i.e. deposits and withdrawals. The server will also respond when asked about the account balance.
A user sends commands through the client, able to deposit money into their account, withdraw money from the same account, and check the balance of the account. No calculations are done via the client; the client only sends requests to the server, which executes whatever the request is and returns the answer to the client.
The balance should be initialized to an X amount by the server. Assume that a single account will be accessed by a single user.
Note: this must be coded in Python and must have the client/server socket programming as detailed above.
Example Code:
#!/usr/bin/python # This is server.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
print (\'Got connection from\',addr)
data = c.recv(1024)
if(data == b\'4\'): exit()
else if(data == b\'3\'): #write your code to print account details print(AccBal)
else if(data == b\'2\'): #write your code to deposit money
else if(data == b\'1\'): #write your code to withdraw money
c.send(bytes(\'Thank you for connecting\',\'utf8\'))
c.close() # Close the connection
#!/usr/bin/python # This is client.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.connect((host, port))
x = input(\'Enter your choice \ 1. Withdrawal\ 2. Deposit\ 3. Check the account\ 4. Exit\');
s.send(bytes(x,\'utf8\'));
print (s.recv(1024))
s.close # Close the socket when done
Solution
1) This is server.py file which initializes the fixed value accBal=1000 which is the minimum amount a bank account must maintain. Then this method constructs an object of bankaccount and calls three methods withdrawl, deposit and check the balance. Since there was no value for the deposit and withdrawl amount given, I assumed it as 100.
from MinimumBalanceAccount import MinimumBalanceAccount
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
accBal=1000
acc=MinimumBalanceAccount(accBal)
print (\'Got connection from\',addr)
data = c.recv(1024)
if(data == b\'4\'): exit()
elif(data == b\'3\'): #write your code to print account details print(AccBal)
c.send(c.balance())
elif(data == b\'2\'): #write your code to deposit money
c.send(c.deposit(100))
elif(data == b\'1\'): #write your code to withdraw money
c.send(c.withdrawl(100))
c.send(bytes(\'Thank you for connecting\',\'utf8\'))
c.close()
2) create a seprate child classs MinimumBalanceAccount which inherits BankAccount
from BalanceAccount import BankAccount
class MinimumBalanceAccount(BankAccount):
def __init__(self, minimum_balance):
BankAccount.__init__(self)
self.minimum_balance = minimum_balance
def withdraw(self, amount):
if self.balance - amount < self.minimum_balance:
print \'Sorry, minimum balance must be maintained.\'
else:
BankAccount.withdraw(self, amount)
3) The BankAccount method is a generic class in this case which has three methods namely withdraw,deposit and balance.
class BankAccount:
def __init__(self):
self.balance = 0
def withdraw(self, amount):
self.balance -= amount
return self.balance
def deposit(self, amount):
self.balance += amount
return self.balance
def balance(self):
return self.balance()
4) This is final client method which is exactly as given in the question.
#!/usr/bin/python # This is client.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.connect((host, port))
x = input(\'Enter your choice \ 1. Withdrawal\ 2. Deposit\ 3. Check the account\ 4. Exit\');
s.send(bytes(x,\'utf8\'));
print (s.recv(1024))
s.close # Close the socket when done


