Question

[JAVA] If you have a class called ShoppingCart, List at least three API (method) that helps...

[JAVA]

If you have a class called ShoppingCart, List at least three API (method) that helps manage the ShoppingCart.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.ArrayList;

class ShoppingCart {

    private ArrayList<String> items;

    public ShoppingCart() {
        items = new ArrayList<>();
    }

    public void addItem(String item) {
        items.add(item);
    }

    public void removeItem(String item) {
        items.remove(item);
    }

    public void clearCart() {
        items.clear();
    }

    public int cartSize() {
        return items.size();
    }

    public void printAllItems() {
        System.out.print("Items in shopping cart are: ");
        for (int i = 0; i < items.size(); i++) {
            System.out.print(items.get(i) + " ");
        }
        System.out.println();
    }
}

class ShoppingCartTest {

    public static void main(String[] args) {
        ShoppingCart cart = new ShoppingCart();
        cart.addItem("headphones");
        cart.addItem("laptop");
        cart.addItem("table");
        System.out.println("Number of items in cart: " + cart.cartSize());
        cart.printAllItems();
    }
}
Add a comment
Know the answer?
Add Answer to:
[JAVA] If you have a class called ShoppingCart, List at least three API (method) that helps...
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
  • Java Concurrency! You have been given a simple API to buy and print postage. Write a...

    Java Concurrency! You have been given a simple API to buy and print postage. Write a program that submits each incoming order to the API and then prints the shipping label. Unfortunately each call to the shipping label API is SLOW… To compensate for this, use concurrency so that your program can be making several calls to the API at one time. We also need to keep track of our expenses, so make sure to calculate how much money is...

  • 6. DataBuffer is a class from the Java API that is used to store one or...

    6. DataBuffer is a class from the Java API that is used to store one or more arrays of the same type of data and the same size together into a single object. These individual arrays are referred to as "banks" in the documentation. Use the Java API to answer the questions below. Ignore the use of the word "abstract" in the API (you'll learn what this means if you take CS 2334 Programming Structures and Abstractions). a. Give the...

  • Java In your own custom ArrayList<E> class(do not use Java Array List API) use Array algorithm...

    Java In your own custom ArrayList<E> class(do not use Java Array List API) use Array algorithm Write a static function called RemoveNullElements() without creating another array. that remove Null elements and shift all the rest of the elements to the left/front (small indexes) of the array without changing the size or length of the original array( this meant the front of the array will have all the not Null elements, and the other haft will be empty/Null. Please provide test...

  •            1.         You often need to know the inheritance ____________________ of the Java API to work...

               1.         You often need to know the inheritance ____________________ of the Java API to work with its classes and subclasses.            2.         All objects have access to the methods of the ____________ class.            3.         If two classes share some common elements, you can define those elements in a ____________.            4.         To call a constructor or method of a superclass from a subclass, you use the ____________ keyword.            5.         A class that can be inherited by another...

  • Go to the Java API and review the Rectangle class briefly. As directed below, create a...

    Go to the Java API and review the Rectangle class briefly. As directed below, create a subclass of the Rectangle class called BetterRectangle as described below. Create another class called RectangleTester, which fully tests the BetterRectangle, by displaying a menu that allows a user to process data for multiple rectangles until they choose to quit. Be sure to include sample runs as block comments in your tester source code file. P9.10 The Rectangle class of the standard Java library does...

  • Implement the AutoShop class described by this API. You do not have to include javadoc comments....

    Implement the AutoShop class described by this API. You do not have to include javadoc comments. The programming question asks you to implement a class that represents a auto shop that has an owner. The auto shop has a collection of vehicles, but the choice of which collection to use is left up to the student. In fact, students might choose to use more than one collection to implement the class. Students should analyze the API of the AutoShop class...

  • Working in java programming. I have a class (call it manage) that takes in an arraylist...

    Working in java programming. I have a class (call it manage) that takes in an arraylist (list) and a boolean (switch), which is created/preset in the parent main function above this class. The arraylist is modified and returned to the main function however, hence it's name 'manage.' I need help declaring the arraylist in the constructor? public class manage{ public manage(){ //Constructor // What is arraylist construction? this.switch = false; } public manage(ArrayList<Integer> list, boolean switch) { this.list = list;...

  • You are given a Node class and a List class: Write a java method insertNodeBefore() which...

    You are given a Node class and a List class: Write a java method insertNodeBefore() which takes two integers (int ref which is the data of the node that we need to insert a new node before, and int d which is the data of the new node we want to insert), for the list mylist.

  • For this question you must write a java class called Rectangle and a client class called...

    For this question you must write a java class called Rectangle and a client class called RectangleClient. The partial Rectangle class is given below. (For this assignment, you will have to submit 2 .java files: one for the Rectangle class and the other one for the RectangleClient class and 2 .class files associated with these .java files. So in total you will be submitting 4 files for part b of this assignment.) // A Rectangle stores an (x, y) coordinate...

  • Anyone helps me in a Java Languageif it it is possible thanks. Write a class called...

    Anyone helps me in a Java Languageif it it is possible thanks. Write a class called Player that holds the following information: Team Name (e.g., Ravens) . Player Name (e.g., Flacco) . Position's Name (e.g. Wide reciver) . Playing hours per week (e.g. 30 hours per week). Payment Rate (e.g., 46 per hour) . Number of Players in the Team (e.g. 80 players) . This information represents the class member variables. Declare all variables of Payer class as private except...

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