Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA: We are going to implement a shopping cart that represents a collection of market items. This cart will be represented by an array. In

JAVA: We are going to implement a shopping cart that represents a collection of market items. This cart will be represented by an array. In this application, we assume the following:

  • The cart stores a collection of items of type String (descriptions for market items),
  • Each market item is identified by an index (unique number)
  • The shopping cart may contain multiple occurrences of the same item,
  • Users can ask how many occurrences of a given item are in the shopping cart (may be 0 or more)
  • Users can remove an item from the shopping cart,
  • Users can checkout the shopping cart (i.e. display number of items in the shopping cart together with total cost),
  • Users can display the content of the shopping cart.

We note also that the market product catalog (set of products available for sale in the market) and the shopping cart (set of items selected by the user for purchase) are stored in two different places.

The following is a demo of your program. Note that the users input below is shown in green, and that the rest of the text below was printed out by the program.

============= Welcome to the Shopping Cart App ============= 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 ENTER COMMAND: p +++++++++++++++++++++++++++++++++++++++++++++ Item id Description Price +++++++++++++++++++++++++++++++++++++++++++++ 0 Apple $1.59 1 Avocado $0.59 2 Banana $0.49 3 Beef $3.79 4 Blueberry $6.89 5 Broccoli $1.79 6 Butter $4.59 7 Carrot $1.19 8 Cereal $3.69 9 Cheese $3.49 10 Chicken $5.09 11 Chocolate $3.19 12 Cookie $9.5 13 Cucumber $0.79 14 Eggs $3.09 15 Grape $2.29 16 Ice Cream $5.39 17 Milk $2.09 18 Mushroom $1.79 19 Onion $0.79 20 Pepper $1.99 21 Pizza $11.5 22 Potato $0.69 23 Spinach $3.09 24 Tomato $1.79 +++++++++++++++++++++++++++++++++++++++++++++ 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 ENTER COMMAND: a 0 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 ENTER COMMAND: a 24 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 ENTER COMMAND: a 0 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 ENTER COMMAND: d Cart Content: Apple, Tomato, Apple, Milk, ENTER COMMAND: O 0 The number of occurrences of Apple (id #0) is: 2

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) {}

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) {}

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

Successful Keyword Searching Initiating Research On Popular Topics Using Electronic Databases

Authors: Randall MacDonald, Susan MacDonald

1st Edition

0313306761, 978-0313306761

Students also viewed these Databases questions

Question

Address the customer by name.

Answered: 1 week ago

Question

3. What might you have done differently

Answered: 1 week ago

Question

4. Did you rethink your decision?

Answered: 1 week ago

Question

3. Did you seek anyones advice?

Answered: 1 week ago