Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 37

Thread: Default Write a Java program to simulate an online shopping cart.

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Default Write a Java program to simulate an online shopping cart.

    Hello, I need some help finishing this shopping cart java program. Here are the directions:

    Project description: Write a Java program to simulate an online shopping cart.

    An online shopping cart is a collection of items that a shopper uses to collect things for purchase. A shopper can add items to the cart, remove them, empty the cart, view the items in the cart, and end shopping and proceed to checkout.
    Using the Java ArrayList class, you will write a program to support these functions. Each item added to the cart will be represented with the CartItem class (se attached .java files).

    When your program begins, you will display a menu of actions the shopper can perform:

    SHOPPING CART OPTIONS
    1 add an item to your cart
    2 remove an item from your cart
    3 view the items in your cart
    4 end shopping and go to checkout
    5 empty your cart
    6 exit the program

    Your program will allow the shopper to add and remove items to the shopping cart. The program should continue as long as the shopper want to keep going. The shopper can exit by choosing option 6, and in this case the shopper will exit without making a purchase. The shopper can also exit by selecting option 4, and go to checkout. In this option, the program will display the amount due by adding up the cost of all the items in the cart. Use the NumberFormat class to format the amount in currency.
    __________________________________________________ __________________________________________________


     
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.util.Scanner;
     
    public class Shopping {
     
    	/**
    	 * In this program you will replicate an online shopping
    	 * cart. You will use the ArrayList class to hold the
    	 * items in your shopping cart.
    	 * You will use the CartItem class to represent items in
    	 * your shopping cart.
    	 * In this driver program you will do the following:
    	 * Create the shopping cart object
    	 * Offer a menu of options:
    	 * 1 add an item to your cart
    	 * 2 remove an item from your cart
    	 * 3 view the items in your cart
    	 * 4 end shopping and go to checkout
    	 * 5 empty your cart
    	 * 6 exit the program
    	 * Use the Scanner class to collect input
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		ArrayList<CartItem> shoppingCart = new ArrayList<CartItem>();
    		Scanner scan = new Scanner(System.in);
    		ArrayList<Integer> intList = new ArrayList<Integer>();
    		boolean keepGoing = true;
    		int choice = 0;
    		int input = 0;
    		int index=0;
    		int total = 0;
    		Integer item;
     
    		while(keepGoing)
    		{
    			System.out.println("\nMenu - Managing a List");
    			System.out.println("1 Add an item to your cart");
    			System.out.println("2 Remove an item from your cart");
    			System.out.println("3 View the items in your cart");
    			System.out.println("4 Exit and add up the total");
    			System.out.println("5 Empty your cart");
    			System.out.println("6 Exit");
    			System.out.println("Select a menu option");
    			choice = scan.nextInt();
    			if (choice <1 || choice >6)
    			{
    				System.out.println("Enter a value between 1 and 6:");
    			}
    			else
    			{
    				switch (choice)
    				{
    				case 1:
    					//add an integer
    					System.out.println("Enter an item:");
    					input = scan.nextInt();
    					item = new Integer(input);
    					intList.add(item);
    					//intList.add(input);
    					break;
    				case 2:
    					//remove from the list
    					System.out.println("Enter an item to remove:");
    					input = scan.nextInt();
    					item = new Integer(input);
    					if (intList.contains(item))
    					{
    						intList.remove(item);
    						System.out.println(item + " has been removed.");
    					}
    					else
    					{
    						System.out.println(item + " was not found in your shopping cart.");
    					}
    					break;
    				case 3:
    					//view the items in your cart
    					System.out.println(intList);
    					break;
    				case 4:
    					//Exit and add up the total
    					for (int i = 0; i<intList.size(); i++)
    					{
    						item = intList.get(i);
    						total = total + item.intValue();
    					}
    					System.out.println("Total is "+ total);
    					System.out.println("Goodbye");
    					keepGoing = false;
    					break;
    				case 5:
    					//Empty the list
    					intList.clear();
    					break;
    				case 6:
    					//exit
    					keepGoing = false;
    					System.out.println("Goodbye");
    					break;
     
    				}
    			}
    		}
    	}
    }
     
     __________________________________________________ _____________________________________
     
     
    public class CartItem {
    	private String product;
    	private int quantity;
    	private double price;
     
    	//constructor
    	public CartItem()
    	{
    		product = "";
    		quantity = 0;
    		price = 0.0;
    	}
    	public String getProduct()
    	{
    	return product;
    	}
    	public double getPrice()
    	{
    	return price;
    	}
    	public int getQuantity()
    	{
    	return quantity;
    	}
     
    	//constructor with parameters
    	public CartItem(String inProduct, int inQuant, double inPrice)
    	{
    		product = new String(inProduct);
    		quantity = inQuant;
    		price = inPrice;
    	}
    	//getter setter public methods for each instance data
    	public boolean equals(CartItem item)
    	{
    		//write the code for the equals method
    		//return true;
    		boolean result = false;
    		if (this.product.equalsIgnoreCase(item.getProduct()) && this.price == item.getPrice())
    			result = true;
    		else
    			result = false;
     
    		return result;
    	}
     
    	public String toString()
    	{
    		//write code for toString method
    		String result="";
     
    		return result;
    	}
    }

    This is what I have so far. I need help finishing the program so it runs properly. Thanks for your help !


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    Can you explain what problems you are having with the program? What items are working and what items are you currently working on?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    i believe that most of the work needs to be done in the CartItem file. I need to write getter setter public methods for each instance data, write code for toString method, and also the constructor with parameters.

    When you run the program and use option 1, it immediately returns an error which I am not sure why. If you try running the program, that might help you determine the other problems.


    Thank you for your help. I appreciate it !

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    immediately returns an error
    Please copy the full text of the error message and paste it here. It has important info about the error.

    The getter/settor methods are very simple and shouldn't be a problem.

    The toString() method returns a String that describes what is in the class. It can have the values of some or all of the class's variables.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    Here is what happens when I run the program and choose option 1:

    Menu - Managing a List
    1 Add an item to your cart
    2 Remove an item from your cart
    3 View the items in your cart
    4 Exit and add up the total
    5 Empty your cart
    6 Exit
    Select a menu option
    1
    Enter an item:
    jeans
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Shopping.main(Shopping.java:58)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Shopping.main(Shopping.java:58)
    At line 58 the code is trying to read an integer using the nextInt() method. "jeans" is not an integer.
    Either change the code to read a String or have the user enter an integer value.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    How do I change the code to have it read a string?

    Also, here is the error when I choose option 2. I assume it is the same error, but for line 66:

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Shopping.main(Shopping.java:66)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    Yes its the same problem. nextInt() wants an integer. What was entered?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    It gave me the error because I did not enter an integer, I entered the word "jeans"

    So like you said, I have to change the code to have it read a string. How do I do that?

    thanks for your help

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    Read the API doc for the Scanner class and find a method that will read the type of data that you want to enter.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    im really not sure what that means. Could you be more specific or help provide me with the code?

    thanks !

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    Here is the link to the API doc for all the Java SE classes. Find the class in the lower left, click the link and the API doc for the class will be shown in the main window.
    Java Platform SE 7
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    which class am i looking for?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    Look at post#10
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    What do i do for the getter setter methods?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    Those are very standard methods for a lot of classes. Did your instructor discuss what they were and how they worked? Have you done a search on the internet for them?
    Try this: java getters and setters
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    i know what they are but i do not know how to apply them to this particular program. Could you show me how?

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    Pick one of the fields in the class and write a getter and a setter method for it. Try to compile it. Copy and paste it here with any error messages you got.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    I am still trying to change the code to have it read a string. Could you please give me the code for that?

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    Sorry, I don't write code for students.

    What Scanner class method are you trying to use to read a String? When you look at the list of methods, what the method returns is in the left column. Look down the list to find methods that return a String.

    I'm done for tonight. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    I never said I was a student. But thank you for your help. I will let you know if I have any more questions.

  22. #22
    Junior Member
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    I am trying to figure out case 4 but I am getting this error:

    5.PNG

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    I am getting this error:
    Please copy the full text of the compiler's error messages and paste it here. I don't see any error messages in the image.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Junior Member
    Join Date
    Dec 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    Here is the first error I am getting:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The constructor CartItem(String) is undefined
    The constructor CartItem(String) is undefined

    at Shopping.main(Shopping.java:59)


    5.PNG

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Default Write a Java program to simulate an online shopping cart.

    The constructor CartItem(String) is undefined
    The compiler can not find a constructor for the CartItem class that takes a String for an argument.
    Either add a constructor to the class that takes a String as argument
    or change the new statement so that it uses one of the existing CartItem constructors.
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Write a Java program to simulate an online shopping cart.
    By astjeanos in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 11th, 2013, 04:32 PM
  2. simple shopping cart
    By dodda911 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 12th, 2013, 08:25 AM
  3. Shopping cart application
    By ashkrish in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 28th, 2012, 01:40 PM
  4. Shopping cart application
    By ashkrish in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 28th, 2012, 12:08 PM
  5. Replies: 1
    Last Post: February 8th, 2012, 05:16 PM