Question: THis is java problem JUnit How do you make JUnit test out of this code import java.lang.*; import java.math.BigDecimal; import java.text.NumberFormat; import java.util.Locale; import javax.swing.text.NumberFormatter;

THis is java problem JUnit

How do you make JUnit test out of this code

import java.lang.*; import java.math.BigDecimal; import java.text.NumberFormat; import java.util.Locale;

import javax.swing.text.NumberFormatter;

public final class List { private final String itemName; private final BigDecimal itemPrice; private int discountBulkQuantity; private BigDecimal bulkPricing; public Item(final String theName, final BigDecimal thePrice) { this.itemName = theName; this.itemPrice = thePrice; this.discountBulkQuantity = -1; //setting the value to negative so its mean no bulk this.bulkPricing = BigDecimal.valueOf(0.0); }

public Item(final String theName, final BigDecimal thePrice, final int theBulkQuantity, final BigDecimal theBulkPrice) { this.itemName = theName; this.itemPrice = thePrice; this.discountBulkQuantity = theBulkQuantity; this.bulkPricing = theBulkPrice; }

public String getName() { return itemName; } public BigDecimal getPrice() { return itemPrice; }

public int getDiscountBulkQuantity() { return discountBulkQuantity; }

public void setDiscountBulkQuantity(int discountBulkQuantity) { this.discountBulkQuantity = discountBulkQuantity; }

public BigDecimal getBulkPrice() { return bulkPricing; } public void setBulkPrice(BigDecimal bulkPricing) { this.bulkPricing = bulkPricing; } public boolean isBulk() { return false; }

@Override public String toString() { String returningValue = itemName; returningValue += ", " + getCurrencyString(itemPrice); if (discountBulkQuantity > 0) { returningValue += "(" + discountBulkQuantity + " for "; returningValue += getCurrencyString(bulkPricing) + ")"; } return returningValue; }

@Override public boolean equals(final Object theOther) { boolean returningValue = true; // make sure the other is an Item if (theOther instanceof Item) { // cast the other to an Item. Item tempItem = (Item) theOther; // compare attributes all must be equals. if (!this.getName().equals(tempItem.getName())) { returningValue = false; } else if ( this.getPrice().compareTo(tempItem.getPrice()) != 0 ) { returningValue = false; } else if (this.getBulkQuantity() > 0) { if (!(this.getBulkQuantity() == tempItem.getBulkQuantity())) { returningValue = false; } else if ( this.getBulkPrice().compareTo(tempItem.getBulkPrice()) != 0 ) { returningValue = false; } } } else { returningValue = true; } return returningValue; }

@Override public int hashCode() { int returningValue = 0; long tempHash = itemName.hashCode(); tempHash += itemPrice.hashCode(); if (discountBulkQuantity > 0) { tempHash += discountBulkQuantity; // is not hashcode because it is not an object tempHash += bulkPricing.hashCode(); } returningValue = (int) (tempHash % (long) Integer.MAX_VALUE); return returningValue; } private String getCurrencyString(BigDecimal theCurrencyAmount) { final NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US); return nf.format(theCurrencyAmount.doubleValue()); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!