Tugas 10 Unit Testing
Nama : Afel Allaric Absor
NRP : 5025231140
Kelas : Pemrograman Bebasis Objek (A)
Tugas 10 Unit Testing
- Isi file SalesItem.java :
import java.util.ArrayList;
import java.util.Iterator;
/**
* The class represents sales items on an online e-commerce site (such
* as Amazon.com). SalesItem objects store all information relevant to
* this item, including description, price, customer comments, etc.
*
* NOTE: The current version is incomplete! Currently, only code
* dealing with customer comments is here.
*
* @author Michael Kölling and David J. Barnes
* @version 0.1 (2011-07-31)
*/
public class SalesItem
{
private String name;
private int price; // in cents
private ArrayList<Comment> comments;
/**
* Create a new sales item.
*/
public SalesItem(String name, int price)
{
this.name = name;
this.price = price;
comments = new ArrayList<Comment>();
}
/**
* Return the name of this item.
*/
public String getName()
{
return name;
}
/**
* Return the price of this item.
*/
public int getPrice()
{
return price;
}
/**
* Return the number of customer comments for this item.
*/
public int getNumberOfComments()
{
return comments.size();
}
/**
* Add a comment to the comment list of this sales item. Return
* true if successful, false if the comment was rejected.
*
* The comment will be rejected if the same author has already
* left a comment or if the rating is invalid. Valid ratings are
* numbers between 1 and 5 (inclusive).
*/
public boolean addComment(String author, String text, int rating)
{
if(ratingInvalid(rating)) { // reject invalid ratings
return false;
}
if(findCommentByAuthor(author) != null) {
// reject multiple comments by same author
return false;
}
comments.add(new Comment(author, text, rating));
return true;
}
/**
* Remove the comment stored at the index given. If the index is
* invalid, do nothing.
*/
public void removeComment(int index)
{
if(index >=0 && index < comments.size()) { // index is valid
comments.remove(index);
}
}
/**
* Upvote the comment at 'index'. That is: count this comment as
* more helpful. If the index is invalid, do nothing.
*/
public void upvoteComment(int index)
{
if(index >=0 && index < comments.size()) { // index is valid
comments.get(index).upvote();
}
}
/**
* Downvote the comment at 'index'. That is: count this comment as
* less helpful. If the index is invalid, do nothing.
*/
public void downvoteComment(int index)
{
if(index >=0 && index < comments.size()) { // index is valid
comments.get(index).downvote();
}
}
/**
* Show all comments on screen. (Currently, for testing purposes:
* print to the terminal. Modify later for web display.)
*/
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("=====================================");
}
/**
* Return the most helpful comment. The most useful comment is the
* one with the highest vote balance. If there are multiple
* comments with equal highest balance, return any one of them.
*/
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;
}
/**
* Check whether the given rating is invalid. Return true if it is
* invalid. Valid ratings are in the range [1..5].
*/
private boolean ratingInvalid(int rating)
{
return rating < 1 || rating > 5;
}
/**
* Find the comment by the author with the given name.
*
* @return The comment if it exists, null if it doesn'>t.
*/
private Comment findCommentByAuthor(String author)
{
for(Comment comment : comments) {
if(comment.getAuthor().equals(author)) {
return comment;
}
}
return null;
}
/**
* For a price given as an int, return a readable String
* representing the same price. The price is given in whole cents.
* For example for price==12345, the following String
* is returned: $123.45
*/
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;
}
}
}
- Isi file dari Comment.java :
public class Comment {
private String author;
private String text;
private int rating;
private int votes;
/**
* Constructor for Comment.
*/
public Comment(String author, String text, int rating) {
this.author = author;
this.text = text;
this.rating = rating;
this.votes = 0;
}
/**
* Get the author of the comment.
*/
public String getAuthor() {
return author;
}
/**
* Get the full details of the comment.
*/
public String getFullDetails() {
return "Author: " + author + "\n" +
"Rating: " + rating + "\n" +
"Votes: " + votes + "\n" +
"Comment: " + text;
}
/**
* Get the current vote count.
*/
public int getVoteCount() {
return votes;
}
/**
* Upvote the comment.
*/
public void upvote() {
votes++;
}
/**
* Downvote the comment.
*/
public void downvote() {
votes--;
}
}
- isi file dari SalesItemTest.java :
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(false, salesItem.addComment("Jane Doe", "Not my style", 0));
assertEquals(false, salesItem.addComment("John Doe", "Amazing read!", 6));
}
}
- Hasil kode saat dijalankan :
- Link repository github berisi kode : https://github.com/afelallaric/Tugas-PBO-10-Unit-Testing.git

Komentar
Posting Komentar