Results 1 to 6 of 6
  1. #1
    blevault is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default Capture elements from an List in a different package

    Hi,
    I am working on a program that has elements in an ArrayList in a ShoppingCart Class in a product package.

    In Shopping Cart in the getItems method, I first created a list (required for assignment) in the getItems method List<Item> itemsList = new ArrayList(items);

    I check this list for size and it matches my ArrayList items.

    I also have my return return itemsList;

    public List getItems()
    {
    List<Item> itemsList = new ArrayList(items);
    //System.out.printf(" Total Price: %s\n", cPrice);
    //clear();
    return itemsList;

    I need to to two things. First my Customer class in a different package I use a getCart method to access the getItems and then move the list as single element into a new list called bill.


    I am unsure of how to code the getCart in my Customer class to get the list and move it to my Bill Class. Here is what I am currently trying.

    public ShoppingCart getCart()
    {
    List<Bill> billList = new ArrayList();
    billList = (List<Bill>) cart.getItems(); //(cart.getItems());
    System.out.printf("\n billList Size: %d\n", billList.size());
    return cart;

    }

    Any help would be appreciated.

    Thanks
    Last edited by blevault; 11-23-2008 at 01:41 AM.

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    What package is Customer in? Just use
    Java Code:
    import (package).Customer;
    in the file in which you wish to use Customer class, then do:
    Java Code:
    Customer customer = new Customer();//
    in the Shopping Cart class, whatever methods you need in Shopping Cart, write them in Customer and they will show up on customer.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    blevault is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default Import for my class

    Hi

    I have my imports set and my package declarations are set. In my Customer Package I call. I tried this code as well as numerous other attempts, but I either cant compile or it retrurns 0 elements.

    billList = (List<Bill>) cart.getItems(); //(cart.getItems());

    Here is my customer class.

    // Customer.java
    // Customer class defines user customer with references to other objects.
    package user;
    // wk3.package user;

    import java.util.ArrayList;
    import java.util.List;
    import product.*;
    //import wk3.product.*;


    public class Customer extends DefaultUser
    {
    private Address address= new Address();
    private ArrayList<Bill> bills = new ArrayList<Bill>();
    private ShoppingCart cart= new ShoppingCart();

    public Customer()
    {
    }

    public Customer(int id)
    {
    super(id);
    }

    public ShoppingCart getCart()
    {
    List<Bill> billList = new ArrayList();
    billList = (List<Bill>) cart.getItems(); //(cart.getItems());
    System.out.printf("\n billList Size: %d\n", billList.size());
    return cart;

    }

    public void checkout()
    {
    getCart();

    }
    Is this a proper statement to have it return the elements of itemsList?

    Thanks

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    as I see it, the getCart() method should return the cart, nothing less, nothing more. The cart that is returned is then used to generate the bill, but this is not done within the getCart method.

  5. #5
    blevault is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Exclamation

    Yes, you must be correct. It only has to return the cart. However, I am unsure of how to use this. How to use all these methods seems to be the biggest issue I have right now. What calls what, and then what do I need to code.

    In my class I have the following.

    attributes:
    -address Address <create>
    -bills ArrayList<Bill> <create>
    -cart ShoppingCart <create>

    +Customer()
    +Customer(int id)
    +checkOut() void
    +getAddress() void
    +getBills() List<Bill>
    +findBillById(int id) Bill
    +findBillByDate(Date date) Bill
    +findCurrentBill() Bill
    +getCart() ShoppingCart

    attributes:
    -TAX_RATE
    -address Address
    -date Date <create>
    -items ArrayList<Item>
    -subTotal float
    -totalTax float
    -totalCost float

    +Bill (List<Item> items)
    +Bill(int id, List<Item> items)
    +getAddress Address
    +setAddress Address
    +getDate Date
    +setDate Date
    +getItems(ArrayList<Item>)
    +subTotal float
    +totalTax float
    +totalCost float

  6. #6
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Lightbulb study OO

    I coded till I wore out, study OO. Set down on the project and figure stuff like Customer has Bill and just do whatever OO tells you to do.
    Java Code:
    // A customer.
    class Customer
    {
        int id;
        boolean done;
        private List<Bill> lb;
        Customer(){lb = new List<Bill>();}
        Customer(int id){this();id = id}
        void checkOut(){done = true;}
        Address getAddress(){}
        List<Bill> getBills() {return }
        +findBillById(int id) Bill
        +findBillByDate(Date date) Bill
        +findCurrentBill() Bill
        +getCart() ShoppingCart
    }
    // Class Bill
    class Bill
    {
        //
        final BigDecimal TAX_RATE;
        static{TAX_RATE = new BigDecimal(123456.7890);}
        Date date;
        Integer id;
        Address address;
        Date <create>
        ArrayList<Item> Items;
        double subTotal;
        double totalTax;
        double totalCost;
    
        Bill (List<Item> items)
        {
            // This will need a cast or some further work.....
            Items = items;
            Bill(int id, List<Item> items)
            {
                id = new Integer(id);
                Items = items;//
            }
            Address getAddress(){return address;}
            void setAddress(String[] data){Address = new (data);} 
            Date getDate(){return date;}
            void setDate{ date = new Date(); }
            ArrayList<Item> getItems(){return Items;}
        }
    }
    
    class Address
    {
        String street_number;
        String street_name;
        String citadel_name;
        String estacion_de_residence;
        String country_of_residence;
        // preliminary constructor .....
        Address(String[] data)
        {
            street_number = data[0];//
            street_name = data[1];//
            citadel_name = data[2];//
            estacion_de_residence = data[3];//
            country_of_residence = data[4];//
        }
    }
    class Item
    {
        String name;
        String description;
        String vendor;
        Item(String name, String description, String vendor)
        {
            name = name;
            description = description;
            vendor = vendor;
        }
    }
    That's got to be the most unusual editor I have ever seen, can you post in our what editor are you using Thread and explain where + became Java code?
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. How to capture a widget image with a GC
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 09:00 PM
  2. Elements package
    By BlitzA in forum New To Java
    Replies: 0
    Last Post: 12-28-2007, 12:58 AM
  3. How to capture IP packets
    By Hasan in forum Networking
    Replies: 1
    Last Post: 05-31-2007, 06:44 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
  •