Tugas Pertemuan 10 Pemrograman Berbasis Objek (PBO) A 2024

Nama : Amadeo Yesa

NRP : 5025231160

Kelas : A

Tahun : 2024


Pada pertemuan 10, dilakukan unit testing tentang sales item.


Class SalesItem

import java.util.ArrayList;
import java.util.Iterator;

public class SalesItem {
    private String name;
    private int price; // in cents private
    ArrayList<Comment> comments;

    public SalesItem(String name, int price) {
        this.name = name;
        this.price = price;
        comments = new ArrayList<Comment>();
    }

    public String getName() {
        return name;    
    }

    public int getPrice() {
        return price; 
    }

    public int getNumberOfComments() {
        return comments.size();
    }

    public boolean addComment(String author, String text, int rating) {
        if(ratingInvalid(rating)) {
            return false;
        }

        if(findCommentByAuthor(author) != null) {
            return false;
        }

        comments.add(new Comment(author, text, rating));
        return true; 
    }

    public void removeComment(int index) {
        if(index >=0 && index < comments.size()) {
            comments.remove(index);
        } 
    }

    public void upvoteComment(int index) {
        if(index >=0 && index < comments.size()) {
            comments.get(index).upvote();
        } 
    }

    public void downvoteComment(int index) {
        if(index >=0 && index < comments.size()) {
            comments.get(index).downvote();
        } 
    }

    public void showInfo() {
        System.out.println("*** " + name + " ***"); 
        System.out.println("Price: " + priceString(price));
        System.out.println();
        System.out.println("Customer comments:"); 
        for(Comment comment : comments) {
            System.out.println("-----------------------------------"); 
            System.out.println(comment.getFullDetails());
        }
        System.out.println(); 
        System.out.println("=====================================");
    }

    public Comment findMostHelpfulComment() {
        Iterator<Comment> it = comments.iterator();
        Comment best = it.next();

        while(it.hasNext()) {
            Comment current = it.next(); 
            if(current.getVoteCount() > best.getVoteCount()) {
                best = current;
            }
        }
        
        return best;
    }

    private boolean ratingInvalid(int rating) {
        return rating < 0 || rating > 5; 
    }

    public Comment findCommentByAuthor(String author) {
        for(Comment comment : comments) { 
            if(comment.getAuthor().equals(author)) {
                return comment; 
            }
        }

        return null; 
    }

    private String priceString(int price) {
        int dollars = price / 100;
        int cents = price - (dollars*100);
        if(cents <= 9) {
            return "$" + dollars + ".0" + cents; // zero padding
        }
        else {
            return "$" + dollars + "." + cents; 
        }
    }
}

Class Comment
public class Comment {
    private String author;
    private String text;
    private int rating;
    private int voteCount;

    public Comment(String author, String text, int rating) {
        this.author = author;
        this.text = text;
        this.rating = Math.max(0, Math.min(rating, 5));
        this.voteCount = 0;
    }

    public String getAuthor() {
        return author;
    }

    public String getText() {
        return text;
    }

    public int getRating() {
        return rating;
    }

    public int getVoteCount() {
        return voteCount;
    }

    public void upvote() {
        voteCount++;
    }

    public void downvote() {
        voteCount--;
    }

    public String getFullDetails() {
        return "Author: " + author + "\n" +
               "Rating: " + rating + "/5\n" +
               "Votes: " + voteCount + "\n" +
               "Comment: " + text;
    }
}

SalesItemTest
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class SalesItemTest {

    @Test
    public void testConstructorAndGetters() {
        SalesItem item = new SalesItem("Laptop", 99999);

        assertEquals("Laptop", item.getName());
        assertEquals(99999, item.getPrice());
        assertEquals(0, item.getNumberOfComments());
    }

    @Test
    public void testAddCommentValid() {
        SalesItem item = new SalesItem("Phone", 49999);

        assertTrue(item.addComment("Alice", "Great product!", 5));
        assertEquals(1, item.getNumberOfComments());
    }

    @Test
    public void testAddCommentDuplicateAuthor() {
        SalesItem item = new SalesItem("Tablet", 29999);

        assertTrue(item.addComment("Bob", "Nice screen!", 4));
        assertFalse(item.addComment("Bob", "Updated review", 3)); // Duplicate author not allowed
    }

    @Test
    public void testAddCommentInvalidRating() {
        SalesItem item = new SalesItem("Camera", 19999);

        assertFalse(item.addComment("Charlie", "Bad rating!", -1)); // Invalid rating
        assertFalse(item.addComment("Diana", "Too high rating!", 6)); // Invalid rating
    }

    @Test
    public void testRemoveComment() {
        SalesItem item = new SalesItem("Monitor", 15999);

        item.addComment("Alice", "Very clear!", 5);
        item.addComment("Bob", "Sleek design!", 4);

        assertEquals(2, item.getNumberOfComments());
        item.removeComment(0); // Remove the first comment
        assertEquals(1, item.getNumberOfComments());
    }

    @Test
    public void testUpvoteDownvoteComment() {
        SalesItem item = new SalesItem("Keyboard", 4999);

        item.addComment("Alice", "Great keys!", 5);
        item.addComment("Bob", "Sturdy build!", 4);

        item.upvoteComment(0);
        item.upvoteComment(0);
        item.downvoteComment(1);

        Comment comment1 = item.findMostHelpfulComment();
        assertEquals("Alice", comment1.getAuthor());
        assertEquals(2, comment1.getVoteCount());

        Comment comment2 = item.findCommentByAuthor("Bob");
        assertEquals(-1, comment2.getVoteCount());
    }

    @Test
    public void testFindMostHelpfulComment() {
        SalesItem item = new SalesItem("Mouse", 2999);

        item.addComment("Alice", "Good!", 4);
        item.addComment("Bob", "Better!", 5);
        item.upvoteComment(1);

        Comment mostHelpful = item.findMostHelpfulComment();
        assertEquals("Bob", mostHelpful.getAuthor());
    }

    @Test
    public void testShowInfo() {
        SalesItem item = new SalesItem("Headphones", 5999);

        item.addComment("Alice", "Excellent sound quality!", 5);
        item.addComment("Bob", "Comfortable to wear.", 4);

        // Check console output using a logging approach, or trust manual verification.
        item.showInfo();
    }
}


Hasil







Source Code:

Comments

Popular posts from this blog

Evaluasi Tengah Semester Pemrograman Berbasis Objek (PBO) A 2024

Tugas Pertemuan 7 Pemrograman Berbasis Objek (PBO) A 2024

Evaluasi Akhir Semester Pemrograman Berbasis Objek (PBO) A 2024