Question
In this assignment, you will practice solving a problem using object-oriented programming and specifically you will use the concept of object composition (i.e., has-a relationship
In this assignment, you will practice solving a problem using object-oriented programming and specifically you will use the concept of object composition (i.e., has-a relationship between objects). You will implement a Java application, called ShoppingCart that could be used in a grocery store. You are asked to implement three classes: GroceryItem, ShoppingCart, and ShoppingCartDriver. Each of these classes is described below.
Problem Description
Class names, methods names and parameter types must exactly match the specification for full credit.
1. GroceryItem class
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
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.
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.
Other Requirements
- Draw a UML structure diagram showing the relationship between the classes in your solution. You may hand draw or use any tool of your choice but the diagram must be submitted in a format that I can read (one of .docx, .pdf, .jpg, .txt). If I cannot read it, I cannot grade it.
- Draw a UML class diagram for your ShoppingCart class (may be combined with the structure diagram).
- Note that automated tools often do not follow the conventions that I require.
- Factor the classes as described above GroceryItem, ShoppingCart and ShoppingCartDriver are all in separate classes with private instance variables
- Constants are normally declared as public
- Comment the GroceryItem and ShoppingCart classes with Javadocs style comments (see Appendix H).
- Use good coding style throughout (see slide deck 01-D Objects and Chapter 1 of your text) including
- correct private/public access modifiers
- consistent indenting
- meaningful variable names
- normal capitalization conventions
- other aspects of easy-to-read code
- You are free to adopt and adapt code from the textbook and class slides. However, document the source of any borrowed code.
(Please I need answer to the UML diagrams. I need help with the diagrams only. The part where it says other requirements.) Thanks!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started