...

/

Solution: Implementing Library Analytics

Solution: Implementing Library Analytics

Here’s the solution to the library analytics challenge.

Let’s discuss the solution for implementing library analytics.

Create the BookAnalytics class

First, we create the BookAnalytics class in the com.smartdiscover.model package.

Press + to interact
package com.smartdiscover.model;
import lombok.Data;
import org.springframework.data.cassandra.core.mapping.Column;
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
import org.springframework.data.cassandra.core.mapping.Table;
import java.util.UUID;
@Data
@Table
public class BookAnalytics {
@PrimaryKey
private UUID bookId;
@Column
private String bookName;
@Column
private long borrowedCount;
@Column
private long viewedCount;
@Column
private double averageRating;
@Column
private long totalRatings;
@Override
public String toString() {
return "BookAnalytics{" +
"bookId=" + bookId +
", bookName='" + bookName + '\'' +
", borrowedCount='" + borrowedCount + '\'' +
", viewedCount='" + viewedCount + '\'' +
", averageRating='" + averageRating + '\'' +
", totalRatings='" + totalRatings + '\'' +
'}';
}
public BookAnalytics(UUID bookId, String bookName) {
this.bookId = bookId;
this.bookName = bookName;
this.borrowedCount = 0;
this.viewedCount = 0L;
this.averageRating = 0D;
}
public void incrementBorrowedCount() {
this.borrowedCount += 1;
}
public void incrementViewedCount() {
this.viewedCount += 1;
}
public void incrementTotalRatings() {
this.totalRatings += 1;
}
}

Code explanation:

  • Lines 10–12: We add annotations like @Data and @Table to refer to a Cassandra table from the POJO.

  • Line 14: We add the bookId property as the primary key using the @PrimaryKey annotation.

  • Lines 17–30: We add properties like bookName, borrowedCount, viewedCount, averageRating, and totalRatings and annotate them ...