Question

In: Computer Science

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 - 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:

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

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. (1 pt)

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. (3 pts)

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. (3 pts)

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. (2 pts)

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. (3 pts)

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. (4 pts)

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. (5 pts)

Ex:

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

First code below, please help me edit this code to fit the parameters set above.:

ShoppingCartPrinter.java

import java.util.Scanner;

public class ShoppingCartPrinter {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
  
ItemToPurchase item1 = new ItemToPurchase();
ItemToPurchase item2 = new ItemToPurchase();

System.out.println("Item 1");
System.out.println("Enter the item name:");
String name1 = scan.nextLine();

System.out.println("Enter the item price:");
int price1 = scan.nextInt();

System.out.println("Enter the item quantity:");
int quantity1 = scan.nextInt();

item1.setName(name1);
item1.setPrice(price1);
item1.setQuantity(quantity1);
scan.nextLine();

System.out.println("");

System.out.println("Item 2");
System.out.println("Enter the item name:");
String name2 = scan.nextLine();

System.out.println("Enter the item price:");
int price2 = scan.nextInt();

System.out.println("Enter the item quantity:");
int quantity2 = scan.nextInt();
System.out.println("");

item2.setName(name2);
item2.setPrice(price2);
item2.setQuantity(quantity2);

System.out.println("TOTAL COST");

int item1Total = item1.getPrice() * item1.getQuantity();
int item2Total = item2.getPrice() * item2.getQuantity();

System.out.println(item1.getName()+" "+item1.getQuantity()+" @ $"+item1.getPrice()+" = $"+item1Total);
System.out.println(item2.getName()+" "+item2.getQuantity()+" @ $"+item2.getPrice()+" = $"+item2Total);
System.out.println();
System.out.println("Total: $"+(item1Total + item2Total));

}

}

ItemToPurchase.java

public class ItemToPurchase {
private String itemName ;
private int itemPrice;
private int itemQuantity;
public ItemToPurchase(){
itemName = "none";
itemPrice = 0;
itemQuantity = 0;
}
public String getName() {
return itemName;
}
public void setName(String itemName) {
this.itemName = itemName;
}
public int getPrice() {
return itemPrice;
}
public void setPrice(int itemPrice) {
this.itemPrice = itemPrice;
}
public int getQuantity() {
return itemQuantity;
}
public void setQuantity(int itemQuantity) {
this.itemQuantity = itemQuantity;
}
  
}

Solutions

Expert Solution

Short Summary:

Implemented the program as per requirement
Attached source code and sample output


**************Please do upvote to appreciate our time. Thank you!******************

Source Code:

ItemToPurchase.java

public class ItemToPurchase {
   private String itemDescription;
   private String itemName ;
   private int itemPrice;
   private int itemQuantity;
  
   public ItemToPurchase(){
       itemDescription="none";
       itemName = "none";
       itemPrice = 0;
       itemQuantity = 0;
   }

   //Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)

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

   //printItemCost() - Outputs the item name followed by the quantity, price, and subtotal

   void printItemCost()
   {
       //Eg., Bottled Water 10 @ $1 = $10
       System.out.println(itemName+" "+itemQuantity+" @ $"+itemPrice+" = $"+(itemPrice*itemQuantity));
   }

   //printItemDescription() - Outputs the item name and description

   void printItemDescription()
   {
       //Bottled Water: Deer Park, 12 oz.

       System.out.println(itemName+": "+itemDescription);
   }

   public String getName() {
       return itemName;
   }
   public String getItemDescription() {
       return itemDescription;
   }

   public void setItemDescription(String itemDescription) {
       this.itemDescription = itemDescription;
   }

   public void setName(String itemName) {
       this.itemName = itemName;
   }
   public int getPrice() {
       return itemPrice;
   }
   public void setPrice(int itemPrice) {
       this.itemPrice = itemPrice;
   }
   public int getQuantity() {
       return itemQuantity;
   }
   public void setQuantity(int itemQuantity) {
       this.itemQuantity = itemQuantity;
   }

}


ShoppingCart.java

package com.core.java;

import java.util.ArrayList;

//This class holds customer info and cart details
public class ShoppingCart {

   private String customerName;
   private String currentDate;
   ArrayList<ItemToPurchase> cartItems=new ArrayList<>();

   //Default cons
   public ShoppingCart()
   {
       customerName="none";
       currentDate="January 1, 2016";
   }
//Parameter cons
   public ShoppingCart(String customerName, String currentDate) {
       super();
       this.customerName = customerName;
       this.currentDate = currentDate;
   }
//Accessors
   public String getCustomerName() {
       return customerName;
   }

   public String getCurrentDate() {
       return currentDate;
   }

   //This method is used to add item to cart
   public void addItem(ItemToPurchase item)
   {
       cartItems.add(item);
   }
   //This method is used to remove item to cart

   public void removeItem(String itemName)
   {
       boolean found=false;
       ItemToPurchase item=null;
       for(ItemToPurchase item1:cartItems)
       {
           if(item1.getName().equalsIgnoreCase(itemName))
           {
               found=true;
               item=item1;
           }
       }
       cartItems.remove(item);
       if(!found)
           System.out.println("Item not found in cart. Nothing removed.");
   }

   //Modifies an item's description, price, and/or quantity. Has parameter ItemToPurchase. Does not return anything
   public void modifyItem(ItemToPurchase item)
   {
       boolean found=false;
       for(ItemToPurchase itemObj:cartItems)
       {
           if(itemObj.getName().equalsIgnoreCase(item.getName()))
           {
               found=true;
               if(!(item.getItemDescription().equalsIgnoreCase("none") && item.getPrice()==0 && item.getQuantity()==0))
               {
                   itemObj.setQuantity(item.getQuantity());
               }
           }
       }
       if(!found)
           System.out.println("Item not found in cart. Nothing modified.");
   }

   //Returns quantity of all items in cart. Has no parameters.

   public int getNumItemsInCart()
   {
       int sum=0;
       for(ItemToPurchase itemObj:cartItems)
       {
           sum=sum+itemObj.getQuantity();
       }
       return sum;
   }

   //Determines and returns the total cost of items in cart. Has no parameters.
   public double getCostOfCart()
   {
       double cost=0;
       for(ItemToPurchase itemObj:cartItems)
       {
           cost=cost+itemObj.getPrice();
       }
       return cost;
   }


   //Outputs total of objects in cart.

   public void printTotal()
   {
       if(cartItems.isEmpty())
           System.out.println("SHOPPING CART IS EMPTY");
       else
       {
           System.out.println(customerName+"'s Shopping Cart - "+currentDate);
           System.out.println("Number of Items: "+getNumItemsInCart());
           for(ItemToPurchase itemObj:cartItems)
           {
               itemObj.printItemCost();
           }
           System.out.println("Total: $"+getCostOfCart());
       }
   }

   //Outputs each item's description.

   public void printDescriptions()
   {

       System.out.println(customerName+"'s Shopping Cart - "+currentDate);
       System.out.println("Item Descriptions");
       for(ItemToPurchase itemObj:cartItems)
       {
           System.out.println(itemObj.getItemDescription());
       }
   }

}

ShoppingCartManager.java

package com.core.java;

import java.util.Scanner;


/** This class gets user input and modifies shopping cart**/
public class ShoppingCartManager {

   static Scanner input=new Scanner(System.in);

   public static void main(String[] args) {

       //Scanner to get user input

       System.out.println("Enter Customer's Name:");
       String custName=input.nextLine(); //Get customer name
       System.out.println("Enter Today's Date:");
       String date=input.nextLine(); //Get date
       System.out.println("Customer Name: "+custName);//Print
       System.out.println("Today's Date: "+date);
       ShoppingCart cart=new ShoppingCart(custName, date); //Create cart
       //Print menu to user
       printMenu(cart);
       input.close();

   }

   //This method displays menu to user
   static void printMenu(ShoppingCart cart)
   {
       //Initialize to any char
       char userInput='a';
       //Loop until user enters quit
       while(userInput!='Q')
       {
           System.out.println("MENU\r\n" +
                   "a - Add item to cart\r\n" +
                   "d - Remove item from cart\r\n" +
                   "c - Change item quantity\r\n" +
                   "i - Output items' descriptions\r\n" +
                   "o - Output shopping cart\r\n" +
                   "q - Quit\r\n" +
                   "\r\n" +
                   "Choose an option: ");
           //Get input
           userInput=input.nextLine().charAt(0);
           //Switch for different inputs
           switch(userInput)
           {
           case 'o':
               System.out.println("OUTPUT SHOPPING CART");
               cart.printTotal();
               break;
           case 'i':
               System.out.println("OUTPUT ITEMS' DESCRIPTIONS");
               cart.printDescriptions();
               break;
           case 'a':
               System.out.println("Enter the item name:");
               String itemName=input.nextLine();
               System.out.println("Enter the item description:");
               String desc=input.nextLine();
               System.out.println("Enter the item price:");
               int price=input.nextInt();
               input.nextLine();
               System.out.println("Enter the item quantity:");
               int quan=input.nextInt();
               input.nextLine();//skip line
               ItemToPurchase item=new ItemToPurchase(desc, itemName, price, quan); //create new object
               cart.addItem(item); //add to cart
               break;
           case 'd':
               System.out.println("REMOVE ITEM FROM CART" );
               System.out.println("Enter name of item to remove:");
               String itemNm=input.nextLine();
               cart.removeItem(itemNm);
           case 'c':
               System.out.println("CHANGE ITEM QUANTITY\n" +
                       "Enter the item name:\n" );
               itemName=input.nextLine();
               System.out.println("Enter the new quantity:\n" );
               quan=input.nextInt();
               input.nextLine();
               ItemToPurchase item1=new ItemToPurchase();
               item1.setQuantity(quan);
               item1.setName(itemName);
               cart.modifyItem(item1);

           }
       }
       input.close();

   }
}

Output:

**************Please do upvote to appreciate our time. Thank you!******************


Related Solutions

IN C++ 7.22 LAB*: Program: Online shopping cart (Part 2) This program extends the earlier "Online...
IN C++ 7.22 LAB*: Program: Online shopping cart (Part 2) This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt) Public member functions SetDescription() mutator & GetDescription() accessor (2 pts) PrintItemCost() - Outputs the item name followed by the quantity, price, and subtotal PrintItemDescription() - Outputs the...
7.7 Ch 7 Program: Online shopping cart (continued) (C) This program extends the earlier "Online shopping...
7.7 Ch 7 Program: Online shopping cart (continued) (C) This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase struct to contain a new data member. (2 pt) char itemDescription[ ] - set to "none" in MakeItemBlank() Implement the following related functions for the ItemToPurchase struct. PrintItemDescription() Has an ItemToPurchase parameter. Ex. of PrintItemDescription() output: Bottled Water: Deer Park, 12 oz. (2) Create three new files: ShoppingCart.h - struct definition and...
This week you will write a program that mimics an online shopping cart . You will...
This week you will write a program that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. It should include all of the following: The basics - Add items to a cart until the user says "done" and then print out a list of items in the cart and a total price. - Ensure the user types in numbers for quantities -...
Python This week you will write a program in Python that mimics an online shopping cart...
Python This week you will write a program in Python that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. It should include all of the following: The basics - Add items to a cart until the user says "done" and then print out a list of items in the cart and a total price. - Ensure the user types in numbers...
Python This week you will write a program in Pyhton that mimics an online shopping cart...
Python This week you will write a program in Pyhton that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. It should include all of the following: The basics - Add items to a cart until the user says "done" and then print out a list of items in the cart and a total price. - Ensure the user types in numbers...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1)...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class to contain a new attribute. (2 pts) item_description (string) - Set to "none" in default constructor Implement the following method for the ItemToPurchase class. print_item_description() - Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter. Ex. of print_item_description() output: Bottled Water: Deer Park, 12 oz. (2) Build the ShoppingCart class with the following data attributes and related methods....
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1)...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class to contain a new attribute. (2 pts) item_description (string) - Set to "none" in default constructor Implement the following method for the ItemToPurchase class. print_item_description() - Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter. Ex. of print_item_description() output: Bottled Water: Deer Park, 12 oz. (2) Build the ShoppingCart class with the following data attributes and related methods....
In python 3 please: This program extends the earlier "Online shopping cart" program. (Start working from...
In python 3 please: This program extends the earlier "Online shopping cart" program. (Start working from your Program 7). (1) Build the ShoppingCart class with the following data attributes and related methods. Note: Some can be method stubs (empty methods) initially, to be completed in later steps.(3 pt) ● Parameterized constructor which takes the customer name and date as parameters ● Attributes ○ customer_name (string) ○ current_date (string) ○ cart_items (list) ● Methods ○ add_item() ■ Adds an item to...
C++.This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1)...
C++.This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt) Public member functions 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 Private data members string itemDescription -...
Binary Search Tree (Part 1): Note: This is the first part of a 2-part lab. Only...
Binary Search Tree (Part 1): Note: This is the first part of a 2-part lab. Only this part is due on Oct 29. But please get it working or you will struggle with the second part. For the second part, the data type will be changing. For now, it is a string. Contents Explanation: 2 BST.cpp: 2 Code you must write: 2 bool insert(string s) (7): 2 TNode *find(string s) (4): 2 void printTreeIO(Tnode *n)(3): 2 void printTreePre(Tnode *n) (3):...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT