ShoppingCart.java - Class definition ShoppingCartManager.java - Contains main() method Build the ShoppingCart class with the following specifications. Private fields String customerName - Initialized in default constructor to "none" String currentDate - Initialized in default constructor to "January 1, 2016" ArrayList cartItems Default constructor Parameterized constructor which takes the customer name and date as parameters Public member methods getCustomerName() accessor getDate() accessor addItem() Adds an item to cartItems array. Has a parameter of type ItemToPurchase. Does not return anything. removeItem() Removes item from cartItems array. Has a string (an item's name) parameter. Does not return anything. If item name cannot be found, output a message: Item not found in cart. Nothing removed. modifyItem() Modifies an item's description, price, and/or quantity. Has a parameter of type ItemToPurchase. Does not return anything. If item can be found (by name) in cart, check if parameter has default values for description, price, and quantity. If not, modify item in cart. If item cannot be found (by name) in cart, output a message: Item not found in cart. Nothing modified. getNumItemsInCart() Returns quantity of all items in cart. Has no parameters. getCostOfCart() Determines and returns the total cost of items in cart. Has no parameters. printTotal() Outputs total of objects in cart. If cart is empty, output a message: SHOPPING CART IS EMPTY printDescriptions() Outputs each item's description. If cart is empty, output a message: SHOPPING CART IS EMPTY Step 3: In main(), prompt the user for a customer's name and today's date. Output the name and date. Create an object of type ShoppingCart. Step 4: Implement the following menu functions in the ShoppingCartManager class printMenu() Prints the following menu of options to manipulate the shopping cart. MENU a - Add item to cart d - Remove item from cart c - Change item quantity i - Output items' descriptions o - Output shopping cart q - Quit executeMenu() Takes 3 parameters: a character representing the user's choice, a shopping cart, and a Scanner object. Performs the menu options described below in step 5, according to the user's choice. Step 5: Implement the menu options Step 5a: In main(), call printMenu() and prompt for the user's choice of menu options. Each option is represented by a single character. If an invalid character is entered, continue to prompt for a valid choice. When a valid option is entered, execute the option by calling executeMenu(). Then, print the menu and prompt for a new option. Continue until the user enters 'q'. Hint: Implement Quit before implementing other options. Step 5b: Implement "Output shopping cart" menu option in executeMenu(). Step 5c: Implement "Output items' descriptions" menu option in executeMenu(). Step 5d: Implement "Add item to cart" menu option in executeMenu(). Ex: ADD ITEM TO CART Enter the item name: Nike Romaleos Enter the item description: Volt color, Weightlifting shoes Enter the item price: 189 Enter the item quantity: 2 Step 5e: Implement "Remove item from cart" menu option in executeMenu(). Ex: REMOVE ITEM FROM CART Enter name of item to remove: Chocolate Chips Step 5f: Implement "Change item quantity" menu option in executeMenu(). Hint: Make new ItemToPurchase object and use ItemToPurchase modifiers before using modifyItem() method.
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
- ShoppingCart.java - Class definition
- ShoppingCartManager.java - Contains main() method
Build the ShoppingCart class with the following specifications.
- Private fields
- String customerName - Initialized in default constructor to "none"
- String currentDate - Initialized in default constructor to "January 1, 2016"
- ArrayList cartItems
- Default constructor
- Parameterized constructor which takes the customer name and date as parameters
- Public member methods
- getCustomerName() accessor
- getDate() accessor
- addItem()
- Adds an item to cartItems array. Has a parameter of type ItemToPurchase. Does not return anything.
- removeItem()
- Removes item from cartItems array. Has a string (an item's name) parameter. Does not return anything.
- If item name cannot be found, output a message: Item not found in cart. Nothing removed.
- modifyItem()
- Modifies an item's description, price, and/or quantity. Has a parameter of type ItemToPurchase. Does not return anything.
- If item can be found (by name) in cart, check if parameter has default values for description, price, and quantity. If not, modify item in cart.
- If item cannot be found (by name) in cart, output a message: Item not found in cart. Nothing modified.
- getNumItemsInCart()
- Returns quantity of all items in cart. Has no parameters.
- getCostOfCart()
- Determines and returns the total cost of items in cart. Has no parameters.
- printTotal()
- Outputs total of objects in cart.
- If cart is empty, output a message: SHOPPING CART IS EMPTY
- printDescriptions()
- Outputs each item's description.
- If cart is empty, output a message: SHOPPING CART IS EMPTY
Step 3: In main(), prompt the user for a customer's name and today's date. Output the name and date. Create an object of type ShoppingCart.
Step 4: Implement the following menu functions in the ShoppingCartManager class
- printMenu()
- Prints the following menu of options to manipulate the shopping cart.
- executeMenu()
- Takes 3 parameters: a character representing the user's choice, a shopping cart, and a Scanner object. Performs the menu options described below in step 5, according to the user's choice.
Step 5: Implement the menu options
Step 5a: In main(), call printMenu() and prompt for the user's choice of menu options. Each option is represented by a single character.
If an invalid character is entered, continue to prompt for a valid choice. When a valid option is entered, execute the option by calling executeMenu(). Then, print the menu and prompt for a new option. Continue until the user enters 'q'.
Hint: Implement Quit before implementing other options.
Step 5b: Implement "Output shopping cart" menu option in executeMenu().
Step 5c: Implement "Output items' descriptions" menu option in executeMenu().
Step 5d: Implement "Add item to cart" menu option in executeMenu().
Ex:
ADD ITEM TO CART Enter the item name: Nike Romaleos Enter the item description: Volt color, Weightlifting shoes Enter the item price: 189 Enter the item quantity: 2Step 5e: Implement "Remove item from cart" menu option in executeMenu().
Ex:
REMOVE ITEM FROM CART Enter name of item to remove: Chocolate ChipsStep 5f: Implement "Change item quantity" menu option in executeMenu().
Hint: Make new ItemToPurchase object and use ItemToPurchase modifiers before using modifyItem() method.
- printMenu()
![```java
public class ItemToPurchase {
private String itemName;
private int itemPrice;
private int itemQuantity;
// Default constructor
public ItemToPurchase() {
itemName = "none";
itemPrice = 0;
itemQuantity = 0;
}
// Mutator for itemName
public void setName(String itemName) {
this.itemName = itemName;
}
// Accessor for itemName
public String getName() {
return itemName;
}
}
```
### Explanation:
This Java code snippet defines a class named `ItemToPurchase`. Below is a breakdown of the key components:
1. **Class Definition:**
- `public class ItemToPurchase { ... }`: This begins the definition of the `ItemToPurchase` class.
2. **Private Member Variables:**
- `private String itemName;`: Represents the name of the item as a string.
- `private int itemPrice;`: Represents the price of the item as an integer.
- `private int itemQuantity;`: Represents the quantity of the item as an integer.
3. **Constructor:**
- `public ItemToPurchase() { ... }`: This is the default constructor which initializes the member variables with default values:
- `itemName` is set to `"none"`.
- `itemPrice` is set to `0`.
- `itemQuantity` is set to `0`.
4. **Mutator Method (Setter):**
- `public void setName(String itemName) { ... }`: Sets the `itemName` to the specified string value passed as a parameter.
5. **Accessor Method (Getter):**
- `public String getName() { ... }`: Returns the current value of `itemName`.
This class can be used in a program to create objects for each item that a user intends to purchase, allowing setting and retrieving of item names while initializing other attributes with default values.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Faad9e4ed-ed50-4fe3-96ca-72ed3d364f6f%2Fecb49f5b-8f4f-4f31-8854-63cb05524b8b%2Fkfg2e28_processed.png&w=3840&q=75)
![```java
// Mutator for itemPrice
public void setPrice(int itemPrice) {
this.itemPrice = itemPrice;
}
// Accessor for itemPrice
public int getPrice() {
return itemPrice;
}
// Mutator for itemQuantity
public void setQuantity(int itemQuantity) {
this.itemQuantity = itemQuantity;
}
// Accessor for itemQuantity
public int getQuantity() {
return itemQuantity;
}
```
**Explanation:**
This Java code snippet demonstrates the use of mutator (setter) and accessor (getter) methods for two private member variables: `itemPrice` and `itemQuantity`.
1. **Mutator Method for itemPrice**:
- `setPrice(int itemPrice)`: This method sets the value of `itemPrice` to the parameter provided. It is a public method allowing external modification of the private `itemPrice` attribute.
2. **Accessor Method for itemPrice**:
- `getPrice()`: Returns the current value of `itemPrice`. As a public method, it provides a way to access the value of the private `itemPrice` attribute from outside the class.
3. **Mutator Method for itemQuantity**:
- `setQuantity(int itemQuantity)`: Sets the value of `itemQuantity`. This method provides external code the ability to set the value of the private variable `itemQuantity`.
4. **Accessor Method for itemQuantity**:
- `getQuantity()`: Returns the current value of `itemQuantity`, allowing external access to this private member variable.
These methods are essential for encapsulation in object-oriented programming, allowing controlled access and modification to private class variables.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Faad9e4ed-ed50-4fe3-96ca-72ed3d364f6f%2Fecb49f5b-8f4f-4f31-8854-63cb05524b8b%2F8j0qjn_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 6 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)