How to make a simple cart using java?

I have a question which I’m completely clueless about, could someone please help me?

“In this question, you are to write a simple shopping cart program that accepts up to
three sets of input. Each set of input is an item with unit cost (in cents) and quantity. After entering the
details for each set, the user is prompted if he wants to insert more items (up to two times) if the user is
willing to continue, he/she will input 1, otherwise 0 will be entered which indicate the end of data input
After three items have been entered or when the user has selected not to add more items, the program
will print out the total cost in dollars, correct to decimal places.”

Hi Bombolz, welcome to the forums.

Your thread title says Java and the file name has the extension .java
Yet you posted this in the JavaScript forum.

Java is NOT JavaScript. Are you certain you mean Java?

Hmm, sorry about that. I meant Java, not JavaScript, but there wasn’t a forum for that is there? Could someone please direct me to the Java forum if there is one? Or can you help me with the problem?

There isn’t one. :frowning:

Your question is kind of ambiguous and tells us nothing of your skill level or how you’re trying to build it (servlets, jsf, etc). Or really, if this even needs to be a web app. Sounds like just a console app. What exactly do you need help with?

I need to generate an output that looks like this:
Where the user inputs are in blue and the output is red

Enter cost for item #1: 40
Enter quantity: 4
Do you wish to add more items? (1/0): 1
Enter cost for item #2: 100
Enter quantity: 1
Do you wish to add more items? (1/0): 1
Enter cost for item #3: 40
Enter quantity: 2
Total: $3.40

I’m using blueJ to do this (which is a free java software).

I forgot to mention that I can’t use arrays to answer the question, which was the main reason I’m totally confused about this.

BlueJ is a teaching IDE for Java. Please read this: An IDE is just designed to help you, doesn’t have an effect on the code other than helping you write better code. Eclipse and Netbeans are also free Java IDEs and are much more widely used.

That said, I’m still confused as to what you’re having problems with. What have you tried and what are you having problems with?

Java is heavily object oriented, but I don’t know where you’re at in your class. I would represent the questions above in an ArrayList() (read this) of simple 2 field objects. But there are a number of other data structures that might work. Show me some code that you’ve written for this and I’ll try to help.

I have my java code but I got some help with it so I can’t understand some of it.

import java.util.*;

public class ShoppingCart {
public static void main(String args) {
Scanner in = new Scanner (;
String isContinue = “1”;
double totalCost = 0;
double itemOne;
int unitOne = 0;
String itemTwo;
int unitTwo = 0;
double dSum = 0.0;
System.out.println("Enter cost for item #1: ");
itemOne = in.nextDouble();
System.out.println("Enter quantity: ");
unitOne = in.nextInt();
System.out.println(“Continue? (1/0)”);
dSum += (itemOne * unitOne);
totalCost = 0;
System.out.printf("Total: $%.2f
", dSum);


What I don’t understand is the System.out.printf("Total: $%.2f
", dSum);

I understand that it’s a floating point for 2 decimals but what’s the

“newline”? eg. instead of

it would be

Oh ok thanks,
So essentially, in this case it’s not needed?

Thanks for the help everyone, I really appreciate it!

Maybe not. But as classes can be used in more than one place it’s probably a good idea to keep it there unless it’s causing problems for you.