Write a set of Python classes that can simulate a messaging application in which one party, Ali, is randomly creating a set of messages that he wants to send to Veli.


 


Write a set of Python classes that can simulate a messaging application in which
one party, Ali, is randomly creating a set of messages that he wants to send to
Veli. A Server process is continually checking if Ali has any messages to send,
and if so, it delivers them to Veli’s phone, and Veli is periodically checking if his
phone has a message from Ali, and, if so, he reads, displays and answers it.
 

Hint: Use three different classes, for Ali, Veli and Server, and provide methods
that perform their various tasks, as well as a simulator engine that performs the
random messaging

 

 


import random
#SAMET SARIAL - 64170037 - COE - QUESTION 2 - HOMEWORK3

class Ali:
    def __init__(self, msgSent): #init function describing which parameter represent first run
        self.messageSent = msgSent

    def sendMessage(self):
        print("[Ali] Sending message: "+str(self.messageSent))


class Veli:
    def __init__(self, msgAvailable, msgReceived):#init function describing which parameter represent first run
        self.messageReceived = msgReceived
        self.messageAvailable = msgAvailable

    def infoSection(self): #checking message recived and readed by veli and this prints this
        if self.messageAvailable == True:
            print("[Veli] Message received: "+str(self.messageReceived))
            print("[Veli] Message has been read.")
            self.messageReceived = ""
        else:
            print("[Veli] Message is unavailable")


class Server: #A Server process is continually checking if Ali has any messages to send
    def __init__(self, msgSent):
        if msgSent == "":
            self.messageAvailable = False
        else:
            self.messageAvailable = True
            self.messageProgress = msgSent

    def getMessageAvailablity(self):#Server getMessageAvailablity fuctions shows for veli message is avaliable or not 
        print("[Veli] Message Availability: "+str(self.messageAvailable))
        return (self.messageAvailable)


numberofmessages = random.randint(0, 5)  # returns a number between 0 and 5
# this section briefly connect classes and functions each other and all process doing in here
for i in range(numberofmessages):
    inputMessage = random.choice(["This is message "+str(i+1), ""])
    aliObj = Ali(inputMessage)# creating aliobj
    aliObj.sendMessage()#sending message with ali
    serverObj = Server(aliObj.messageSent)#representing server object
    if (serverObj.getMessageAvailablity() == True ):#this is checking message avaliablity if there is message this if will run else break
        veliobj = Veli(serverObj.messageAvailable, serverObj.messageProgress)
        veliobj.infoSection()
    else:
        break

Yorum Gönderme

0 Yorumlar