Question

Write code in Java programming language. The ShoppingCart class will be composed with an array of...

Write code in Java programming language.

The ShoppingCart class will be composed with an array of Item objects. You do not need to

implement the Item class for this question, only use it. Item has a getPrice() method that returns a

float and a one-argument constructor that takes a float that specifies the price. The ShoppingCart

class should have:

Constructors: A no-argument and a single argument that takes an array of Items.

Fields:

• Items: an array of Item objects (maximum 10 objects).

• numItems: An int that tracks the number of items in the users cart.

• taxRate: A double representing the tax rate to be applied, defaulted to .05

Methods:

• Setters and getters for numItems and taxRate

• addItem(Item i): Adds an item to the cart. Returns a boolean. If the cart is full, return

false and do not add the item to the cart. Otherwise, add the item to the array and return

true.

• subtotal(): Returns a float representing the combined cost of all the items in the cart not

including tax.

• total(): Returns a float representing the subtotal plus tax. The tax amount is subtotal

times the taxRate.

Write a class called ‘TestShoppingCart’ with a main method that does the following:

1. Create a ShoppingCart object.

2. Adds 10 Items to it, each item should have a randomly generated price between $0 and

$100.

3. Print the subtotal, tax and total dollar amount that the customer would have to pay

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// ShoppingCart.java

import java.util.*;

public class ShoppingCart

{

//declaring variables

List<ShoppingCartItem> items;

int numberOfItems;

double total;

//default constructor

public ShoppingCart(){

items = new ArrayList<ShoppingCartItem>();

numberOfItems = 0;

total = 0;

   }

  public  void addItem(Product product) {

        boolean newItem = true;

for (ShoppingCartItem scItem : items)

{

if (scItem.getProduct().getId() == product.getId())

{

              newItem = false;

              scItem.incrementQuantity();

}

}

if (newItem) {

ShoppingCartItem scItem = new ShoppingCartItem(product);

items.add(scItem);

}

}

public void update(Product product, String quantity) {

int qty = -1;

qty = Integer.parseInt(quantity);

        if (qty >= 0)

  {            

ShoppingCartItem item = null;

for (ShoppingCartItem scItem : items) {

if (scItem.getProduct().getId() == product.getId()) {

if (qty != 0) {

// set item quantity to new value

scItem.setQuantity(qty);

}

else

{

item = scItem;

break;

}

}

}

if (item != null) {

// remove from cart

items.remove(item);

}

}

}

public s List<ShoppingCartItem> getItems() {

return items;

}

public synchronized int getNumberOfItems()

{

numberOfItems = 0;

for (ShoppingCartItem scItem : items)

{

numberOfItems += scItem.getQuantity();

}

return numberOfItems;

}

public  double getSubtotal() {

        double amount = 0;

for (ShoppingCartItem scItem : items)

{

Product product = (Product) scItem.getProduct();

amount += (scItem.getQuantity() * product.getPrice().doubleValue());

}

return amount;

}

public  double getTotal() {

         double amount = 0;

                   double tax=this.getSubtotal()*0.05;

                   amount += tax;

                    total = amount;

                    return total;

}

public  void clear() {

items.clear();

numberOfItems = 0;

total = 0;

}

}

Add a comment
Know the answer?
Add Answer to:
Write code in Java programming language. The ShoppingCart class will be composed with an array of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in java PART ONE ======================================= Below is the "RetailItem" class definition that we used in Chapter 6. Write a "CashRegister" class that leverages this "Ret...

    in java PART ONE ======================================= Below is the "RetailItem" class definition that we used in Chapter 6. Write a "CashRegister" class that leverages this "RetailItem" class which simulates the sale of a retail item. It should have a constructor that accepts a "RetailItem" object as an argument. The constructor should also accept an integer that represents the quantity of items being purchased. Your class should have these methods: getSubtotal: returns the subtotal of the sale, which is quantity times price....

  • Create a Java application, that support the following: Create an Employee class, which holds following information:...

    Create a Java application, that support the following: Create an Employee class, which holds following information: Employee First Name Employee Last Name Employee Phone Number Employee Address Employee id Employee Title Employee salary Create an application that uses the Employee class Constructors –Getters and setters A minimum of 3 constructors including default constructor equals() method Helper methods toString() method Create an array of 5 Employee objects Use a Scanner to read in 5 employee information Change 2 employees information (3-items...

  • Write the following program in Java using Eclipse. A cash register is used in retail stores...

    Write the following program in Java using Eclipse. A cash register is used in retail stores to help clerks enter a number of items and calculate their subtotal and total. It usually prints a receipt with all the items in some format. Design a Receipt class and Item class. In general, one receipt can contain multiple items. Here is what you should have in the Receipt class: numberOfItems: this variable will keep track of the number of items added to...

  • java A University would like to implement its students registery as a binary search tree, calledStudentBST....

    java A University would like to implement its students registery as a binary search tree, calledStudentBST. Write an Student node class, called StudentNode, to hold the following information about an Student: - id (as a int) - gpa (as double) StudentNode should have constructors and methods (getters, setters, and toString()) to manage Write the StudentBST class, which is a binary search tree to hold objects of the class StudentNode. The key in each node is the id. : import.java.ArrayList; public...

  • Write code to create a Java class called "Student". The class should hold information about a...

    Write code to create a Java class called "Student". The class should hold information about a student: name, id and whether or not the student is currently registered. There should be a constructor that takes name, id and registration status. Add getters and setters for the three properties. Make sure that you use appropriate visibility modifiers (public vs. private).

  • Java programming The purpose of this problem is to practice using a generic Urn class. NOTE:...

    Java programming The purpose of this problem is to practice using a generic Urn class. NOTE: Refer to the code for the ArrayStack class from Chapter 12. Use that code as a starting point to create the Urn class, modifying it to remove the methods in the ArrayStack class (push, pop, etc) then add the methods described below. Also change the variable names to reflect the new class. For example the array name should NOT be stack, instead it should...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • Java Programming Question

    After pillaging for a few weeks with our new cargo bay upgrade, we decide to branch out into a new sector of space to explore and hopefully find new targets. We travel to the next star system over, another low-security sector. After exploring the new star system for a few hours, we are hailed by a strange vessel. He sends us a message stating that he is a traveling merchant looking to purchase goods, and asks us if we would...

  • In Java programming language Please write code for the 6 methods below: Assume that Student class...

    In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...

  • Complete an Array-Based implementation of the ADT List including a main method and show that the...

    Complete an Array-Based implementation of the ADT List including a main method and show that the ADT List works. Draw a class diagram of the ADT List __________________________________________ public interface IntegerListInterface{ public boolean isEmpty(); //Determines whether a list is empty. //Precondition: None. //Postcondition: Returns true if the list is empty, //otherwise returns false. //Throws: None. public int size(); // Determines the length of a list. // Precondition: None. // Postcondition: Returns the number of items in this IntegerList. //Throws: None....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT