Implementasi Unit Test
Nama: Azka Rizqullah Ramadhani
NRP: 5025231148
Kelas: Pemrograman Berbasis Object (A)
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)) { // reject invalid ratings
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()) { // index is valid
comments.remove(index);
}
}
public void upvoteComment(int index)
{
if(index >=0 && index < comments.size()) { // index is valid
comments.get(index).upvote();
}
}
public void downvoteComment(int index)
{
if(index >=0 && index < comments.size()) { // index is valid
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;
}
private 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 votes;
public Comment(String author, String text, int rating) {
this.author = author;
this.text = text;
this.rating = rating;
this.votes = 0;
}
public String getAuthor() {
return author;
}
public String getFullDetails() {
return "Author: " + author + "\n" +
"Rating: " + rating + "\n" +
"Votes: " + votes + "\n" +
"Comment: " + text;
}
public int getVoteCount() {
return votes;
}
public void upvote() {
votes++;
}
public void downvote() {
votes--;
}
}
Class SalesItemTest
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
public class SalesItemTest {
@Test
public void testAddCommentAndCount() {
SalesItem salesItem = new SalesItem("Java Book", 12345);
assertTrue(salesItem.addComment("James Duckling", "Great book...", 4));
assertTrue(salesItem.addComment("Fred", "Like it", 2));
assertEquals(2, salesItem.getNumberOfComments());
}
@Test
public void testRejectDuplicateAuthorComment() {
SalesItem salesItem = new SalesItem("Java Book", 12345);
assertTrue(salesItem.addComment("James Duckling", "Great book...", 4));
assertEquals(false, salesItem.addComment("James Duckling", "Not as good on second read.", 3));
}
@Test
public void testRejectInvalidRating() {
SalesItem salesItem = new SalesItem("Java Book", 12345);
assertEquals(true, salesItem.addComment("Jane Doe", "Not my style", 0));
assertEquals(false, salesItem.addComment("John Doe", "Amazing read!", 6));
}
}
Hasil
Komentar
Posting Komentar