0% found this document useful (0 votes)
2 views3 pages

Chat System Code

The document contains code for a simple chat system implemented in Python, featuring classes for Message, User, and ChatRoom. Users can join or leave chatrooms, send messages, and view chat history. An example usage section demonstrates how to create a chatroom and interact with it using multiple users.

Uploaded by

mp9348371804
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Chat System Code

The document contains code for a simple chat system implemented in Python, featuring classes for Message, User, and ChatRoom. Users can join or leave chatrooms, send messages, and view chat history. An example usage section demonstrates how to create a chatroom and interact with it using multiple users.

Uploaded by

mp9348371804
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chat system - code

nirliptapanda515@[Link]
# -------------------------------
# Message Class
# -------------------------------
class Message:
message_counter = 1 # simple counter

def __init__(self, sender, content):


[Link] = sender
[Link] = content
[Link] = Message.message_counter
Message.message_counter += 1

def __str__(self):
return f"({[Link]}) {[Link]}: {[Link]}"

# -------------------------------
# User Class
# -------------------------------
class User:
def __init__(self, username):
[Link] = username
[Link] = None

def join_chatroom(self, chatroom):


if [Link]:
print(f"{[Link]} is already in a chatroom.")
else:
chatroom.add_user(self)
[Link] = chatroom
print(f"{[Link]} joined {[Link]}")

def leave_chatroom(self):
if not [Link]:

Chat system - code 1


print(f"{[Link]} is not in any chatroom.")
else:

nirliptapanda515@[Link]
[Link].remove_user(self)
print(f"{[Link]} left {[Link]}")
[Link] = None

def send_message(self, content):


if not [Link]:
print(f"{[Link]} cannot send a message (not in a chatroo
m).")
else:
[Link](self, content)

# -------------------------------
# ChatRoom Class
# -------------------------------
class ChatRoom:
def __init__(self, name):
[Link] = name
[Link] = []
[Link] = []

def add_user(self, user):


[Link](user)

def remove_user(self, user):


[Link](user)

def broadcast(self, sender, content):


message = Message(sender, content)
[Link](message)
print(message) # Show message to all users

def show_chat_history(self):
print(f"\nChat History of {[Link]}:")
for msg in [Link]:
print(msg)

Chat system - code 2


print()

nirliptapanda515@[Link]
# ---------------------------------------
# Example Usage
# ---------------------------------------
if __name__ == "__main__":
room = ChatRoom("Python Lounge")

u1 = User("Alice")
u2 = User("Bob")
u3 = User("Charlie")

u1.join_chatroom(room)
u2.join_chatroom(room)

u1.send_message("Hello everyone!")
u2.send_message("Hi Alice!")

u3.join_chatroom(room)
u3.send_message("Hey guys, what's up?")

room.show_chat_history()

u1.leave_chatroom()
u2.leave_chatroom()
u3.leave_chatroom()

Chat system - code 3

You might also like