Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a UML diagrams of a grocery store that has three classes: GroceryItem, ShoppingCart, and ShoppingCartDriver. Each of these classes is described below. The GroceryItem

Create a UML diagrams of a grocery store that has three classes: GroceryItem, ShoppingCart, and ShoppingCartDriver. Each of these classes is described below.

The GroceryItem class represents one item ordered from a grocery store. The GroceryItem has three instance variables: name (of type String), price (of type double) and quantity (of type int).

Implement the following methods for the GroceryItem class:

  • GroceryItem (String name, double price, int qty) - A constructor to initialize all instance variables. A value for each instance variable will be passed as a parameter to the constructor. The parameters must be in the order name, price and quantity.
  • Getters for all instance variables.
  • Setter methods for each instance variable.
  • boolean equals(Object item): a method to test the equality of two items. Two items are considered to be equal if they have the same name. The price and quantity should not be compared. The comparison must be case insensitive.
  • String toString(): a method that returns a nicely formatted string description of the item. All instance variables should be displayed, separated by tabs (use "\t") and the entire GroceryItem should be displayed on one line. In addition to name, price and quantity, calculate the total value of the GroceryItem (price * quantity) and add it to the end of the string.

Rice $1.50 3 $4.50

Use the NumberFormat or DecimalFormat class to display prices with exactly two digits following the decimal place.

2. 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
  • 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.

3. ShoppingCartDriver class

In a separate class, implement a test program (called a driver) to do the following actions:

  • Create two ShoppingCarts with your choice of customer names.
  • Add at least three GroceryItems to the first ShoppingCart and at least five GroceryItems to the second ShoppingCart.
  • Test the special case where the GroceryItem has already been added (add() causes the existing item's quantity to be changed).
  • Test the special case where the array is too full to add another item.
  • Print both invoices in a nicely formatted output.
  • Your driver should also test all the methods in the ShoppingCart class (see p. 26-29 for a discussion of good testing).
  • Do not use a Scanner to read any inputs from the user. Instead, use hard coded values for the different methods' inputs.

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

Understanding Oracle APEX 5 Application Development

Authors: Edward Sciore

2nd Edition

1484209893, 9781484209899

Students also viewed these Databases questions

Question

Python: List of Even Integers

Answered: 1 week ago