Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(JAVA) if anyone can help with these following methods, following the instructions in the method header comments, would really appreciate it! also would really appreciate

(JAVA) if anyone can help with these following methods, following the instructions in the method header comments, would really appreciate it! also would really appreciate if you could write //comments next to the important steps explaining what you did!

Our shopping cart can store a set of market items. Each market item is defined by a unique identifier (index), a description, and a unit price. Lets first define the set of final java fields that will represent this environment. To do so, add the following lines of codes to your ShoppingCart class :

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

// Define final parameters

private static final int CART_CAPACITY = 20; // shopping cart max capacity

private static final double TAX_RATE = 0.05; // sales tax

// a perfect-size two-dimensional array that stores the available items in the market

// MARKET_ITEMS[i][0] refers to a String that represents the description of the item

// identified by index i

// MARKET_ITEMS[i][1] refers to a String that represents the unit price of the item

// identified by index i in dollars.

public static final String[][] MARKET_ITEMS = new String[][] {{"Apple", "$1.59"},

{"Avocado", "$0.59"}, {"Banana", "$0.49"}, {"Beef", "$3.79"}, {"Blueberry", "$6.89"},

{"Broccoli", "$1.79"}, {"Butter", "$4.59"}, {"Carrot", "$1.19"}, {"Cereal", "$3.69"},

{"Cheese", "$3.49"}, {"Chicken", "$5.09"}, {"Chocolate", "$3.19"}, {"Cookie", "$9.5"},

{"Cucumber", "$0.79"}, {"Eggs", "$3.09"}, {"Grape", "$2.29"}, {"Ice Cream", "$5.39"},

{"Milk", "$2.09"}, {"Mushroom", "$1.79"}, {"Onion", "$0.79"}, {"Pepper", "$1.99"},

{"Pizza", "$11.5"}, {"Potato", "$0.69"}, {"Spinach", "$3.09"}, {"Tomato", "$1.79"}};

The design of the ShoppingCart operations is provided here as the six following commented method headings:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

// adds the item with the given its identifier (index) at the end of the cart

public static int add(int index, String[] cart, int count) {}

// Returns how many occurrences of the item with index itemIndex are present in the shopping cart

public static int occurrencesOf(int itemIndex, String[] cart, int count) {}

// Removes the first (only one) occurrence of itemToRemove if found and returns the number of

// items in the cart after remove operation is completed either successfully or not

public static int remove(String itemToRemove, String[] cart, int count) {}

// returns the total value (cost) of the cart without tax in $ (double)

public static double getSubTotalPrice(String[] cart, int count) {}

// prints the Market Catalog (item identifiers, description, and unit prices)

public static void printMarketCatalog() {}

// Displays the cart content (items separated by commas)

public static void displayCartContent(String[] cart, int count) {}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

/**

* adds the item with the given identifier index at the end of the cart

*

* @param index of the item within the marketItems array

* @param cart shopping cart

* @return the number of items present in the cart after the item with identifier index is added

*/

public static int add(int index, String[] cart, int count) {

// TODO complete this method

return 0;

}

/**

* Returns how many occurrences of the item with index itemIndex are present in the shopping cart

*

* @param itemIndex identifier of the item to count its occurrences in the cart

* @param cart shopping cart

* @param count number of items present within the cart

* @return the number of occurrences of item in the cart

*/

public static int occurrencesOf(int itemIndex, String[] cart, int count) {

// TODO complete this method

return 0;

}

Lets now implement add then OccurrencesOf methods. add() method will simply add the item (description) with the given index to the end of the array cart. It should return the total number of items present within the cart array after the item with identifier index is added. If the cart is full (reaches its capacity) and the user tries to add new item, the following warning message MUST be displayed and the count of the cart should not change after the method returns:

"WARNING: The cart is full. You cannot add any new item."

occurrencesOf() method returns how many occurrences of the market item of the given itemIndex are present in the cart (may be 0 or more). You can also notice that the cart contains elements of type String (items descriptions). On the other hand, we can add a market item to the cart, only given its index. To help you implement occurrencesOf() method (and may be later other ones), you can develop a private helper method that returns the item description (String) given its index (int). For instance,

private static String getItemDescription(int index) {}

We note that you DO NOT need to check for the validity of the provided index (positive number within the length of the MARKET_ITEMS array).

1

2

3

4

5

6

// Checks that items can be added more than one time and are found

public static boolean testAddOccurrencesOfDuplicateItems() {}

// Checks that the correct output is returned when the user tries to add too much items to the cart

// exceeding its capacity

public static boolean testAddingTooMuchItems() {}

DEVELOP REMOVE METHOD

Lets now implement remove operation. Recall that the signature of this method is exactly the following:

1

public static int remove(String itemToRemove, String[] cart, int count) {}

where itemToRemove represents the item to remove from the cart, cart represents the shopping cart, and count represents the number of market items present in the cart before remove is called. This method returns the number of items in the cart after remove operation is complete.

We note that remove operation works as follows. If itemToRemove is found to equal one of the strings referenced by the array cart, remove effectively takes one of the occurrences of the String element (the first match).

We first note that our cart represents an unordered collection of Strings. This means that the order of items in the array cart is not important. So, remove operation should not maintain the items present in the cart in the same order as that in which they were originally added. [There other collections used to store elements in order]. Given that, the algorithm that we propose to remove an item from the shopping cart is as follows (other algorithms also work; but you are required to implement this one for this assignment):

  • Find the itemToRemove in the cart>, or set it to -1 if it does not exist in the cart
  • If itemToRemove found,
    • move the element at the end of the array to this index
    • Update count
  • If itemToRemove not found, display the following warning message
    • "WARNING: " + itemToRemove + " not found in the shopping cart."
  • return itemToRemove in the cart>

To help you implement remove operation, you HAVE TO implement the following private helper method:

1

2

3

4

5

6

7

8

9

10

/**

* Returns the index of an item within the shopping cart

*

* @param item description

* @param cart Shopping cart

* @param count number of items present in the shopping cart

* @return index of the item within the shopping cart, and -1 if the item does not exist in the

* cart

*/

private static int indexOf(String item, String[] cart, int count) {}

Note here that indexOf does not return the identifier of the provided item (i.e. its index within the MARKET_ITEMS array). It returns the index of item within the cart array.

Once your remove operation is implemented, you can test its functioning with accordance to the instructions provided in the write-up. Recall that it is your responsibility to verify the correctness of your code using the appropriate test methods.

DEVELOP THE REST OF YOUR SHOPPING CART OPERATIONS

Now, following the same development process, implement the following methods:

1

2

3

4

5

6

7

8

// returns the total value (cost) of the cart without tax in $

public static double getSubTotalPrice(String[] cart, int count) {}

// prints this Market Catalog (item identifiers, description, and prices)

public static void printMarketCatalog() {}

// Displays the cart content (items separated by commas)

public static void displayCartContent(String[] cart, int count) {}

The expected output for printMarketCatalog and displayCartContent methods should be conform to the output format provided at the top of the write-up in the demo of the program. For your information, each line of product within the market catalog is represented by the following String:

 + "\t\t" +  + " \t " + 

Recall that your are responsible for defining additional test methods to check thoroughly the good functioning of your implementation with respect to the write-up. But, your ShoppingTests class MUST include at least seven static test methods (including those provided in this write-up).

STEP5. DRIVER APPLICATION

The final step in this assignment is to implement the main method of the ShoppingCart class. This method serves as the driver of the application. Organize this functionality into whatever custom static methods you see fit. But, make sure that running the main method within your ShoppingCart class results in an interaction section comparable to the sample shown at the top of the write-up. Any new variables that you create for this driver must be local within some static method. Pay close attention to the details within that example, to ensure that your programs welcome message, thank you message, command prompts, the different outputs are all printed in the same manner. Here are some specific requirements for how this interactive session should proceed:

  • You do not need to worry about erroneous input from the user, because all of our grading tests will focus on properly encoded commands, as described within this specification.
  • You MUST create and use only one instance of the Scanner class in your entire program.
  • All commands are case insensitive and are provided in the following command menu:
COMMAND MENU: [P] print the market catalog [A ] add one occurrence of an item to the cart given its identifier [C] checkout [D] display the cart content [O ] number of occurrences of an item in the cart given its identifier [R ] remove one occurrence of an item from the cart given its identifier [Q]uit the application

For instance,

  • If the first non-white-space character within the command string is a P (upper or lower case), the market catalog should be displayed.
  • If the first character in the user command line is a C (upper or lower case), a checkout operation will be proceeded and the number of items in the shopping cart together with subtotal (without tax) and total (including tax) should be displayed conforming to the following format:
    "#items: " +  + " Subtotal: $" + String.format("%.2f",  ) + " Tax: $" + String.format("%.2f",) + " TOTAL: $" + String.format("%.2f",)

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

Guide To Client Server Databases

Authors: Joe Salemi

2nd Edition

1562763105, 978-1562763107

More Books

Students also viewed these Databases questions

Question

2. What potential barriers would you encourage Samuel to avoid?

Answered: 1 week ago