Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(JAVA) 15.7 Program: Shopping Cart LinkedList Redo Chapter 7\'s program using th

ID: 3573385 • Letter: #

Question

(JAVA)

15.7 Program: Shopping Cart LinkedList

Redo Chapter 7's program using the LinkedList collection type in the ShoppingCart class rather than the ArrayList type.

The interface and output should be identical to the Chapter 7 Assignment.

Online shopping cart (Java) Chapter 7.20

(1) Create two files to submit:

ItemToPurchase.java - Class definition

ShoppingCartPrinter.java - Contains main() method

Build the ItemToPurchase class with the following specifications:

Private fields

String itemName - Initialized in default constructor to "none"

int itemPrice - Initialized in default constructor to 0

int itemQuantity - Initialized in default constructor to 0

Default constructor

Public member methods (mutators & accessors)

setName() & getName() (2 pts)

setPrice() & getPrice() (2 pts)

setQuantity() & getQuantity() (2 pts)

(2) In main(), prompt the user for two items and create two objects of the ItemToPurchase class. Before prompting for the second item, call scnr.nextLine(); to allow the user to input a new string. (2 pts)

Ex:


(3) Add the costs of the two items together and output the total cost. (2 pts)

Ex:

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:


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:

Explanation / Answer

ShoppingCartPrinter.java

import java.util.LinkedList;

import java.text.NumberFormat;

import java.util.Scanner;

//Class header

public class ShoppingCartPrinter {

    //Start of main method

    public static <Item> void main(String[] args){

        //Declare and instantiate a variable that is an linkedList that can hold Product objects

        LinkedList<Product> item = new LinkedList<Product>();

        //Declare necessary local variables here

        String Name = null;

        double Price = 0;

        int Quantity = 0;

        Scanner scan = new Scanner(System.in);

// create a do while that will be keep looping as long as user wants to continue shopping

         String keepShopping = "Yes";

         Product item1 = new Product(Name, Price, Quantity);

        //do while loop start

            do

               {

            //Ask user to enter product name and store it in appropriate local variable

            System.out.print("Please Enter the Item Name: ");

            Name = scan.next();

            //Ask user to enter product price and store it in appropriate local variable

            System.out.print("Please Enter the item Price: ");

            Price = scan.nextDouble();

            //Ask user to enter quantity and store it in appropriate local variable

            System.out.print("Please enter the Item Quantity: ");

            Quantity = scan.nextInt();

            // create a new Product object using above inputed values

            Product newitem = new Product(Name, Price, Quantity);

       

            //set the do while loop to continue to loop if Yes option is selected

                 } while (keepShopping.equals("Yes"));

                  // do while loop end

        // print the total price of the shopping cart

    }//end of main method

}//end of Shop class

ItemToPurchase.java

//Represents an item in a shopping cart.

//***************************************************************

import java.text.NumberFormat;

public class ItemToPurchase

{

    private String name;

    private double price;

    private int quantity;

    private double subtotal;

    private int inventory;

    public Product (String name, double price, int quantity)

    {

        this.name = name;

        this.price = price;

        this.quantity = quantity;

        subtotal = price*quantity;

        inventory = 10;

    }

  public Product(String itemName, double itemPrice, int quantity2) {

    }

    public String toString ()

    {

        NumberFormat fmt = NumberFormat.getCurrencyInstance();

        return (“     " + fmt.format(price) + "     " + quantity

                + " " + fmt.format(subtotal));

    }

    //   Returns the unit price of the Product

    public double getPrice()

    {

        return price;

    }

    //   Returns the name of the Product

    public String getName()

    {

        return name;

    }

    //   Returns the quantity of the Product

    public int getQuantity()

    {

        return quantity;

    }

    //   Returns the sub total of the Product

    public double getSubTotal()

    {

        return subtotal;

    }

    public boolean checkInventory()

    {

        boolean flag = false;

        if (inventory > quantity)

        {

            flag = true;

            inventory = inventory - quantity;

            if (inventory <= 0)

                placeOrder(quantity);

        }

        return flag;

    }

    // Replenishes stock to 10 times the quantity of last order

    private void placeOrder(int orderQuantity)

    {

        inventory = orderQuantity * 10;

    }

}//end of class Item

import java.util.LinkedList;

import java.text.NumberFormat;

import java.util.Scanner;

//Class header

public class ShoppingCartPrinter {

    //Start of main method

    public static <Item> void main(String[] args){

        //Declare and instantiate a variable that is an linkedList that can hold Product objects

        LinkedList<Product> item = new LinkedList<Product>();

        //Declare necessary local variables here

        String Name = null;

        double Price = 0;

        int Quantity = 0;

        Scanner scan = new Scanner(System.in);

// create a do while that will be keep looping as long as user wants to continue shopping

         String keepShopping = "Yes";

         Product item1 = new Product(Name, Price, Quantity);

        //do while loop start

            do

               {

            //Ask user to enter product name and store it in appropriate local variable

            System.out.print("Please Enter the Item Name: ");

            Name = scan.next();

            //Ask user to enter product price and store it in appropriate local variable

            System.out.print("Please Enter the item Price: ");

            Price = scan.nextDouble();

            //Ask user to enter quantity and store it in appropriate local variable

            System.out.print("Please enter the Item Quantity: ");

            Quantity = scan.nextInt();

            // create a new Product object using above inputed values

            Product newitem = new Product(Name, Price, Quantity);

       

            //set the do while loop to continue to loop if Yes option is selected

                 } while (keepShopping.equals("Yes"));

                  // do while loop end

        // print the total price of the shopping cart

    }//end of main method

}//end of Shop class

ItemToPurchase.java

//Represents an item in a shopping cart.

//***************************************************************

import java.text.NumberFormat;

public class ItemToPurchase

{

    private String name;

    private double price;

    private int quantity;

    private double subtotal;

    private int inventory;

    public Product (String name, double price, int quantity)

    {

        this.name = name;

        this.price = price;

        this.quantity = quantity;

        subtotal = price*quantity;

        inventory = 10;

    }

  public Product(String itemName, double itemPrice, int quantity2) {

    }

    public String toString ()

    {

        NumberFormat fmt = NumberFormat.getCurrencyInstance();

        return (“     " + fmt.format(price) + "     " + quantity

                + " " + fmt.format(subtotal));

    }

    //   Returns the unit price of the Product

    public double getPrice()

    {

        return price;

    }

    //   Returns the name of the Product

    public String getName()

    {

        return name;

    }

    //   Returns the quantity of the Product

    public int getQuantity()

    {

        return quantity;

    }

    //   Returns the sub total of the Product

    public double getSubTotal()

    {

        return subtotal;

    }

    public boolean checkInventory()

    {

        boolean flag = false;

        if (inventory > quantity)

        {

            flag = true;

            inventory = inventory - quantity;

            if (inventory <= 0)

                placeOrder(quantity);

        }

        return flag;

    }

    // Replenishes stock to 10 times the quantity of last order

    private void placeOrder(int orderQuantity)

    {

        inventory = orderQuantity * 10;

    }

}//end of class Item