Results 1 to 4 of 4
  1. #1
    nickc88 is offline Member
    Join Date
    Mar 2009
    Location
    New Jersey
    Posts
    2
    Rep Power
    0

    Default Using arrayList to store data from one class to another.

    Hi i am taking my first java course and i have been stuck for a couple of days on a certain assignment. What i want to do is send string type data from one class and store it in a arrayList of another class. Also the data that i am sending over is in 3 categories. The assignment is to store an item from a store. the three categories are item name, item department, and item cost. I would be sending them from the item class to the shopping cart class.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    There is usually more than one way to do things like this.
    You could make up a data store class:
    Java Code:
    class ShoppingCart {
        List<ItemStore> items = new ArrayList<ItemStore>();
    
        public void addItem(ItemStore store) {
            items.add(store);
        }
    
    class Item {
        ShoppingCart cart;
    
        private void addSelection() {
            ItemStore item = new ItemStore("ClearCut", "DVD", 10.00);
            cart.addItem(item);
        }
    }
    
    class ItemStore {
        String name;
        String itemDepartment;
        double cost;
    }

  3. #3
    softslate is offline Member
    Join Date
    Mar 2009
    Location
    Schenectady, NY
    Posts
    11
    Rep Power
    0

    Default

    The above post is a great summary. The idea is to create classes (beans) of each of the "nouns" you're working with (ie., the item and the shopping cart) with properties describing them (name, cost, items). Then write methods that "do" the things you need to do with the classes, such as add an item to the cart.
    SoftSlate Commerce Java Shopping Cart
    www.softslate.com

  4. #4
    nickc88 is offline Member
    Join Date
    Mar 2009
    Location
    New Jersey
    Posts
    2
    Rep Power
    0

Similar Threads

  1. help me to store data in a class
    By neeraj.singh in forum New To Java
    Replies: 2
    Last Post: 08-06-2009, 09:31 AM
  2. how to store the data in data base
    By eclipse3.4ide in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 05:25 AM
  3. how to store an ArrayList<string> into cookies?
    By perplexingtrax in forum Java Servlet
    Replies: 7
    Last Post: 01-12-2009, 01:30 AM
  4. How to create folder(s) and store data?
    By Grom in forum New To Java
    Replies: 2
    Last Post: 08-20-2008, 10:01 AM
  5. how to use btree to store data
    By fred in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:52 PM

Posting Permissions

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