Question

In: Computer Science

IN JAVA PLEASE!!! Note: Creating multiple Scanner objects for the same input stream yields unexpected behavior....

IN JAVA PLEASE!!!

Note: Creating multiple Scanner objects for the same input stream yields unexpected behavior. Thus, good practice is to use a single Scanner object for reading input from System.in. That Scanner object can be passed as an argument to any methods that read input.

(1) Create an ItemToPurchase class per the following specifications:

  • Private fields
  • Create a default constructor
    • string itemName - Initialized in default constructor to "none"
    • float itemPrice - Initialized in default constructor to 0.0
    • int itemQuanity - Initialized in default constructor to 0
    • string itemDescription - Initialized in default constructor to "none"
  • Parameterized constructor to assign item name, item description, item price, and item quantity
  • Public member methods
    • setDescription() mutator & getDescription() accessor
    • setName() mutator & getName() accessor
    • setQuantity() mutator & getQuantity() accessor
    • setPrice() mutator & getPrice() accessor
    • printItemCost() - Outputs the item name followed by the quantity, price, and subtotal
    • printItemDescription() - Outputs the item name and description

Ex. of printItemCost() output:

Bottled Water 10 @ $1 = $10

Ex. of printItemDescription() output:

Bottled Water: Deer Park, 12 oz.

(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

  • Public member methods

  • getCustomerName() accessor

  • getDate() accessor

  • 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()
    • 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 this message: SHOPPING CART IS EMPTY
  • printDescriptions()
    • Outputs each item's description.

Ex. of printTotal() output:

John Doe's Shopping Cart - February 1, 2016
Number of Items: 8

Nike Romaleos 2 @ $189 = $378
Chocolate Chips 5 @ $3 = $15
Powerbeats 2 Headphones 1 @ $128 = $128

Total: $521

Ex. of printDescriptions() output:

John Doe's Shopping Cart - February 1, 2016

Item Descriptions
Nike Romaleos: Volt color, Weightlifting shoes
Chocolate Chips: Semi-sweet
Powerbeats 2 Headphones: Bluetooth headphones

(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.
Ex.

Enter Customer's Name:
John Doe
Enter Today's Date:
February 1, 2016

Customer Name: John Doe
Today's Date: February 1, 2016

(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.
Ex:

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

Choose an option: 

(5) Implement Output shopping cart menu option.
Ex:

OUTPUT SHOPPING CART
John Doe's Shopping Cart - February 1, 2016
Number of Items: 8

Nike Romaleos 2 @ $189 = $378
Chocolate Chips 5 @ $3 = $15
Powerbeats 2 Headphones 1 @ $128 = $128

Total: $521

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

OUTPUT ITEMS' DESCRIPTIONS
John Doe's Shopping Cart - February 1, 2016

Item Descriptions
Nike Romaleos: Volt color, Weightlifting shoes
Chocolate Chips: Semi-sweet
Powerbeats 2 Headphones: Bluetooth headphones

(7) Implement Add item to cart menu option.
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

(8) Implement Remove item menu option.
Ex:

REMOVE ITEM FROM CART
Enter name of item to remove:
Chocolate Chips

(9) Implement Change item quantity menu option. Hint: Make new ItemToPurchase object and use ItemToPurchase modifiers before using modifyItem() method.
Ex:

CHANGE ITEM QUANTITY
Enter the item name:
Nike Romaleos
Enter the new quantity:
3

Solutions

Expert Solution

/*** ItemToPurchase.java ***/

public class ItemToPurchase
{
   private String itemName = "";
   private int itemPrice = 0;
   private int itemQuantity = 0;
   private int quantity = 0;
   private int price = 0;
   private String itemDescription = "none";

   public ItemToPurchase()
   {
       this.itemDescription = "none";
       this.itemName = "";
       this.itemPrice = 0;
       this.itemQuantity = 0;
   }

   public ItemToPurchase(String itemName, String itemDescription, int itemPrice, int itemQuantity )
   {
       this.itemName = itemName;
       this.itemPrice = itemPrice;
       this.itemQuantity = itemQuantity;
       this.itemDescription = itemDescription;
   }


   public void setName(String itemName)
   {
       this.itemName = itemName;
   }

   public String getName()
   {
       return itemName;
   }
  
   public void setPrice(int itemPrice)
   {
       this.itemPrice = itemPrice;
   }
  
   public int getPrice()
   {
       return itemPrice;
   }
  
   public void setQuantity(int itemQuantity)
   {
       this.itemQuantity = itemQuantity;
   }
  
   public int getQuantity()
   {
       return itemQuantity;
   }
  
   //Part 2 public member methods
  
   public void setDescription(String itemDescription)
   {
       this.itemDescription = itemDescription;
   }
  
   public String getDescription()
   {
       return itemDescription;
   }
  
   public void printItemCost()
   {
       System.out.println(itemName + " " + itemQuantity + "@ $" + itemPrice + " = $" + (itemQuantity * itemPrice));
   }
  
   public void printItemDescription()
   {
      System.out.println(itemName +": " + itemDescription);
   }
}
  
/*** ShoppingCart.java ***/

import java.util.ArrayList;
import java.util.Scanner;

public class ShoppingCart
{
   private String customerName;
   private String currentDate;
   private ArrayList<ItemToPurchase> cartItems;
   Scanner scnr = new Scanner(System.in);
  
   //Default Constructor
   public ShoppingCart()
   {
       customerName = "none";
       currentDate = "January 1, 2016";
       cartItems = new ArrayList<ItemToPurchase>();
   }
  
   //Pramaterized Constructor/Overloader
   public ShoppingCart(String customerName, String currentDate)
   {
       this.customerName = customerName;
       this.currentDate = currentDate;
       cartItems = new ArrayList<ItemToPurchase>();
   }
  
   //Public member methods
   public String getCustomerName()
   {
       return customerName;
   }
  
   public String getDate()
   {
       return currentDate;
   }
  
   public void addItem(ItemToPurchase item)
   {
       cartItems.add(item);
   }
  
   public void removeItem(String itemName)
   {
       int found = 0;
       for (int i = 0; i < cartItems.size(); i++)
       {
           if((cartItems.get(i).getName().equals(itemName)))
           {
               cartItems.remove(i);
               return;
           }
       }
       System.out.println("Item not found in cart. Nothing removed.");          
   }
  
   public void modifyItem(String ItemToPurchase)
   {
       int found = 0;
       for (int i = 0; i < cartItems.size(); i++)
       {
           if((cartItems.get(i).getName().equals(ItemToPurchase)))
           {
               System.out.println("Enter the new quantity");
               int amount = scnr.nextInt();
               cartItems.get(i).setQuantity(amount);
               return;
           }
       }
       System.out.println("Item not found in cart. Nothing modified.");
   }
  
   public int getNumItemsInCart()
   {
       int totalQuant = 0;
      
       for (int i = 0; i < cartItems.size(); i++)
       {
           totalQuant = totalQuant + cartItems.get(i).getQuantity();
       }
       return totalQuant;
   }
  
   public int getCostOfCart()
   {
       int totalCost = 0;
      
       for (int i = 0; i < cartItems.size(); i++)
       {
           totalCost = totalCost + (cartItems.get(i).getQuantity() * cartItems.get(i).getPrice());
       }
       return totalCost;
   }
  
   public void printTotal()
   {
       System.out.println(this.customerName + "Shopping Cart" + currentDate);
       if(cartItems != null && !cartItems.isEmpty()) {
       System.out.println("Number of items: " + getNumItemsInCart());
      
       for(ItemToPurchase item: cartItems)
       {
           item.printItemCost();
       }
      
       System.out.println();
       System.out.println("Total:" + "$" + getCostOfCart());
       }else {
       System.out.println("Empty Cart");
       }
   }
  
   public void printDescriptions()
   {
       System.out.println(customerName + "'s Shopping Cart - " + currentDate);
       System.out.println("Item Descriptions");
       for (ItemToPurchase item : cartItems)
       {
           item.printItemDescription();
       }
   }
}

/*** ShoppingCartManager.java ***/

import java.util.Scanner;
public class ShoppingCartManager
{
   static Scanner scnr = new Scanner(System.in);
   public static void main(String[] args)
   {
       String customerName;
       String currentDate;
      
       System.out.println("Enter Customer's Name:");
       customerName = scnr.nextLine();
      
       System.out.println("Enter Today's Date:");
       currentDate = scnr.nextLine();
       ShoppingCart cartItems = new ShoppingCart(customerName, currentDate);
      
       System.out.println("");
       System.out.println("Customer Name: " + customerName);
       System.out.println("Today's Date: " + currentDate);
      
       printMenu(cartItems);
   }
      
   public static void printMenu(ShoppingCart cartItems)
   {  
       char select;
       String userInput;
       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\n");
       System.out.println("Choose an option:");
      
       select = scnr.nextLine().charAt(0);
      
       while (select != 'q' && select != 'a' && select != 'd' && select != 'c' && select != 'i' && select != 'o')
       {
           System.out.println("Choose an option:");
           select = scnr.nextLine().charAt(0);
       }
  
       switch (select)
       {
           case 'q':
           System.exit(0);
           break;
          
           // i - Output items' Description
           case 'i':
           System.out.println("OUTPUT ITEMS' DESCRIPTIONS");
           cartItems.printDescriptions();
          
           break;
          
           // o - Output shopping cart
           case 'o':
           System.out.println("OUTPUT SHOPPING CART");
           cartItems.printTotal();
  
           break;
          
           //a - Add item to cart
           case 'a':
           String Name;
           String Description;
           int Price;
           int Quantity;
           System.out.println("");
           System.out.println("ADD ITEM TO CART");
           System.out.println("Enter the item name:");
           Name = scnr.nextLine().trim().toString();
          
           System.out.println("Enter the item description:");
           Description = scnr.nextLine().trim().toString();
          
           System.out.println("Enter the item price:");
           Price = scnr.nextInt();
           scnr.nextLine();
          
           System.out.println("Enter the item quantity:");
           Quantity = scnr.nextInt();
           scnr.nextLine();
          
           ItemToPurchase itemToPurchase = new ItemToPurchase(Name, Description, Price, Quantity);
           cartItems.addItem(itemToPurchase);
          
           break;
          
           // d- remove item from cart
           case 'd':
           String remove;
           System.out.println("");
           System.out.println("REMOVE ITEM FROM CART");
           System.out.println("Enter name of item to remove:");
           remove = scnr.nextLine();
           cartItems.removeItem(remove);
          
           break;
          
           // c - Change item quantity
           case 'c':
           int quant;
           String Name2;
           System.out.println("CHANGE ITEM QUANTITY");
           System.out.println("Enter the item name:");
           Name2 = scnr.nextLine();
           System.out.println("Enter the new quantity:");
           quant = scnr.nextInt();
           scnr.nextLine();
           cartItems.modifyItem(Name2);
          
           break;
       }
       printMenu(cartItems);
   }
}

Output:

Enter Customer's Name:
John Doe
Enter Today's Date:
February 1, 2016

Customer Name: John Doe
Today's Date: February 1, 2016

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

Choose an option:
a

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

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

Choose an option:
o
OUTPUT SHOPPING CART
John DoeShopping CartFebruary 1, 2016
Number of items: 2
Nike Romaleos 2@ $189 = $378

Total:$378

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

Choose an option:
q


Solving your question and helping you to well understand it is my focus. So if you face any difficulties regarding this please let me know through the comments. I will try my best to assist you. However if you are satisfied with the answer please don't forget to give your feedback.
Thank you.


Related Solutions

9.25 LAB*: Program: Online shopping cart (Part 2) Note: Creating multiple Scanner objects for the same...
9.25 LAB*: Program: Online shopping cart (Part 2) Note: Creating multiple Scanner objects for the same input stream yields unexpected behavior. Thus, good practice is to use a single Scanner object for reading input from System.in. That Scanner object can be passed as an argument to any methods that read input. This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Private fields string itemDescription -...
USING THE IF STATEMENT & Scanner for input and println for output - Write a java...
USING THE IF STATEMENT & Scanner for input and println for output - Write a java program where the user enters a temperature as a whole number from input, and outputs a “most likely” season [either SUMMER, SPRING, FALL or WINTER] depending on the temperature entered. SUMMER would be 90 or higher SPRING   would be 70 to less than 90 FALL       would be 50 to less than 70 WINTER would be less than 50 Consider it an error if the...
User the Scanner class for your input Write a java program to calculate the area of...
User the Scanner class for your input Write a java program to calculate the area of a rectangle. Rectangle Area is calculated by multiplying the length by the width   display the output as follow: Length =   Width = Area = Load: 1. Design (Pseudocode ) 2. Source file (Java file, make sure to include comments) 3. Output file (word or pdf or jpig file)
Provide a formatted printing statement that yields $▯▯323.500 for input 323.5. Note that ▯ means a...
Provide a formatted printing statement that yields $▯▯323.500 for input 323.5. Note that ▯ means a space.
What is the weakness about using the same key stream multiple times for encryption in a...
What is the weakness about using the same key stream multiple times for encryption in a stream cipher? How can it be solved in practice?
Write a JAVA program that compares two strings input to see if they are the same?
Write a JAVA program that compares two strings input to see if they are the same?
Java Program General Scenario: You are creating an interactive application that gets input from the keyboard...
Java Program General Scenario: You are creating an interactive application that gets input from the keyboard and keeps track of a grocery store that a businessman runs. In order to develop this application, you will be using 2 interfaces, 1 abstract class, and 1 concrete class. You should use the interfaces and the abstract class to create the concrete class. Your application should have overridden toString() at appropriate places so that you could display the object state, i.e., the string...
Java Programming (8th Edition) Joyce Farrell p.530, Chapter 10 Problem 1E with user input with scanner...
Java Programming (8th Edition) Joyce Farrell p.530, Chapter 10 Problem 1E with user input with scanner and the following results: Enter name of horse: ABC Enter color of horse: Red Enter year: 2000 Enter name of horse: DEF Enter color of horse: Blue Enter year: 2010 Enter number of races: 6 ABC is red and born in 2000. DEF is blue and born in 2010. DEF completed 6 races..
java please Write a program that creates an ArrayList and adds 5 circle objects to the...
java please Write a program that creates an ArrayList and adds 5 circle objects to the list , and display all elements in the list by invoking the object’s toString() method.
JAVA, this is practice to better understand java. //Please note throughout if possible looking to better...
JAVA, this is practice to better understand java. //Please note throughout if possible looking to better understand the process. Write the following class: Person. Define Class Person Write the class header Class Variables Class Person is to have the following data members: firstName of type String, lastName of type String representing a person’s first and last names and ssn of type int representing a social security number. Each data member has an access specifier of type private. Constructors Class Person...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT