Here is my code 1 import javautilScanner 2 3 public class Sh
Here is my code:
1 import java.util.Scanner;
2
3 public class ShoppingCartPrinter
4 {
5 public static void main (String[] args)
6 {
7 ItemToPurchase item1 = new ItemToPurchase();
8 ItemToPurchase item2 = new ItemToPurchase();
9 Scanner kbdInput = new Scanner(System.in);
10
11 String itemName;
12 double itemPrice;
13 int itemQuantity;
14
15 System.out.println(\"Item 1\");
16 System.out.println(\"Enter the item name: \");
17 itemName = kbdInput.nextLine();
18
19 System.out.println(\"Enter the item price: \");
20 itemPrice = kbdInput.nextDouble();
21 System.out.println(\"Enter the item quantity: \");
22 itemQuantity = kbdInput.nextInt();
23
24 item1.setName(itemName);
25 item1.setPrice(itemPrice);
26 item1.setQuantity(itemQuantity);
27 kbdInput.nextLine();
28
29 System.out.println(\"Item 2\");
30 System.out.println(\"Enter the item name: \");
31 itemName = kbdInput.nextLine();
32 System.out.println(\"Enter the item price: \");
33 itemPrice = kbdInput.nextDouble();
34 System.out.println(\"Enter the item quantity: \");
35 itemQuantity = kbdInput.nextInt();
36
37 item2.setName(itemName);
38 item2.setPrice(itemPrice);
39 item2.setQuantity(itemQuantity);
40 kbdInput.nextLine();
41
42 double item1Total = item1.getQuantity() * item1.getPrice();
43
44 System.out.println(\"TOTAL COST\");
45 System.out.println(item1.getName() + \" \" + item1.getQuantity() + \" @ $\" + item1.getPrice() + \" = $\" + item1Total);
46
47 double item2Total = item2.getQuantity() * item2.getPrice();
48
49 System.out.println(item2.getName() + \" \" + item2.getQuantity() + \" @ $\" + item2.getPrice() + \" = $\" + item2Total);
50 System.out.println(\"Total: $\" + (item1Total + item2Total));
51
52
53
54 }
55 }
This is my ItemToPurchase class with all of my get/set:
1 public class ItemToPurchase
2 {
3 private String itemName;
4 private double itemPrice;
5 private int itemQuantity;
6
7 public ItemToPurchase()
8 {
9 itemName = \"none\";
10 itemPrice = 0;
11 itemQuantity = 0;
12 }
13
14 public void setName(String name)
15 {
16 itemName = name;
17 }
18
19 public String getName()
20 {
21 return itemName;
22 }
23
24 public void setPrice(double price)
25 {
26 itemPrice = price;
27 }
28
29 public double getPrice()
30 {
31 return itemPrice;
32 }
33
34 public void setQuantity(int quantity)
35 {
36 itemQuantity = quantity;
37 }
38
39 public int getQuantity()
40 {
41 return itemQuantity;
42 }
The objective is to extend my \"Online shopping cart\" program.
(1) Extend the ItemToPurchase class per the following specifications:
Private fields
string itemDescription - Initialized in default constructor to \"none\"
Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)
Public member methods
setDescription() mutator & getDescription() accessor (2 pts)
printItemCost() - Outputs the item name followed by the quantity, price, and subtotal
printItemDescription() - Outputs the item name and description
Ex. of printItemCost() output:
Ex. of printItemDescription() output:
(2) Create two new files:
ShoppingCart.java - Class definition
ShoppingCartManager.java - Contains main() method
Build the ShoppingCart class with the following specifications. Note: Some can be method stubs (empty methods) initially, to be completed in later steps.
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 (1 pt)
Public member methods
getCustomerName() accessor (1 pt)
getDate() accessor (1 pt)
addItem()
Adds an item to cartItems array. Has parameter 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 this message: Item not found in cart. Nothing removed.
modifyItem()
Modifies an item\'s description, price, and/or quantity. Has parameter 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 this message: Item not found in cart. Nothing modified.
getNumItemsInCart() (2 pts)
Returns quantity of all items in cart. Has no parameters.
getCostOfCart() (2 pts)
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 this message: SHOPPING CART IS EMPTY
printDescriptions()
Outputs each item\'s description.
Ex. of printTotal() output:
Ex. of printDescriptions() output:
(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. (1 pt)
Ex.
(4) Implement the printMenu() method. printMenu() has a ShoppingCart parameter, and outputs a menu of options to manipulate the shopping cart. Each option is represented by a single character. Build and output the menu within the method.
If the an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call printMenu() in the main() method. Continue to execute the menu until the user enters q to Quit. (3 pts)
Ex:
(5) Implement Output shopping cart menu option. (3 pts)
Ex:
(6) Implement Output item\'s description menu option. (2 pts)
Ex.
(7) Implement Add item to cart menu option. (3 pts)
Ex:
(8) Implement Remove item menu option. (4 pts)
Ex:
(9) Implement Change item quantity menu option. Hint: Make new ItemToPurchase object and use ItemToPurchase modifiers before using modifyItem() method. (5 pts)
Ex:
Solution
Here is the solution for 1st question, the modified ItemToPurchase.java version:
//This is my ItemToPurchase class with all of my get/set:
public class ItemToPurchase
{
private String itemName;
private double itemPrice;
private int itemQuantity;
//(1) Extend the ItemToPurchase class per the following specifications:
//Private fields
//string itemDescription
private String itemDescription;
public ItemToPurchase()
{
itemName = \"none\";
itemPrice = 0;
itemQuantity = 0;
//Initialized in default constructor to \"none\"
itemDescription = \"none\";
}
//Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)
public ItemToPurchase(String iDesc)
{
itemName = \"none\";
itemPrice = 0;
itemQuantity = 0;
itemDescription = iDesc;
}
//setDescription() mutator & getDescription() accessor (2 pts)
public void setDescription(String iDesc)
{
itemDescription = iDesc;
}
public String getDescription()
{
return itemDescription;
}
//printItemCost() - Outputs the item name followed by the quantity, price, and subtotal
public void printItemCost()
{
System.out.println(itemName + \" \" + itemQuantity + \" @ $\" + itemPrice + \" = $\" + itemQuantity * itemPrice);
}
//printItemDescription() - Outputs the item name and description
public void printItemDescription()
{
System.out.println(itemName + \": \" + itemDescription);
}
public void setName(String name)
{
itemName = name;
}
public String getName()
{
return itemName;
}
public void setPrice(double price)
{
itemPrice = price;
}
public double getPrice()
{
return itemPrice;
}
public void setQuantity(int quantity)
{
itemQuantity = quantity;
}
public int getQuantity()
{
return itemQuantity;
}
}
![Here is my code: 1 import java.util.Scanner; 2 3 public class ShoppingCartPrinter 4 { 5 public static void main (String[] args) 6 { 7 ItemToPurchase item1 = new Here is my code: 1 import java.util.Scanner; 2 3 public class ShoppingCartPrinter 4 { 5 public static void main (String[] args) 6 { 7 ItemToPurchase item1 = new](/WebImages/2/here-is-my-code-1-import-javautilscanner-2-3-public-class-sh-970331-1761495879-0.webp)
![Here is my code: 1 import java.util.Scanner; 2 3 public class ShoppingCartPrinter 4 { 5 public static void main (String[] args) 6 { 7 ItemToPurchase item1 = new Here is my code: 1 import java.util.Scanner; 2 3 public class ShoppingCartPrinter 4 { 5 public static void main (String[] args) 6 { 7 ItemToPurchase item1 = new](/WebImages/2/here-is-my-code-1-import-javautilscanner-2-3-public-class-sh-970331-1761495879-1.webp)
![Here is my code: 1 import java.util.Scanner; 2 3 public class ShoppingCartPrinter 4 { 5 public static void main (String[] args) 6 { 7 ItemToPurchase item1 = new Here is my code: 1 import java.util.Scanner; 2 3 public class ShoppingCartPrinter 4 { 5 public static void main (String[] args) 6 { 7 ItemToPurchase item1 = new](/WebImages/2/here-is-my-code-1-import-javautilscanner-2-3-public-class-sh-970331-1761495879-2.webp)
![Here is my code: 1 import java.util.Scanner; 2 3 public class ShoppingCartPrinter 4 { 5 public static void main (String[] args) 6 { 7 ItemToPurchase item1 = new Here is my code: 1 import java.util.Scanner; 2 3 public class ShoppingCartPrinter 4 { 5 public static void main (String[] args) 6 { 7 ItemToPurchase item1 = new](/WebImages/2/here-is-my-code-1-import-javautilscanner-2-3-public-class-sh-970331-1761495879-3.webp)
![Here is my code: 1 import java.util.Scanner; 2 3 public class ShoppingCartPrinter 4 { 5 public static void main (String[] args) 6 { 7 ItemToPurchase item1 = new Here is my code: 1 import java.util.Scanner; 2 3 public class ShoppingCartPrinter 4 { 5 public static void main (String[] args) 6 { 7 ItemToPurchase item1 = new](/WebImages/2/here-is-my-code-1-import-javautilscanner-2-3-public-class-sh-970331-1761495879-4.webp)