Answered step by step
Verified Expert Solution
Link Copied!

Question

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 transcribedimage text in transcribedIm 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 ted y ve style and d int targarettede other) Overrides el ade method is the object class Return the per c ode for the object Return them of companion of the codes of two Thementer if they are ading and of the area and they are descending order Note that you se e the comparable face for the I LOROrder 4. Please provide implementations for the following methods of the Cart class - The rest of the methods may remain the same as before a. String tostring Overridesetostringed the object class Test Ite class with methods test_toString(args), test_quaiscargs), test_hashCode args), and test_compareTo(args), b. TestItenOrder class with methods test tostring args), test equals(args), test_hashCodeCargs), and test_compareTo(aras). c Test Cart class with methods test_toString(args), test_quals(args), test_hashCode args). test compare to (args), and test cenoare( a s ), Fach test method should take one or more parameters as mpul display fest memput parameters the expected result and computed actual result return the result of their companion as 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 inutilize the objects to be tested and call their test methods with one sample value to show that they are function correctly The output mught be something like the followme Test class running Test Iten class running - ... testing to_String with input(s): ... Expected result: ... Actual result: ... SUCCESS testing equals with input(s): ... Expected result: ... Actual result: ... SUCCESS sa tingress of this Item Order b boolean equals(object other) int hashcode() Overrides the method in the atje elass Return true if the specified item is eqvalent to this them, and false otherwise Two items are equivalent if they have exactly equivalent towders in the cart and their membership status are the same Overrides the hashcode method in the objet Returns the generated use cock for the object. Return result of so of the codes of Two Cants, I if they are in ascending onde, if they are equal or they are in descending onder Note that you should also implement the comparable Toerface for the Cart d int correro(Cart other) int Laureart First Ir the selected thrary Hotell then theems should be sorted by ascending price ander and returned in this order to the Inter printed out on the console in this order Ir the weed library is Thoms", then the should be sorted by descending price onder and started in this order to be printed out on the come in this ordet Note that you should implement the c o r Interface for the Cart BONUS PART: Find the action listeners (ie, actionPerformed() methods) implemented with a Lambda Expression in the base source code, and replace them with object-oncated ActionListener interface implementations. And comment out the Lambda Expression based implementation Mark them clearly #7 implements that can find them easily and text both of your implement by commenting the alternative implementation easily Once you complete the above questions replace the con method of Tacoma above with an appropriate Lambda Expression in your implementation. In other words, for the Botell Bookstore there will be an object- onented implementation and for the Tam Bookstore there will be a lambda expression based implementation 6. Write the following test classes (a regular lest class, not a Juut test class) that will be able to access all the public methods of the above three classes: Cart, ItenOrder, 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 pro 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 prmt out the input parameters, expected result, and actual result 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 ted y ve style and d int targarettede other) Overrides el ade method is the object class Return the per c ode for the object Return them of companion of the codes of two Thementer if they are ading and of the area and they are descending order Note that you se e the comparable face for the I LOROrder 4. Please provide implementations for the following methods of the Cart class - The rest of the methods may remain the same as before a. String tostring Overridesetostringed the object class Test Ite class with methods test_toString(args), test_quaiscargs), test_hashCode args), and test_compareTo(args), b. TestItenOrder class with methods test tostring args), test equals(args), test_hashCodeCargs), and test_compareTo(aras). c Test Cart class with methods test_toString(args), test_quals(args), test_hashCode args). test compare to (args), and test cenoare( a s ), Fach test method should take one or more parameters as mpul display fest memput parameters the expected result and computed actual result return the result of their companion as 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 inutilize the objects to be tested and call their test methods with one sample value to show that they are function correctly The output mught be something like the followme Test class running Test Iten class running - ... testing to_String with input(s): ... Expected result: ... Actual result: ... SUCCESS testing equals with input(s): ... Expected result: ... Actual result: ... SUCCESS sa tingress of this Item Order b boolean equals(object other) int hashcode() Overrides the method in the atje elass Return true if the specified item is eqvalent to this them, and false otherwise Two items are equivalent if they have exactly equivalent towders in the cart and their membership status are the same Overrides the hashcode method in the objet Returns the generated use cock for the object. Return result of so of the codes of Two Cants, I if they are in ascending onde, if they are equal or they are in descending onder Note that you should also implement the comparable Toerface for the Cart d int correro(Cart other) int Laureart First Ir the selected thrary Hotell then theems should be sorted by ascending price ander and returned in this order to the Inter printed out on the console in this order Ir the weed library is Thoms", then the should be sorted by descending price onder and started in this order to be printed out on the come in this ordet Note that you should implement the c o r Interface for the Cart BONUS PART: Find the action listeners (ie, actionPerformed() methods) implemented with a Lambda Expression in the base source code, and replace them with object-oncated ActionListener interface implementations. And comment out the Lambda Expression based implementation Mark them clearly #7 implements that can find them easily and text both of your implement by commenting the alternative implementation easily Once you complete the above questions replace the con method of Tacoma above with an appropriate Lambda Expression in your implementation. In other words, for the Botell Bookstore there will be an object- onented implementation and for the Tam Bookstore there will be a lambda expression based implementation 6. Write the following test classes (a regular lest class, not a Juut test class) that will be able to access all the public methods of the above three classes: Cart, ItenOrder, 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 pro 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 prmt out the input parameters, expected result, and actual result

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions