Unit Testing
Nama : Hafidz Panji Ashari
NRP : 5025231278
Kelas : PBO-A
Kelas PhoneProduct
import java.util.ArrayList;
import java.util.Comparator;
public class PhoneProduct {
private String brand; // Nama merk
private String model; // Model handphone
private double price; // Harga dalam USD
private ArrayList<Review> reviews; // Daftar ulasan pengguna
// Constructor
public PhoneProduct(String brand, String model, double price) {
this.brand = brand;
this.model = model;
this.price = price;
this.reviews = new ArrayList<>();
}
// Getters dan Setters
public String getBrand() {
return brand;
}
public String getModel() {
return model;
}
public double getPrice() {
return price;
}
// Menambahkan ulasan pengguna
public boolean addReview(String username, String comment, int rating) {
if (rating < 1 || rating > 5) {
return false; // Rating harus valid
}
if (findReviewByUser(username) != null) {
return false; // Tidak boleh ada ulasan ganda dari user yang sama
}
reviews.add(new Review(username, comment, rating));
return true;
}
// Menghapus ulasan berdasarkan indeks
public void removeReview(int index) {
if (index >= 0 && index < reviews.size()) {
reviews.remove(index);
}
}
// Menampilkan informasi handphone dan ulasan
public void displayInfo() {
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
System.out.println("Price: $" + price);
System.out.println("User Reviews:");
for (Review review : reviews) {
System.out.println("------------------------------");
System.out.println(review.getDetails());
}
}
// Menghitung rata-rata rating
public double getAverageRating() {
if (reviews.isEmpty()) {
return 0.0;
}
double totalRating = 0;
for (Review review : reviews) {
totalRating += review.getRating();
}
return totalRating / reviews.size();
}
// Menemukan ulasan terbaik berdasarkan rating
public Review getBestReview() {
return reviews.stream()
.max(Comparator.comparingInt(Review::getRating))
.orElse(null);
}
// Helper untuk mencari ulasan berdasarkan username
private Review findReviewByUser(String username) {
for (Review review : reviews) {
if (review.getUsername().equalsIgnoreCase(username)) {
return review;
}
}
return null;
}
}
Kelas Review
public class Review {
private String username; // Nama pengguna
private String comment; // Ulasan
private int rating; // Rating (1-5)
public Review(String username, String comment, int rating) {
this.username = username;
this.comment = comment;
this.rating = rating;
}
public String getUsername() {
return username;
}
public String getComment() {
return comment;
}
public int getRating() {
return rating;
}
// Detail ulasan
public String getDetails() {
return "User: " + username + "\nRating: " + rating + "/5\nComment: " + comment;
}
}
Unit Testing dengan JUnit
Tambahkan Dependency JUnit
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.3</version>
<scope>test</scope>
</dependency>
Kelas Test untuk PhoneProduct
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class PhoneProductTest {
@Test
public void testAddReview() {
PhoneProduct phone = new PhoneProduct("Samsung", "Galaxy S23", 999.99);
assertTrue(phone.addReview("user1", "Great phone!", 5));
assertFalse(phone.addReview("user1", "Duplicate review", 4));
assertFalse(phone.addReview("user2", "Invalid rating", 6));
}
@Test
public void testGetAverageRating() {
PhoneProduct phone = new PhoneProduct("Apple", "iPhone 14", 1099.99);
phone.addReview("user1", "Amazing phone", 5);
phone.addReview("user2", "Good but expensive", 4);
assertEquals(4.5, phone.getAverageRating());
}
@Test
public void testGetBestReview() {
PhoneProduct phone = new PhoneProduct("Xiaomi", "Redmi Note 12", 299.99);
phone.addReview("user1", "Great value", 4);
phone.addReview("user2", "Perfect phone for the price", 5);
Review bestReview = phone.getBestReview();
assertNotNull(bestReview);
assertEquals("user2", bestReview.getUsername());
assertEquals(5, bestReview.getRating());
}
}
Comments
Post a Comment