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

Create a class Item, representing a sales item as specified in the UML-diagram t

ID: 3769674 • Letter: C

Question

Create a class Item, representing a sales item as specified in the UML-diagram to the right. The two fields should be initialized in a constructor. The method toString should return a string representing the information about a sales item eg.: Create a class Driver and create a method herein named test. This method should be static, have return type void and take no parameters Create two Item-objects H and i2 in the test-method and write these to the terminal via their toString-method. Create a new class: ShoppingCart representing a shopping cart to contain sales items. The relationship between ShoppingCart and Item is shown in the UML diagram below: Implement the methods addltem and removeltem so they can add/remove the Item-object p to/from the ShoppingCart-object Create a ShoppingCart-object in the test-method in your Driver-class and add the two existing Item-objects to the shopping cart Implement the method findMostExpensive; it should return the most expensive iterm-object contained in the ShoppingCart-object. If no sales items are in the shopping cart - return null. Test if the method works by expanding the test-method to use it and show the results in the terminal Use findMostExpensive (in ShoppingCart) and toString (in Item) in the test-method of your Driver-class to print out the most expensive sales item of i1 and i2 Let the Item-class implement the interface Comparable, so that the Item-objects will be sorted by their price. If two sales items have the same price, they should be sorted by their name instead. Test if the implementation works by expanding the test-method to use it and show the results in the terminal

Explanation / Answer

import java.text.NumberFormat;

public class ShoppingCart

{

    private int itemCount;      // total number of items in the cart

    private double totalPrice;  // total price of items in the cart

    private int capacity;       // current cart capacity

    //Declare an instance variable cart to be an array of Items and instantiate cart in the constructor to be an array holding capacity Items.

    private Item[] cart = new Item[capacity];

    // -----------------------------------------------------------

    // Creates an empty shopping cart with a capacity of 5 items.

    // -----------------------------------------------------------

    public ShoppingCart()

    {

        capacity = 5;

        itemCount = 0;

        totalPrice = 0.0;

    }

    // -------------------------------------------------------

    // Adds an item to the shopping cart.

    // Add the item to the cart and update the totalPrice instance variable (note this variable takes into account the quantity).

    // -------------------------------------------------------

  

    public void addToCart(String itemName, double price, int quantity)

    {

        if (itemCount == capacity)

        {

            increaseSize();

            cart[itemCount] = new Item(itemName, price, quantity);

            totalPrice += (totalPrice * quantity);

            itemCount++;

        }

    }

    // -------------------------------------------------------

    // Returns the contents of the cart together with

    // summary information.

    // -------------------------------------------------------

    public String toString()

    {

        NumberFormat fmt = NumberFormat.getCurrencyInstance();

        String contents = " Shopping Cart ";

        contents += " Item Unit Price Quantity Total ";

        for (int i = 0; i < itemCount; i++)

            contents += cart[i].toString() + " ";

        contents += " Total Price: " + fmt.format(totalPrice);

        contents += " ";

        return contents;

    }

    // ---------------------------------------------------------

    // Increases the capacity of the shopping cart by 3

    // ---------------------------------------------------------

    private void increaseSize ()

    {

        Item[] temp = new Item[cart.length + 3];

        for (int num = 0; num < cart.length; num++)

        {

            temp[num] = cart[num];

            cart = temp;

        }

    }

}

import java.text.NumberFormat;

public class Item

{

    private String name;

    private double price;

    private int quantity;

    // -------------------------------------------------------

    // Create a new item with the given attributes.

    // -------------------------------------------------------

    public Item (String itemName, double itemPrice, int numPurchased)

    {

        name = itemName;

        price = itemPrice;

        quantity = numPurchased;

    }

    // -------------------------------------------------------

    //   Return a string with the information about the item

    // -------------------------------------------------------

    public String toString ()

    {

        NumberFormat fmt = NumberFormat.getCurrencyInstance();

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

            + fmt.format(price*quantity));

    }

    // -------------------------------------------------

    //   Returns the unit price of the item

    // -------------------------------------------------

    public double getPrice()

    {

        return price;

    }

    // -------------------------------------------------

    //   Returns the name of the item

    // -------------------------------------------------

    public String getName()

    {

        return name;

    }

    // -------------------------------------------------

    //   Returns the quantity of the item

    // -------------------------------------------------

    public int getQuantity()

    {

        return quantity;

    }

}

import java.text.NumberFormat;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote