Results 1 to 14 of 14
- 11-02-2011, 03:54 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
Need help with shopping cart program.
Hello All!
I have been asked to write a program as follows:
2. Create the ShoppingCart project, use a while loop to ask user to enter the price and units of each item (for example, 44.95, 1). After a user finishes entering the information for one item, ask if he/she would like to continue. Calculate the total payment at the end, including the 8.5% tax. At the end, please print out the total payment of the shopping cart. The following example shows how the application runs.
Please enter the price of a new item (e.g. 49.50):
44.95
Please enter the units of this item (e.g. 2):
1
Continue? (yes/no):
yes
Please enter the price of a new item (e.g. 49.50):
18.99
Please enter the units of this item (e.g. 2):
2
Continue? (yes/no):
no
Total Payment with Tax: $89.98
Thank you for your order.
__________________________________________________ _____________________________
So far this is what I have for the program:
Java Code:package shoppingcart; import java.util.*; public class ShoppingCart { public static void main(String[] args) { Scanner in = new Scanner (System.in); final double SALES_TAX_RATE = 0.085; String isContinue = "yes"; double totalCost = 0; String itemOne; int unitOne = 0; String itemTwo; int unitTwo = 0; while(isContinue.equalsIgnoreCase("yes")) { System.out.println("Please enter the price of a new item (e.g. 49.50): "); itemOne = in.next(); System.out.println("Please enter the units of this item (e.g. 2): "); unitOne = in.nextInt(); System.out.println("Continue? (yes/no)"); isContinue =in.next(); System.out.println("Please enter the price of a new item (e.g. 49.50): "); itemTwo = in.next(); System.out.println("Please enter the units of this item (e.g. 2): "); unitTwo = in.nextInt(); System.out.println("Continue? (yes/no)"); isContinue =in.next(); } totalCost = ??????????????? System.out.printf("Total payment with tax: $%., 2f\n", totalCost); } }
Can someone tell me what I am doing wrong??? The parts where there is "??????????" is where I'm lost.. but I think there's more wrong with it =\ Any help would be greatly appreciated!Last edited by Eranga; 11-02-2011 at 05:12 AM. Reason: code tags added
- 11-02-2011, 05:13 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Need help with shopping cart program.
Please use the code tags when you are posting the code segments in the forum next time.
- 11-02-2011, 05:16 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Need help with shopping cart program.
Regarding your question, once user enter the details think about a way to store them temporally. Have you heard about collections like, arrays, lists, ArrayList and so on? If you are don't know about those, read about them first of all. Then you could easily find the solution for your question. If you are stuck on something, let me know.
- 11-02-2011, 05:36 AM #4
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
Re: Need help with shopping cart program.
Hello,
How do I use tags in my posts? Sorry..
Regarding the problem I posted.. The teacher has not taught us anything about arrays or anything like that so maybe there's another way to do it?
- 11-02-2011, 05:43 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Need help with shopping cart program.
Yes it is. You can initialize a variable to sum of all the cost at the top.
Then after collecting the unit price and the number of items do the calculation and added,Java Code:double dSum = 0;
Note that you have to do the relevant conversions.Java Code:dSum += itemOne * unitOne;
- 11-02-2011, 05:45 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Need help with shopping cart program.
You no need to do the same twice in the while loop too, did you see any point doing that?
- 11-02-2011, 05:55 AM #7
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
Re: Need help with shopping cart program.
I don't understand.. I have to also add sales tax so where would that come in?
- 11-02-2011, 06:05 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Need help with shopping cart program.
Forget the tax for the moment. Don't mess up all at once. Have a look at the following code segment. Go through the comment and try to understand the logic.
Java Code:import java.util.*; public class ShoppingCart { public static void main(String[] args) { Scanner in = new Scanner (System.in); final double SALES_TAX_RATE = 0.085; String isContinue = "yes"; double totalCost = 0; double itemOne; int unitOne = 0; String itemTwo; int unitTwo = 0; double dSum = 0.0; // Initialized to summation while(isContinue.equalsIgnoreCase("yes")) { // Read the unit price as a double value System.out.println("Please enter the price of a new item (e.g. 49.50): "); itemOne = in.nextDouble(); // Number of items as int System.out.println("Please enter the units of this item (e.g. 2): "); unitOne = in.nextInt(); System.out.println("Continue? (yes/no)"); isContinue =in.next(); // Summation dSum += (itemOne * unitOne); // The rest is not required since you are in a while loop /* System.out.println("Please enter the price of a new item (e.g. 49.50): "); itemTwo = in.next(); System.out.println("Please enter the units of this item (e.g. 2): "); unitTwo = in.nextInt(); System.out.println("Continue? (yes/no)"); isContinue =in.next(); */ } // Not required totalCost = 0; // Print the result. Note the changes I have done. System.out.printf("Total payment with tax: $%.2f\n", dSum); } }
- 11-02-2011, 06:08 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Need help with shopping cart program.
- 11-03-2011, 04:09 AM #10
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
Re: Need help with shopping cart program.
Hello Eranga,
Thank you for your reply.
I tried the program you wrote and it worked fine but the total ended up being 82.93 instead of 89.98. I declared variable, "SALES_TAX_RATE" in the beginning and when I did the summation I just tried to multiply it with the rest so it looks like this
dSum += (itemOne * unitOne * SALES_TAX_RATE);
but that didn't work either??? Ughhh sorry! I'm verrrry lost!
- 11-03-2011, 04:24 AM #11
Re: Need help with shopping cart program.
Debug your code. On the line above your calculation print out itemOne, unitOne and sales_tax_rate to make sure they hold the values you expect.
- 11-03-2011, 04:35 AM #12
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
Re: Need help with shopping cart program.
How do I do that? I'm using netbeans
- 11-03-2011, 04:59 AM #13
Re: Need help with shopping cart program.
What do you mean "How do I do that". You know what a print statements is, you have several in your code. Just add another one to print out the values of your variables.
- 11-03-2011, 05:27 AM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Need help with shopping cart program.
Or else you can easily use break points with the NetBeans. Haven't you tried them before?
Similar Threads
-
Shopping Cart
By aliala in forum New To JavaReplies: 1Last Post: 07-30-2011, 12:00 AM -
Creating a Shopping Cart using JSP
By lison4luv in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 04-20-2011, 06:18 AM -
Trying to validate in a js shopping cart
By rockc in forum New To JavaReplies: 1Last Post: 02-11-2010, 12:28 AM -
I need help for this shopping cart application!
By helloworld in forum New To JavaReplies: 3Last Post: 09-29-2008, 06:18 PM -
Simple Shopping Cart Help
By CoOlbOyCoOl in forum NetBeansReplies: 0Last Post: 05-12-2007, 05:21 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks