Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ShoppingCart class: The ShoppingCart class represents a collection of GroceryItems. The ShoppingCart class is identified by the following private instance variables: customer: a string that

ShoppingCart class:

The ShoppingCart class represents a collection of GroceryItems. The ShoppingCart class is identified by the following private instance variables:

  • customer: a string that represents the customer's name.
  • order: an instance variable of type array of GroceryItem
  • numItems: an instance variable that indicates the number of GroceryItems that have been added to the ShoppingCart

Implement the following public methods for the ShoppingCart class:

  • A no-arguments constructor that instantiates an array of GroceryItems to an arbitrary size of your choice and sets numItems to 0. Each item in the array will initially be null by default. Set the name of the customer to the empty string ("").
  • A constructor that has one parameter, the customer's name. The constructor records the customer's name and instantiates the array of GroceryItems to an arbitrary size of your choice. Each item in the array will initially be null by default. Set numItems to 0.
  • A third constructor that has two parameters, the customer's name and the initial capacity of the array. The constructor records the customer's name and instantiates the array of GroceryItems with the given capacity. Each item in the array will initially be null by default. Set numItems to 0.
  • boolean add(String name, double price, int quantity): a method that takes three inputs parameters to represent the GroceryItem's name, price and quantity (the parameters must be in that order). If the GroceryItem has already been ordered, add() will increment the quantity of the existing item by the new quantity. Otherwise, the method creates an object of type GroceryItem and adds this object to the next available array location in order. Note that items[0] must be filled before items[1] and items[1] must be filled before items[2], etc. The method should return true if a new GroceryItem was added or the quantity of an existing GroceryItem was changed and false if the array was full and the GroceryItem could not be added.
  • int find(String name): A method that has one parameter of type String which represents the name of a GroceryItem. find() searches the items array and returns the index of the matching GroceryItem. find() returns -1 if no match is found in the items array. find() must call the equals() method from the GroceryItem class. Recall that you must pass a GroceryItem to the equals() method so you must use the GroceryItem constructor to convert the name String into a GroceryItem.
  • double getTotalBeforeTax(): a method that calculates and returns the total value of all items in this ShoppingCart
  • double getTax(double taxRate): a method with one parameter, taxRate. getTax() calculates and returns a double representing the tax to be paid on the ShoppingCart.
  • int getNumGroceryItems(): a method that returns the number of GroceryItems in the ShoppingCart. In the example given below, getNumGroceryItems() would return 4.
  • int getTotalQuantity(): a method that sums and returns the quantities of all GroceryItems in the ShoppingCart. In the example given below, getTotalQuantity() would return 10.
  • toString(): a method that returns a string representation of the ShoppingCart that includes the following:
    • customer's name,
    • number of GroceryItems in this statement,
    • details of all the GroceryItems, one per line
    • the ShoppingCart total before tax
    • the amount of tax to be added (use a fixed rate of 7.5%)
    • the ShoppingCart total after tax has been added

Sample of String returned from toString():

Maria Chavez

---------------------------------------------------

Item Price Qty Total

---------------------------------------------------

Rice $1.50 3 $4.50

Sushi $8.25 2 $16.50

Sambusa $5.80 4 $23.20

Injera $2.50 1 $2.50

---------------------------------------------------

Total: $46.70

Tax: $3.50

---------------------------------------------------

Grand total: $50.20

Other

  • Declare a constant for the tax rate and use the name of the constant in toString().
  • The methods in the ShoppingCart class will not print anything.

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

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions