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.

Results 1 to 4 of 4

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

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

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

    Hey, I need a lot of help working on this assignment. The prompt is as follows:

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

    I've gotten this far:


    import java.util.ArrayList;
    import java.util.Scanner;

    public class ManageList {

    /** * 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
    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 - Shopping Cart Options");
    System.out.println("1 add an item to the cart");
    System.out.println("2 remove an item from the cart");
    System.out.println("3 view the items in the cart");
    System.out.println("4 Exit and check-out");
    System.out.println("5 Empty the 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 you want:");
    input = scan.nextInt();
    item = new Integer(input);
    intList.add(item);
    //intList.add(input);
    break;
    case 2:
    //remove from the list
    System.out.println("Enter and 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 = ""; //say that in a string
    quantity = 0;//say that in an int
    price = 0.0;//say that in a double
    }
    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;
    }
    }
    ___________________________________________



    can someone help me finish this off? Thank you :/


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

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

    What's your specific technical question?

    When posting code, please use the highlight tags.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

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

    Well I don't really have much of a technical question. I got this far with my program and I'm just stuck. I am in major need of someone pointing me in the right direction.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

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

    Please read the Announcement topic at the top of the sub-forum to learn how to post code in code or highlight tags and other useful info for newcomers.

    Why are you stuck? What are you stuck on? Show us the part of the assignment you don't know how to do with the code you've tried to do that part and an explanation why it's not providing the desired results. Something like, "I need to do x, I tried to do y, and I'm getting result z when I need to be getting result q."

    Apart from finishing the code you posted (the thing we can't do), what would you like us to help you with?

Similar Threads

  1. simple shopping cart
    By dodda911 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 12th, 2013, 08:25 AM
  2. Shopping cart application
    By ashkrish in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 28th, 2012, 01:40 PM
  3. Shopping cart application
    By ashkrish in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 28th, 2012, 12:08 PM
  4. Replies: 1
    Last Post: February 8th, 2012, 05:16 PM
  5. Validating fields in a js shopping cart
    By rockc in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 03:37 PM