WishList class

Report a typo

Imagine that you are a software engineer working on an online store that sells various products. Your store is top-rated and receives a high volume of traffic from customers worldwide.

To keep up with the demand, you have developed a distributed system that is able to scale horizontally across multiple servers. Each server runs a copy of your store's application and communicates with other servers through a message queue.

One of the features of your store is a wishlist that allows customers to save products that they are interested in purchasing later. Customers can add and remove products from their wishlists at any time, and the changes are reflected in real time on the store's website.

To implement this feature, complete the WishList class that we started for you. The WishList class stores a list of product IDs for each customer. It has a addProduct(int productId) method that allows a customer to add a product to their wish list and a removeProduct(int productId) method that allows a customer to remove a product from their wishlist. The parameter in both cases is the ID of the product that is added or removed from the list.

By using the Semaphore, make sure that the WishList class is thread-safe!

Only one thread should be able to modify the list at a time!

The input format is the following:

  1. The number of products added to the wish list.

  2. The number of products removed from the wishlist.

Output: The final size of the wishlist after the modifications. (Initially, the list is empty.)

Sample Input 1:

1000
1000

Sample Output 1:

0
Write a program in Java 17
class WishList {
private ArrayList<Integer> products; // list of product_ids

public WishList() {
this.products = new ArrayList<>();
}

public void addProduct(int product_id) {
//TODO: your code goes here
}

public void removeProduct(int product_id) {
//TODO: your code goes here
}

public int getSize() {
return products.size();
}
}
___

Create a free account to access the full topic