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

  • Home
  • SEARCH
  • 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 6830823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:39:50+00:00 2026-05-26T22:39:50+00:00

I tried to debug it but I have no clue what code I’m loosing.

  • 0

I tried to debug it but I have no clue what code I’m loosing. Mind checking up for me? I want to add Item Names, price and quantity into ITEM[] array but when I try to print , it doesn’t return anything

item.java

import java.text.NumberFormat;

public class Item {
    private String name;
    private double price;
    private int quantity;

    public Item(String itemName, double itemPrice, int numPurchased) {
        name = itemName;
        price = itemPrice;
        quantity = numPurchased;
    }

    public String toString() {
        NumberFormat fmt = NumberFormat.getCurrencyInstance();
        return (name + "\t" + fmt.format(price) + "\t" + quantity + "\t"
                + fmt.format(price * quantity));
    }

    public double getPrice() {
        return price;
    }

    public String getName() {
        return name;
    }

    public int getQuantity() {
        return quantity;
    }

}

ShoppingCart.java

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
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.
// -------------------------------------------------------
public void addToCart(String itemName, double price, int quantity)
{if(itemCount==cart.length)
 increaseSize();
else{
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 = "\nShopping Cart\n";
contents += "\nItem\t\tUnit Price\tQuantity\tTotal\n";
for (int i = 0; i < itemCount; i++)
contents += cart[i].toString() + "\n";
contents += "\nTotal Price: " + fmt.format(totalPrice);
contents += "\n";
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;

}

}



}

Shopping1.java

import java.util.Scanner;
public class Shopping1 {


 public static void main(String[] args)
{
 String answer="y";
 ShoppingCart cart = new ShoppingCart();
 while(answer=="y")
 {
 System.out.println("Enter the Name, Price, and Quantity of Item:");
 Scanner scan = new Scanner (System.in);
 String name = scan.next();
 double price = scan.nextDouble();
 int quantity = scan.nextInt();

 cart.addToCart(name, price, quantity);

 String printout = cart.toString();
 System.out.println(printout);
 System.out.println("Do you want to continue? y/n");
 Scanner ans = new Scanner (System.in);
 answer = ans.next();
 }
 System.out.println("thank you !");
}
}

My output was:

Enter the Name, Price, and Quantity of Item:
yuvin
3
2

Shopping Cart

Item        Unit Price  Quantity    Total

Total Price: MYR0.00

Do you want to continue? y/n
n
thank you !

While I was expecting some return of the array which I just inserted

  • 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-05-26T22:39:50+00:00Added an answer on May 26, 2026 at 10:39 pm

    the line

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

    is executed prior to the constructor, therefore capacity will be zero at that time and you will get an array without space for elements …

    //edit:

    additionally there is a flaw in that addToCart method …

    if (itemCount==cart.length) is true, the item you are about to add is discarded, because you only increase the array size … you should increase the array size and then add the item …

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

Sidebar

Related Questions

I have tried setting the debug flags using the set command in cmake but
I'm not exactly sure how to properly debug this but have tried a few
I have tried to debug and find where the mismatch is coming from but
I tried everything to debug this one but I can't get to the bottom
I tried to use this code but it doesn't work. http://developer.apple.com/library/ios/#samplecode/PageControl/Introduction/Intro.html Ld /Users/waitonza/Library/Developer/Xcode/DerivedData/Dr_Ngoo-aanknxmuodcgjicaigxevljxokeq/Build/Products/Debug-iphonesimulator/Dr Ngoo.app/Dr
I have various Debug.WriteLine messages, I tried to see those messages using export MONO_DEBUG_LEVEL=debug,
I have some projects from XNA 1.0 that I wanted to debug but when
I have tried to implement this scenario . I have created Code First model,
I have a program that monitors debug messages and I have tried using a
Tried a bunch of things but I can't get it to work consistently amid

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.