Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Im stuck on the compareTo methods, and the test class. public class Cart { //Array List to store ordered items. private final List myItemOrders; //Check

image text in transcribed

image text in transcribed

image text in transcribed

Im stuck on the compareTo methods, and the test class.

public class Cart { //Array List to store ordered items. private final List myItemOrders; //Check if customer is a member private boolean myIsMember; // Constructor for empty cart public Cart() { myItemOrders = new ArrayList(); } public void add(final ItemOrder theOrder) { for (int i = 0; i

for (int i = 0; i

public final class Item { //Name of item private final String myName; //Price of item private final BigDecimal myPrice; //Bulk quantity of item. private int myBulkQuantity; //Bulk price of item private BigDecimal myBulkPrice;

//If item is bulk or not private final boolean myBulkOption; public Item(final String theName, final BigDecimal thePrice) { myName = Objects.requireNonNull(theName); //Check for a price less than zero. Allows for BOGO items if (thePrice.compareTo(BigDecimal.ZERO) == -1) { throw new IllegalArgumentException(); } myPrice = Objects.requireNonNull(thePrice); myBulkOption = false; } public Item(final String theName, final BigDecimal thePrice, final int theBulkQuantity, final BigDecimal theBulkPrice) { myName = Objects.requireNonNull(theName); if ((thePrice.compareTo(BigDecimal.ZERO) == -1) || (theBulkQuantity

//Get item price public BigDecimal getPrice() { return myPrice; } //Get item bulk quantity public int getBulkQuantity() { return myBulkQuantity; } //Get item bulk price public BigDecimal getBulkPrice() { return myBulkPrice; }

//If item is bulk or not public boolean isBulk() { return myBulkOption; } @Override public String toString() { final StringBuilder builder = new StringBuilder(128); final NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US); builder.append(myName); builder.append(", "); builder.append(nf.format(myPrice)); // Bulk option. if (isBulk()) { builder.append(" ("); builder.append(myBulkQuantity); builder.append(" for "); builder.append(nf.format(myBulkPrice)); builder.append(')'); } return builder.toString(); } @Override public boolean equals(final Object theOther) { boolean returnValue = false;

if (theOther != null && this.getClass() == theOther.getClass()) { final Item otherItem = (Item) theOther; returnValue = (Objects.equals(myName, otherItem.myName)) && (myPrice.compareTo(otherItem.myPrice) == 0); if (isBulk()) { returnValue = (Objects.equals(myName, otherItem.myName)) && (myPrice.compareTo(otherItem.myPrice) == 0) && (Integer.compare(myBulkQuantity, otherItem.myBulkQuantity) == 0) && (myBulkPrice.compareTo (otherItem.myBulkPrice) == 0); } } return returnValue; }

@Override public int hashCode() { final int hash; if (isBulk()) { hash = Objects.hash(myName, myPrice); } else { hash = Objects.hash(myName, myPrice, myBulkQuantity, myBulkPrice); } return hash; } }

public final class ItemOrder { //Reference to item private final Item myItem; private final int myQuantity; public ItemOrder(final Item theItem, final int theQuantity) { myItem = Objects.requireNonNull(theItem); if (theQuantity

//Get item public Item getItem() { return myItem; } //Get quantity of items public int getQuantity() { return myQuantity; }

@Override public String toString() { final StringBuilder builder = new StringBuilder(128); builder.append(getClass().getSimpleName()); builder.append(", Quantity: "); builder.append(myQuantity); return builder.toString(); } }

1. Please do the following cosmetic and layout changes on the GUI of the application to improve its look-and- feel. Please clearly mark your changes on the code with appropriate comments (e.g., #1.a - changes ...') to help us to locate your changes quickly. a Change the Title of the window to identify the application more appropriately. For example, something like "PA#03 UW Bookstore Application might be a better name. b. Change the colors of the panels to some other colors of your choice to improve its aesthetics. C. Append a Bookstore" postfix to the end of each Campus name, e.g., "Tacoma will be replaced by "Tacoma Bookstore, and so on. d. Move the checkbox with the "customer has store membership label to the top panel under the library selection. Change the label name to something more meaningful. e. Move the "order total:" to the bottom panel, left of the "Clear button. Change its label to something more meaningful. The "Clear" button should be displayed on the right on the bottom panel. f. Remove the "Seattle Bookstore from the list displayed at the top. Your new version will support only two bookstores. 2. Please provide implementations for the following methods of the Item class The rest of the methods may remain the same as before: a. String toString() Overrides the toString method in the object class. returns a String representation of this Item: name, followed by a comma and a space, followed by price. See the provided examples in the textbook. Overrides the equals method in the object class. returns true if the specified item is equivalent to this Item, and false otherwise. b. boolean equals(object other) Two Items are equivalent if they have exactly equivalent names, prices, bulk quantities and bulk prices. See the provided template in the textbook. int hashCode() Overrides the hashCode method in the object class. Returns the generated hash code for the object. See the examples in the textbook. d. int compareTo(Item other) Returns the result of comparison of the names of two Items alphabetically; -1 if they are in ascending order, O if they are equal, and 0 if they are in descending order. Note that you should also implement the comparable Interface for the Item class. 3. Please provide implementations for the following methods of the ItemOrder class The rest of the methods may remain the same as before: a. String toString() Overrides the toString method in the object class. returns a String representation of this Itemorder. It should contain the item description as given above and the quantity. boolean equals(Object other) | Overrides the equals method in the object class. returns true if the specified item is equivalent to this Item, and false otherwise. b Two Itemorders are equivalent if they have exactly equivalent items and quantities. | int hashCode() Overrides the hashCode method in the object class. Returns the generated hashCode for the object. int compareTo(ItemOrder other) Returns the result of comparison of the hash codes of two ItemOrders: - 1 if they are in ascending order, 0 if they are equal, and 0 if they are in descending order. Note that you should also implement the comparable Interface for the Itemorder class. 4. Please provide implementations for the following methods of the Cart class The rest of the methods may remain the same as before: String toString() Overrides the toString method in the object class. returns a String representation of this Itemorder. b boolean equals(Object other) Overrides the equals method in the object class. returns true if the specified item is equivalent to this Item, and false otherwise. Two items are equivalent if they have exactly equivalent Itemorders in the cart and their membership status are the same. int hashCode Overrides the hashCode method in the object class. Returns the generated hashCode for the object. d. int compareTo(Cart other) Returns the result of comparison of the hash codes of two Carts; -1 if they are in ascending order, 0 if they are equal, and 0 if they are in descending order. Note that you should also implement the comparable Interface for the Cart class. C. int compare(Cart first, Cart second) If the selected library is "Bothell", then the items should be sorted by ascending price order and returned in this order to be later printed out on the console in this order. If the selected library is "Tacoma", then the items should be sorted by descending price order and returned in this order to be later printed out on the console in this order. Note that you should also implement the Comparator Interface for the Cart class. 5. Once you complete the above questions, replace the compare() method of Tacoma above with an appropriate Lambda Expression in your implementation. In other words, for the Bothell Bookstore there will be an object- oriented implementation, and for the Tacoma Bookstore there will be a lambda expression based implementation. 6. Write the following test classes (a regular test class, not a JUnit test class) that will be able to access all the public methods of the above three classes: Cart, ItemOrder, and Item. Your test classes will include the following methods for validation testing of your application. The validation testing is performed to prove that your program satisfies all the requirements mentioned in the assignment. Write a separate method for each new method you developed in the scope of this assignment - Each test method will print out the input parameters, expected result, and actual result: a. TestItem class with methods test_toString(args), test_equals(args), test_hashcode(args), and test_compareTo(args), b. TestItemorder class with methods test_toString(args), test_equals(args), test_hashCode (args), and test_compareTo(args), c. Test Cart class with methods test_toString(args), test_equals(args), test_hashCode (args), test_compare To(args), and test_compare(args), Each test method should take one or more parameters as input, display test name, input parameters, the expected result, and computed actual result; return the result of their comparison as 1 to indicate "SUCCESS and -1 to indicate "FAIL" to the caller which will be print it out. In the main() method of the Test... classes, you read in or initialize the objects to be tested and call their test methods with one sample value to show that they are functioning correctly. The output might be something like the following: Test class running ... TestItem class running ... .. testing to_String with input(s): ... Expected result: ... Actual result: .... SUCCESS testing equals with input(s): ... Expected result: ... Actual result: ... SUCCESS BONUS PART: Find the action listeners (i.e., actionPerformed() methods) implemented with a Lambda Expression in the base source code, and replace them with object-oriented ActionListener interface implementations. And comment out the Lambda Expression based implementations. Mark them clearly (e.g. #7 implements ...) so that I can find them easily and test both of your implementations by commenting out the alternative implementation easily. 1. Please do the following cosmetic and layout changes on the GUI of the application to improve its look-and- feel. Please clearly mark your changes on the code with appropriate comments (e.g., #1.a - changes ...') to help us to locate your changes quickly. a Change the Title of the window to identify the application more appropriately. For example, something like "PA#03 UW Bookstore Application might be a better name. b. Change the colors of the panels to some other colors of your choice to improve its aesthetics. C. Append a Bookstore" postfix to the end of each Campus name, e.g., "Tacoma will be replaced by "Tacoma Bookstore, and so on. d. Move the checkbox with the "customer has store membership label to the top panel under the library selection. Change the label name to something more meaningful. e. Move the "order total:" to the bottom panel, left of the "Clear button. Change its label to something more meaningful. The "Clear" button should be displayed on the right on the bottom panel. f. Remove the "Seattle Bookstore from the list displayed at the top. Your new version will support only two bookstores. 2. Please provide implementations for the following methods of the Item class The rest of the methods may remain the same as before: a. String toString() Overrides the toString method in the object class. returns a String representation of this Item: name, followed by a comma and a space, followed by price. See the provided examples in the textbook. Overrides the equals method in the object class. returns true if the specified item is equivalent to this Item, and false otherwise. b. boolean equals(object other) Two Items are equivalent if they have exactly equivalent names, prices, bulk quantities and bulk prices. See the provided template in the textbook. int hashCode() Overrides the hashCode method in the object class. Returns the generated hash code for the object. See the examples in the textbook. d. int compareTo(Item other) Returns the result of comparison of the names of two Items alphabetically; -1 if they are in ascending order, O if they are equal, and 0 if they are in descending order. Note that you should also implement the comparable Interface for the Item class. 3. Please provide implementations for the following methods of the ItemOrder class The rest of the methods may remain the same as before: a. String toString() Overrides the toString method in the object class. returns a String representation of this Itemorder. It should contain the item description as given above and the quantity. boolean equals(Object other) | Overrides the equals method in the object class. returns true if the specified item is equivalent to this Item, and false otherwise. b Two Itemorders are equivalent if they have exactly equivalent items and quantities. | int hashCode() Overrides the hashCode method in the object class. Returns the generated hashCode for the object. int compareTo(ItemOrder other) Returns the result of comparison of the hash codes of two ItemOrders: - 1 if they are in ascending order, 0 if they are equal, and 0 if they are in descending order. Note that you should also implement the comparable Interface for the Itemorder class. 4. Please provide implementations for the following methods of the Cart class The rest of the methods may remain the same as before: String toString() Overrides the toString method in the object class. returns a String representation of this Itemorder. b boolean equals(Object other) Overrides the equals method in the object class. returns true if the specified item is equivalent to this Item, and false otherwise. Two items are equivalent if they have exactly equivalent Itemorders in the cart and their membership status are the same. int hashCode Overrides the hashCode method in the object class. Returns the generated hashCode for the object. d. int compareTo(Cart other) Returns the result of comparison of the hash codes of two Carts; -1 if they are in ascending order, 0 if they are equal, and 0 if they are in descending order. Note that you should also implement the comparable Interface for the Cart class. C. int compare(Cart first, Cart second) If the selected library is "Bothell", then the items should be sorted by ascending price order and returned in this order to be later printed out on the console in this order. If the selected library is "Tacoma", then the items should be sorted by descending price order and returned in this order to be later printed out on the console in this order. Note that you should also implement the Comparator Interface for the Cart class. 5. Once you complete the above questions, replace the compare() method of Tacoma above with an appropriate Lambda Expression in your implementation. In other words, for the Bothell Bookstore there will be an object- oriented implementation, and for the Tacoma Bookstore there will be a lambda expression based implementation. 6. Write the following test classes (a regular test class, not a JUnit test class) that will be able to access all the public methods of the above three classes: Cart, ItemOrder, and Item. Your test classes will include the following methods for validation testing of your application. The validation testing is performed to prove that your program satisfies all the requirements mentioned in the assignment. Write a separate method for each new method you developed in the scope of this assignment - Each test method will print out the input parameters, expected result, and actual result: a. TestItem class with methods test_toString(args), test_equals(args), test_hashcode(args), and test_compareTo(args), b. TestItemorder class with methods test_toString(args), test_equals(args), test_hashCode (args), and test_compareTo(args), c. Test Cart class with methods test_toString(args), test_equals(args), test_hashCode (args), test_compare To(args), and test_compare(args), Each test method should take one or more parameters as input, display test name, input parameters, the expected result, and computed actual result; return the result of their comparison as 1 to indicate "SUCCESS and -1 to indicate "FAIL" to the caller which will be print it out. In the main() method of the Test... classes, you read in or initialize the objects to be tested and call their test methods with one sample value to show that they are functioning correctly. The output might be something like the following: Test class running ... TestItem class running ... .. testing to_String with input(s): ... Expected result: ... Actual result: .... SUCCESS testing equals with input(s): ... Expected result: ... Actual result: ... SUCCESS BONUS PART: Find the action listeners (i.e., actionPerformed() methods) implemented with a Lambda Expression in the base source code, and replace them with object-oriented ActionListener interface implementations. And comment out the Lambda Expression based implementations. Mark them clearly (e.g. #7 implements ...) so that I can find them easily and test both of your implementations by commenting out the alternative implementation easily

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions