ramsess90

Zad 9

Feb 9th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. class User:
  2.     def __init__(self, Username):
  3.         self.Username = Username
  4.         self.ReceivedMessages = []
  5.  
  6.     def add_message(self, data, sender):
  7.         if sender in get_all_usernames():
  8.             sender = get_one_user(sender)
  9.             one_message = Message(data[3], sender)
  10.             self.ReceivedMessages.append(one_message)
  11.  
  12.  
  13. class Message:
  14.     def __init__(self, Content, Sender):
  15.         self.Content = Content
  16.         self.Sender = Sender
  17.  
  18.  
  19. def print_message(username_one, username_two):
  20.     if username_one not in get_all_usernames() or username_two not in get_all_usernames():
  21.         return
  22.  
  23.     # Get messages for user
  24.     all_messages_for_user_one = get_one_user(username_one).ReceivedMessages
  25.     all_messages_with_user_two = list(filter(lambda x: x.Sender.Username == username_two, all_messages_for_user_one))
  26.     all_messages_for_user_two = get_one_user(username_two).ReceivedMessages
  27.     all_messages_with_user_one = list(filter(lambda x: x.Sender.Username == username_one, all_messages_for_user_two))
  28.  
  29.     if len(all_messages_with_user_two) == 0 and len(all_messages_with_user_one) == 0:
  30.         print('No messages')
  31.  
  32.     i = -1
  33.     while True:
  34.         i += 1
  35.         if i >= len(all_messages_with_user_one) and i >= len(all_messages_with_user_two):
  36.             break
  37.         if i < len(all_messages_with_user_two):
  38.             print(f'{username_one}: {all_messages_with_user_two[i].Content}')
  39.         if i < len(all_messages_with_user_one):
  40.             print(f'{all_messages_with_user_one[i].Content} :{username_two}')
  41.  
  42.  
  43. def get_all_usernames():
  44.     return list(map(lambda x: x.Username, all_users))
  45.  
  46.  
  47. def get_one_user(searched_name):
  48.     user = list(filter(lambda x: x.Username == searched_name, all_users))
  49.     if len(user) != 0:
  50.         return user[0]
  51.     return -1
  52.  
  53.  
  54. all_users = []
  55. while True:
  56.     data = input().split()
  57.  
  58.     if data[0].lower() == 'exit':
  59.         data = input().split()
  60.         if len(data) == 2:
  61.             print_message(data[0], data[1])
  62.         break
  63.     if data[0].lower() == 'register':
  64.         one_username = User(data[1])
  65.         if one_username.Username not in get_all_usernames():
  66.             all_users.append(one_username)
  67.             get_all_usernames().append(one_username.Username)
  68.     elif data[1].lower() == 'send':
  69.         if data[0] in get_all_usernames() and data[2] in get_all_usernames():
  70.             get_one_user(data[0]).add_message(data, data[2])
Advertisement
Add Comment
Please, Sign In to add comment