Record users who like it


describe:

In order to implement the community like function, it is required to design a like recorder. This tool contains the following two methods:

1. Like method: This method needs to pass in the user name as a parameter. If the user has not liked it, the like behavior will be recorded. If the user has already liked it, his like behavior will be deleted.

2. getLikeUsers method: This method needs to return the names of all like users, and the order is not required.

(To ensure the correct answer, please use HashSet to complete this question)


Enter description:

username 


Output description: 

 All usernames that have liked and not unliked, no order required. (The output is in the form of Arrays.toString)


Example:


enter:

Tom Jim Lucy Lily Tom Lucy Tom

Output:

[Tom, Lily, Jim]

 

  • Since this question is about recording the list of users who liked it, a collection should be selected to store the users. First, analyze the business requirements to choose which set to use. Under this business requirement, the order is not important, but the names should not be repeated, so it is best to choose a Set collection with fast traversal speed and insertion and deletion speed to store like users and set memory. The data type is String, and the code reference writing method is: private HashSet names = new HashSet();

  • According to the preset code, the part that needs to be completed is the implementation class of the LikeRecorder interface, so the methods in the interface should be rewritten here. In the like method, the user name is passed in as a parameter, and the if statement is used to determine whether the user name is included in the collection. If it is included, the user name will be removed. If it is not included, the user name will be added to the collection. To determine whether the object exists in the collection, you can use the contains method.

The reference writing method of this part of the code is:

@Override
public void like(String name) {
  if (names.contains(name)) {
      names.remove(name);
  } else {
      names.add(name);
  }
}

 

In the default code, the output format is Arrays.toString, so we should set the return value of the getLikeUsers method to the array type. Then the getLikeUsers() method should convert the HashSet collection into an array, so here we can call the toArray method of the HashSet collection. 

 The code reference writing method is as follows:

  @Override
public String[] getLikeUsers() {
    return names.toArray(new String[0]);
}

Complete code:

import java.util.*;

public class Main {

    public static void main(String[] args) {
        LikeRecorder recorder = new LikeRecorderImpl();

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String name = scanner.next();
            recorder.like(name);
        }

        System.out.println(Arrays.toString(recorder.getLikeUsers()));
    }

}

/**
 * 点赞记录器
 */
interface LikeRecorder {

    /**
     * 若用户没有点赞过,则记录此次点赞行为。
     * 若用户曾经点赞过,则删除用户点赞记录。
     *
     * @param username 用户名
     */
    void like(String username);

    /**
     * 返回所有点赞的用户名
     *
     * @return 用户名数组
     */
    String[] getLikeUsers();

}

class LikeRecorderImpl implements LikeRecorder {
    private HashSet<String> names = new HashSet();
    public String[] getLikeUsers(){
        String[] str= new String[names.size()];
        return names.toArray(str);
    }
    public void like(String username){
        if(names.contains(username)){
            names.remove(username);
        }
        else{
            names.add(username);
        }
    }
    

}

 

Guess you like

Origin blog.csdn.net/m0_64799972/article/details/125622263