Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with writing a j united method. a. Write test cases (i.e., JUnit 5 test methods) for the equals(), hashCode() , compareTo(), and

I need help with writing a j united method.

a. Write test cases (i.e., JUnit 5 test methods) for the equals(), hashCode() , compareTo(), and compare() methods of the Item class.

here is the item class

/* * TCSS305 * * Item Class */

package model;

import java.text.NumberFormat; import java.util.Locale; import java.util.Objects;

import java.math.BigDecimal;

public final class Item implements Comparable { private final String TheName; private final BigDecimal ThePrice; private int TheBulkQuantity; private BigDecimal TheBulkPrice; private final boolean TheBulkOption;

public Item(final String theName, final BigDecimal thePrice) { this.TheName = Objects.requireNonNull(theName); if (thePrice.compareTo(BigDecimal.ZERO) == -1) { throw new IllegalArgumentException(); } this.ThePrice = Objects.requireNonNull(thePrice); this.TheBulkOption = false; }

public Item(final String theName, final BigDecimal thePrice, final int theBulkQuantity, final BigDecimal theBulkPrice) { this.TheName = Objects.requireNonNull(theName); if (thePrice.compareTo(BigDecimal.ZERO) == -1 || theBulkQuantity < 0 || theBulkPrice.compareTo(BigDecimal.ZERO) == -1) { throw new IllegalArgumentException(); } this.ThePrice = Objects.requireNonNull(thePrice); this.TheBulkQuantity = theBulkQuantity; this.TheBulkPrice = Objects.requireNonNull(theBulkPrice); this.TheBulkOption = true; }

public BigDecimal getPrice() { return this.ThePrice; }

public int getBulkQuantity() { return this.TheBulkQuantity; }

public BigDecimal getBulkPrice() { return this.TheBulkPrice; }

public boolean isBulk() { return this.TheBulkOption; }

@Override public String toString() { final NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US); final StringBuilder SB = new StringBuilder(); SB.append(this.TheName); SB.append(", "); SB.append(nf.format(this.ThePrice)); if (this.isBulk()) { SB.append(" ("); SB.append(this.TheBulkQuantity); SB.append(" for "); SB.append(nf.format(this.TheBulkPrice)); SB.append(')'); } return SB.toString(); }

@Override public boolean equals(final Object theOther) { boolean result = false; if (theOther != null && this.getClass() == theOther.getClass()) { final Item otherItem = (Item) theOther; result = (Objects.equals(this.TheName, otherItem.TheName) && this.ThePrice.compareTo(otherItem.ThePrice) == 0); if (this.isBulk()) { result = (Objects.equals(this.TheName, otherItem.TheName) && this.ThePrice.compareTo(otherItem.ThePrice) == 0 && Integer.compare(this.TheBulkQuantity, otherItem.TheBulkQuantity) == 0 && this.TheBulkPrice.compareTo(otherItem.TheBulkPrice) == 0); } } return result; }

public int hashCode() { return 7*Objects.hashCode(TheName) + 11*Objects.hashCode(ThePrice) + 13*Objects.hashCode(TheBulkQuantity) + 17*Objects.hashCode(TheBulkPrice) + 19*Objects.hashCode(TheBulkOption); }

@Override public int compareTo(Item other) { int comp = TheName.compareToIgnoreCase(other.TheName); //this returns 0 if they are equal if(comp==0) return 0; else if(comp>0) //if they are arranged in descending order, output 1 return 1; return -1; }

public int compare(Item first, Item second) { if (first.ThePrice.compareTo(second.ThePrice)<0) return -1; else if (first.ThePrice.compareTo(second.ThePrice)>0) return 1; else return 0;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Moving Objects Databases

Authors: Ralf Hartmut Güting, Markus Schneider

1st Edition

0120887991, 978-0120887996

More Books

Students also viewed these Databases questions

Question

=+5. What do you want them to think?

Answered: 1 week ago

Question

=+What the product does for the end-user.)

Answered: 1 week ago