Final answer:
The getLikes() function returns the number of likes from a user for a post by a given author, validates the existence of users and posts, and is case-sensitive for usernames.
Step-by-step explanation:
The getLikes() function you mentioned would be used to track user interactions in a social media context by returning the number of likes a specific user has given to a post from a particular author. The function should consider several cases. If the user or the post author doesn't exist in the dataset, the function should return -3. If there are no users or posts, indicated by having zero or a negative number of users or posts stored, the function should return -2. Otherwise, the function should search through the array of Post objects to find the first post by the specified author and then return the number of likes that specific user has given to that post.
Here is how the logic should be implemented:
- Verify that the number of posts and users stored are greater than 0.
- Search for a post by the given author.
- If the post is found, search for the user in the array of User objects.
- If both the post and user are found, return the number of likes the user has given to the post.
- Otherwise, return the appropriate error code.
The function is designed to be case-sensitive, thus different capitalizations would indicate different users.