7 22 Ch 7 Program Online Shopping Cart Continued Java Github

(JAVA ZyBook) 7.22 Ch 7 Program: Online shopping cart(continued) (Java) - I am getting a specific erroron ZyBook "Exception in thread "main"java.util.NoSuchElementException atjava.base/java.util.Scanner.throwFor(Scanner.java:". I know thishas to do with the scanner and input not being read correctly. Cansomeone please review the code and tell me what I should go to getit to work on ZyBook. (Works fine on IntelliJ)

7.22 Ch 7 Program: Online shopping cart (continued) (Java)

Note: Creating multiple Scanner objects for the same inputstream yields unexpected behavior. Thus, good practice is to use asingle Scanner object for reading input from System.in. ThatScanner object can be passed as an argument to any methods thatread input.

This program extends the earlier "Online shopping cart" program.(Consider first saving your earlier program).

(1) Extend the ItemToPurchase class per the followingspecifications:

  • Private fields
  • string itemDescription - Initialized in default constructor to"none"
  • Parameterized constructor to assign item name, itemdescription, item price, and item quantity (default values of 0).(1 pt)
  • Public member methods
  • setDescription() mutator & getDescription() accessor (2pts)
  • printItemCost() - Outputs the item name followed by thequantity, price, and subtotal
  • printItemDescription() - Outputs the item name anddescription

(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 becompleted 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 dateas 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'sname) parameter. Does not return anything.
    • If item name cannot be found, output this message: Item notfound in cart. Nothing removed.
  • modifyItem()

    • Modifies an item's description, price, and/or quantity. Hasparameter ItemToPurchase. Does not return anything.
    • If item can be found (by name) in cart, check if parameter hasdefault values for description, price, and quantity. If not, modifyitem 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 noparameters.
  • printTotal()

    • Outputs total of objects in cart.
    • If cart is empty, output this message: SHOPPING CART ISEMPTY
  • printDescriptions()

    • Outputs each item's description.


(4) Implement the printMenu() method. printMenu() has aShoppingCart parameter, and outputs a menu of options to manipulatethe shopping cart. Each option is represented by a singlecharacter. Build and output the menu within the method.

If the an invalid character is entered, continue to prompt for avalid choice. Hint: Implement Quit before implementing otheroptions. Call printMenu() in the main() method. Continue toexecute the menu until the user enters q to Quit. (3 pts)

(5) Implement Output shopping cart menu option. (3 pts)

(6) Implement Output item's description menu option. (2 pts)

(7) Implement Add item to cart menu option. (3 pts)

(8) Implement Remove item menu option. (4 pts)

(9) Implement Change item quantity menu option. Hint: Make newItemToPurchase object and use ItemToPurchase modifiers before usingmodifyItem() method. (5 pts)

My Program Code:

import java.util.Scanner;public class ShoppingCartManager { //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. public static void printMenu(ShoppingCart shoppingCart) { System.out.println ("nMENU"); System.out.println ("a - Add item to cart"); System.out.println ("d - Remove item from cart"); System.out.println ("c - Change item quantity"); System.out.println ("i - Output items' descriptions"); System.out.println ("o - Output shopping cart"); System.out.println ("q - Quit"); System.out.println ("nChoose an option:"); Scanner scnr = new Scanner (System.in); Scanner scnrInt = new Scanner (System.in); // Declare a new item to purchase. ItemToPurchase item = new ItemToPurchase (); String Name = ""; String Description = ""; int Price = 0; int Quantity = 0; String option = scnr.next (); scnr.nextLine(); switch(option.charAt (0)){ case 'a': System.out.println ("nADD ITEM TO CART"); scnr = new Scanner (System.in); System.out.println ("Enter the item name:"); Name = scnr.nextLine (); System.out.println ("Enter the item description:"); Description = scnr.nextLine (); System.out.println ("Enter the item price:"); Price = scnrInt.nextInt (); System.out.println ("Enter the item quantity:"); Quantity = scnrInt.nextInt (); item = new ItemToPurchase (Name, Description, Price, Quantity); shoppingCart.addItem (item); printMenu (shoppingCart); break; case 'd': System.out.println ("REMOVE ITEM FROM CART"); System.out.println ("Enter name of item to remove:"); scnr.nextLine (); String itemName = scnr.nextLine (); shoppingCart.removeItem (itemName); printMenu (shoppingCart); break; case 'c': System.out.println ("CHANGE ITEM QUANTITY"); System.out.println ("Enter the item name:"); scnr.nextLine (); itemName = scnr.nextLine (); shoppingCart.modifyItem (itemName); printMenu (shoppingCart); break; case 'i': System.out.println ("OUTPUT ITEM'S DESCRIPTIONS"); shoppingCart.printDescriptions (); printMenu (shoppingCart); break; case 'o': System.out.println ("OUTPUT SHOPPING CART"); shoppingCart.printTotal (); printMenu (shoppingCart); break; default: System.out.println ("Invalid Choice!"); printMenu (shoppingCart); } System.out.println ("n"); } public static void main (String[]args){ { Scanner scnr = new Scanner (System.in); System.out.println ("Enter Customer's Name:"); String customerName = scnr.nextLine (); System.out.println ("Enter Today's Date:"); String currentDate = scnr.nextLine (); System.out.println ("nCustomer Name: " + customerName); System.out.println ("Today's Date: " + currentDate); // Create a linked list shopping cart. ShoppingCart shoppingCart = new ShoppingCart (customerName, currentDate); // Print menu. printMenu (shoppingCart); } }}

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

import java.util.ArrayList;import java.util.Scanner;public class ShoppingCart { private String CustomerName; private String Date; private ArrayList<ItemToPurchase> cartItems; Scanner scnr = new Scanner(System.in); boolean found; public ShoppingCart() { CustomerName = "none"; Date = "January 1, 2016"; cartItems = new ArrayList<ItemToPurchase>(); }//parametrised constructor public ShoppingCart(String CustomerName, String CurrentDate) { this.CustomerName = CustomerName; this.Date = CurrentDate; cartItems = new ArrayList<ItemToPurchase>(); } //getCustomerName() accessor (1 pt) //getDate() accessor (1 pt) public String getCustomerName() { return CustomerName; } public String getDate() { return Date; } //addItem() //Adds an item to cartItems array. Has parameter ItemToPurchase. Does not return anything. public void addItem(ItemToPurchase item) { cartItems.add(item); } //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. public void removeItem(String itemName) { //found = false; for (int i = 0; i < cartItems.size (); i++) { if ((cartItems.get (i).getName ().equals (itemName))) { cartItems.remove (i); //found = true; break; } else { System.out.println ("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. public void modifyItem(String itemName) { found = false; for (int i = 0; i < cartItems.size(); i++) { if ((cartItems.get(i).getName().equals(itemName))) { System.out.println("Enter the new quantity:"); int quant = scnr.nextInt(); cartItems.get(i).setQuantity(quant); found = true; break; } } if (!found) System.out.println("Item not found in cart. Nothing removed"); } //getNumItemsInCart() (2 pts) //Returns quantity of all items in cart. Has no parameters. public int getNumItemsInCart() { int totalQuantity = 0; if (cartItems.isEmpty()) { System.out.println("SHOPPING CART IS EMPTY"); } else { for (int i = 0; i < cartItems.size(); i++) { totalQuantity = totalQuantity + cartItems.get(i).getQuantity(); } } return totalQuantity; } //getCostOfCart() (2 pts) //Determines and returns the total cost of items in cart. Has no parameters. public int getCostOfCart() { int totalCost = 0; if (cartItems.isEmpty()) { System.out.println("SHOPPING CART IS EMPTY"); } else { for (int i = 0; i < cartItems.size(); i++) { totalCost = totalCost + (cartItems.get(i).getQuantity() * cartItems.get(i).getPrice()); } } //Price + Price*Quantity return totalCost; } //printTotal() // //Outputs total of objects in cart. //If cart is empty, output this message: SHOPPING CART IS EMPTY public void printTotal() { if (cartItems.isEmpty()) { System.out.println("SHOPPING CART IS EMPTY"); } else { System.out.println(CustomerName + "'s Shopping Cart - " + Date); System.out.println("Number of Items: " + getNumItemsInCart()); for (ItemToPurchase item : cartItems) { item.printItemCost(); } System.out.println("nnTotal: $" + getCostOfCart()); } } //printDescriptions() //Outputs each item's description. public void printDescriptions() { if (cartItems.isEmpty()) { System.out.println("SHOPPING CART IS EMPTY"); } else { System.out.println(CustomerName + "'s Shopping Cart - " + Date); System.out.println("nItem Descriptions"); for (ItemToPurchase item : cartItems) { item.printItemDescription(); } } }}

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

public class ItemToPurchase {//class properties private String Name; private int Price; private int Quantity; private String Description;//default constructor to set properties to default values public ItemToPurchase() { Name="None"; Price=0; Quantity=0; Description = "None"; }//parametrised constructor public ItemToPurchase (String Name, String Description, int Price, int Quantity) { this.Name = Name; this.Description = Description; this.Price = Price; this.Quantity = Quantity; }//getters and setters to get and set properties public String getName() { return Name; } public void setName(String Name) { this.Name = Name; } public int getPrice() { return Price; } public void setPrice(int Price) { this.Price = Price; } public int getQuantity() { return Quantity; } public void setQuantity(int Quantity) { this.Quantity = Quantity; } public String getDescription() { return Description; } public void setDescription(String Description) { this.Description = Description; } public void printItemCost() { System.out.println(Name + " " + Quantity + " @ $" + Price + Price*Quantity); } public void printItemDescription() { System.out.println(Name + ":" + Description); }}----------------------------------------------------

Error ScreenShot:

John Doe February 1, 2016 Input f Enter Customers Name: Enter Todays Date: Customer Name: John Doe Todays Date: February 1

Enter Customers Name: Enter Todays Date: Customer Name: John Doe Todays Date: February 1, 2016 MENU Add item to cart a Exp

Similarly in all the other test cases the same errorappears.

John Doe February 1, 2016 Input f Enter Customer's Name: Enter Today's Date: Customer Name: John Doe Today's Date: February 1, 2016 MENU Add item to cart a d Remove item from cart : - Change item quantity C Your output i Output items' descriptions Output shopping cart qQuit Choose an option Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor (Scanner.java:937) lat java.base/java.util.Scanner.next (Scanner.java:1478) lat ShoppingCartManager.printMenu(ShoppingCartManager.java:31) lat ShoppingCartManager.main(ShoppingCartManager.java:115) Enter Customer's Name: Enter Today's Date: Customer Name: John Doe Today's Date: February 1, 2016 MENU Add item to cart a Expected output d Remove item from cart Change item quantity C i Output items' descriptions Output shopping cart qQuit Choose option: an option: Choose an Choose option: an Show transcribed image text John Doe February 1, 2016 Input f Enter Customer's Name: Enter Today's Date: Customer Name: John Doe Today's Date: February 1, 2016 MENU Add item to cart a d Remove item from cart : - Change item quantity C Your output i Output items' descriptions Output shopping cart qQuit Choose an option Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor (Scanner.java:937) lat java.base/java.util.Scanner.next (Scanner.java:1478) lat ShoppingCartManager.printMenu(ShoppingCartManager.java:31) lat ShoppingCartManager.main(ShoppingCartManager.java:115)
Enter Customer's Name: Enter Today's Date: Customer Name: John Doe Today's Date: February 1, 2016 MENU Add item to cart a Expected output d Remove item from cart Change item quantity C i Output items' descriptions Output shopping cart qQuit Choose option: an option: Choose an Choose option: an

arizmendiourprome1954.blogspot.com

Source: https://www.answersdive.com/ExpertAnswers/java-zybook-722-ch-7-program-online-shopping-cart-continued-java-getting-specific-error-zy

0 Response to "7 22 Ch 7 Program Online Shopping Cart Continued Java Github"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel