Answer to Question #198527 in Python for Andu

Question #198527

Project 5: Social Media account

Implement a program that manages the data of the social media account. The user can access only his/her own account. The account is characterized by username, password, name, sumame, birth date and list of friends (characterised by the account username). The data are stored into a json file. The application should manage the following characteristics:

Sign up: register to the system

Sign in: by providing usename and password and showing all the user data

Search for a user (using name and surname)

Add a used to the friend list

Changes need to be stored back in the file. The above functionalities are implemented through methods of a class named SocialMedia. The application interacts with the user through a menu where the user is asked for the next action to undertake (e.g., 1-Sign Up, 2

Sign In, etc.) Put comments in your code.


1
Expert's answer
2021-05-28T02:56:48-0400
import pathlib
import json


class Account:
    #Constructor
    def __init__(self):
        self.username =""
        self.password =""
        self.name =""
        self.suname =""
        self.birthDate =""
        self.listFriends = [] #(characterised by the account username)
            
    #This function allows to create a new Account
    def signUp(self):
        self.username =input("Enter the username: ")
        self.password =input("Enter the password: ")
        self.name =input("Enter the name: ")
        self.suname =input("Enter the suname: ")
        self.birthDate =input("Enter the birthdate: ")
        print("\nYou have successfully registered\n")
        
    #This function allows to set account information
    def setAccountInformation(self,username,password,name,suname,birthDate,listFriends):
        self.username=username
        self.password=password
        self.name=name
        self.suname=suname
        self.birthDate=birthDate
        self.listFriends=listFriends
        
    #This function allows to display Account Information
    def displayAccountInformation(self):
        print(f"The username : {self.username}")
        print(f"The password: {self.password}")
        print(f"The name: {self.name}")
        print(f"The suname: {self.suname}")
        print(f"The birth date: {self.birthDate}")
        
        print("Friends: ")
        for friend in self.listFriends:
            print(friend)


    #This function allows to get username
    def getUsername(self):
        return self.username
    #This function allows to get password
    def getPassword(self):
        return self.password
    #This function allows to get name
    def getName(self):
        return self.name
    #This function allows to get suname
    def getSuname(self):
        return self.suname
    #This function allows to add a new friend
    def getAddNewFriend(self,friendUsername):
        self.listFriends.append(friendUsername)


    
 
def main():
    #File name
    FILE_NAME='accounts.json'
    choice=""
    #list of Accounts
    accounts=[]
    readAccounts(accounts,FILE_NAME)
    currentAccount=None
    while choice!="5":
        print("1 - Sign up: register to the system")
        print("2 - Sign in")
        print("3 - Search for a user")
        print("4 - Add a user to the friend list")
        print("5 - Log out")
        choice=input("Your choice: ")
        if(choice=="1"):#Sign up: register to the system
            newAccount=Account()
            newAccount.signUp()
            accounts.append(newAccount)
            saveAccounts(accounts,FILE_NAME)
        elif(choice=="2"):#Sign in
            username =input("Enter the username of the account: ")
            password =input("Enter the password of account: ")
            
            accountExist=False
            for acc in accounts:
                if acc.getUsername()==username and acc.getPassword()==password:
                    acc.displayAccountInformation()
                    currentAccount=acc
                    print("")
                    accountExist=True
                    break
            if accountExist==False:
                print("\nThe account does not exist.\n")
                    
        elif(choice=="3"):#Search for a user
            name =input("Enter the name of account: ")
            surname =input("Enter the surname of the account: ")
            
            accountExist=False
            for acc in accounts:
                if acc.getName()==name and acc.getSuname()==surname:
                    acc.displayAccountInformation()
                    print("")
                    accountExist=True
            if accountExist==False:
                print("\nThe account does not exist.\n")
        elif(choice=="4"): #Add a used to the friend list
            if currentAccount==None:
                print("\nYou need to sign in!\n")
            else:
                username =input("Enter the username of the account you want to add to your friend list: ")
                accountExist=False
                if currentAccount.getUsername()!=username:
                    for acc in accounts:
                        if acc.getUsername()==username:
                            currentAccount.getAddNewFriend(username)
                            print("\nThe account has been added to the friend list.\n")
                            print("")
                            accountExist=True
                            saveAccounts(accounts,FILE_NAME)
                            break
                    if accountExist==False:
                        print("\nThe account does not exist.\n")
                else:
                    print("\nYou can't add youself to the friend list.\n")
            
        elif(choice=="5"): #Log out
            pass
        else:
            print("\nSelect a correct menu item.\n")
#This function allows to save all accounts to the json file
def saveAccounts(accounts,fileName):
    #convert the object to a dictionary
    accountsDictionary = [account.__dict__ for account in accounts]
    with open(fileName, "w") as accountsFile:
        json.dump(accountsDictionary, accountsFile)
#This function allows to read all accounts from the json file
def readAccounts(accounts,fileName):
    pathlibPath = pathlib.Path(fileName)
    if pathlibPath.exists():
        with open(fileName,'r+') as file:
            accountData = json.load(file)
            for account in accountData:
                accountFromFile=Account()
                accountFromFile.setAccountInformation(account['username'],account['password'],account['name'],account['suname'],account['birthDate'],account['listFriends'])
                accounts.append(accountFromFile)
                
               
        
    
main()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS