Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8134313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:58:20+00:00 2026-06-06T09:58:20+00:00

I have the following assignment: Implement a basic shopping basket without using any predefined

  • 0

I have the following assignment:

Implement a basic shopping basket without using any predefined
collection libraries. Please comment your code to support your design
decision and any assumption that you make. Your shopping basket must
support the following two methods:-

  1. void add(Item i, int n) - adds n copies of i to the basket
  2. int totalPrice() – compute the total price of the basket. The
    total price must be returned in constant time; however, void add(Item
    i, int n)
    does not need to be returned in constant time.

I have implemented the shopping class like this but don’t getting the clue how to implement totalPrice method.

public class Shopping {

public void add(Item i, int n){
    int totalCost = (int) (i.getItemPrice()*n);
}


public static void main(String arg[]){
    Item item = new Item();
    item.setItemPrice(10);
    Shopping shopping = new Shopping();
    shopping.add(item,4);
}

}

I have been asked this in a test. Does anyone can give me some idea how this can be done?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:58:21+00:00Added an answer on June 6, 2026 at 9:58 am

    You can declare a member variable in the class, that you can used to store the total price in the basket. And every time you add items to the basket you can update the value of this variable.
    Let’s say we add a member variable:

    private int total;
    

    Then modify your add(…) method to add the value of totalCost to total and the only thing you should do in your totalPrice() method is return the value of total. You can initialize the variable total in the constructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Tkinter in a programming assignment and have the following problem. I
I have an assignment where I need to implement church numerals in SML using
I have the following assignment: Write a complete 8086 program to perform the calculator
For a programming assignment, we have the following requirements: It needs to be a
In my assignment i have a problem with reading a file. See the following
I have the following code that I am working on for my college assignment.
I have an assignment that requires us to implement a doubly linked list class.
I have the following assignment for homework. Requirements Design a class called TokenGiver with
I have as an assignment to implement something like a 3-way mergeshort in java.
I Have the following code which fires OnCreatedUser and doesn't throw any errors. Profile.Title

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.