Results 1 to 14 of 14
  1. #1
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Question 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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Need help with shopping cart program.

    Please use the code tags when you are posting the code segments in the forum next time.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default 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.

  4. #4
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default 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?

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Need help with shopping cart program.

    Yes it is. You can initialize a variable to sum of all the cost at the top.

    Java Code:
    double dSum = 0;
    Then after collecting the unit price and the number of items do the calculation and added,

    Java Code:
    dSum += itemOne * unitOne;
    Note that you have to do the relevant conversions.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default 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?

  7. #7
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: Need help with shopping cart program.

    I don't understand.. I have to also add sales tax so where would that come in?

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default 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);
                     
        }
    }

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Need help with shopping cart program.

    Quote Originally Posted by donthatejustenvy View Post
    I don't understand.. I have to also add sales tax so where would that come in?
    When you have a problem, if you unable to identify the complete implementation at once, break down the complete question into simple steps. Then think about how to address each step only one at a time. It is the most easiest way to solve the complete. :)

  10. #10
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default 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. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default 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.

  12. #12
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: Need help with shopping cart program.

    How do I do that? I'm using netbeans

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default 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.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Shopping Cart
    By aliala in forum New To Java
    Replies: 1
    Last Post: 07-30-2011, 12:00 AM
  2. Creating a Shopping Cart using JSP
    By lison4luv in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 04-20-2011, 06:18 AM
  3. Trying to validate in a js shopping cart
    By rockc in forum New To Java
    Replies: 1
    Last Post: 02-11-2010, 12:28 AM
  4. I need help for this shopping cart application!
    By helloworld in forum New To Java
    Replies: 3
    Last Post: 09-29-2008, 06:18 PM
  5. Simple Shopping Cart Help
    By CoOlbOyCoOl in forum NetBeans
    Replies: 0
    Last Post: 05-12-2007, 05:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •