Results 1 to 18 of 18
  1. #1
    Klahking is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default Creating an ArrayList from an ArrayList

    I am trying to create a "customerArray" an ArrayList from another ArrayList.

    I have created the first ArrayList "productList and populated it and now need to take the data from that arraylist "productList" and create "customerArray". I then need to walk through it, I am little confuced on that. Does the "productList" ArrayList have productList[0], productList[1] ... for all the elements I have put into it? I used the createHistory() to travese the productList.

    How would I use the calculateCharge method in Customer.java to traverse the "productList" and pull out only the elements I need to add the charges for the "customerArray" in Sales.java?


    // My instructions are as follows:

    Create the system of classes described in the UML.// Done

    calculateCharge // Not finiahed
    traverses the productList to calculate and return the charges

    addToProductList // Done
    adds either a Music or App to the product list // Done

    createHistory // Done
    calls calculateCharge and returns the output String for one customer

    OUTPUT // not finished
    You will print the output on a JOptionPane dialog box as follows. //


    Create a test class, Sales, that creates two Customers with the following data and puts them in a one-dimensional array, customerArray.

    (I did this already with createHistory(), prints fine)

    Davis
    Cooper Street, Arlington, Texas 76019
    account #65783
    Meet Bach, $.99, 1/5/2010
    CLASSICAL, Bach, 5
    Rihanna, $1.29, 6/5/2010
    ROCK, Rihanna, 8
    Tennis,$ 9.99, 3/18/2010
    GAME, Ubisoft

    Jones
    Fifth Street, Dallas, Texas 76000
    account #11123
    For the Road, $.99, 2/8/2010
    COUNTRY, Swift, 12
    Learn Java, $5.99, 9/1/2010
    EDUCATION, Adobe
    Pages, $9.99, 5/30/2010
    PRODUCTIVITY, Apple

    You will create the customers and add their purchases to their ArrayList, productList.
    Using the enhanced for loop, walk through the productList for each customer and create the purchase history reports.



    Customer.java

    import java.util.ArrayList;

    public class Customer implements PurchaseHistory
    {

    private String name;
    private Address address;
    private int accountNumber;
    ArrayList<Product> productList = new ArrayList<Product>(); // create an ArrayList

    public Customer(String name, Address address, int accountNumber)
    {
    setName(name);
    setAddress(address);
    setAccountNumber(accountNumber);
    }

    public String getName()
    {
    return name;
    }

    public void setName(String name)
    {
    this.name = name;
    }

    public Address getAddress()
    {
    return address;
    }

    public void setAddress(Address address)
    {
    this.address = address;
    }

    public int getAccountNumber()
    {
    return accountNumber;
    }

    public void setAccountNumber(int accountNumber)
    {
    this.accountNumber = accountNumber;
    }

    @Override
    public String toString()
    {
    return (name + "\n" + address + "\n" + "account #" + accountNumber + "\n");
    }

    public String createHistory()
    {
    String output = "";

    for(int i = 0; i < productList.size(); i++)output += productList.get(i);

    return output;
    }

    public void calculateCharge(Double charge)// unsure walks through product list of customers and adds it.
    {
    this.calculateCharge(charge);
    }

    public void addToProductList(Product product)// unsure
    {
    productList.add(product);
    }

    /*public ArrayList<Product> toArray()
    {
    return null;
    }*/

    }

    Sales.java

    public class Sales
    {
    public static void main(String[] args)
    {
    // I do not think I can have both of these, I already created Customer c1 and c2.

    //Customer customerArray[] = new Customer[2];

    //ArrayList<Customer> customerArray = new ArrayList<Customer>();


    Customer c1 = new Customer("Davis", new Address("Cooper Street", "Arlington", "Texas", 76019), 65783);

    Customer c2 = new Customer("Jones", new Address("Fifth Street", "Dallas", "Texas", 76000), 11123);


    c1.addToProductList(new Music("Meet Bach", .99, new Date(1, 5, 2010), Music.GenreType.CLASSICAL, "Bach", 5));
    c1.addToProductList(new Music("Rihanna", 1.29, new Date(6, 5, 2010), Music.GenreType.ROCK, "Rihanna", 8));
    c1.addToProductList(new App("Tennis", 9.99, new Date(3, 18, 2010), App.Type.GAME, "Ubisoft"));

    c2.addToProductList(new Music("For the Road", .99, new Date(2, 8, 2010), Music.GenreType.COUNTRY, "Swift", 12));
    c2.addToProductList(new App("Learn Java", 5.99, new Date(9, 1, 2010), App.Type.EDUCATION, "Adobe"));
    c2.addToProductList(new App("Pages", 9.99, new Date(5, 30, 2010), App.Type.PRODUCTIVITY, "Apple"));

    // This prints correctly
    System.out.println(c1 + c1.createHistory());
    System.out.println(c2 + c2.createHistory());
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default

    Does the "productList" ArrayList have productList[0],
    No, ArrayList is a class with methods not an array with elements accessed by [0]
    Read the API doc for the ArrayList class to see what method to use to get elements from it.

    Please use code tags when posting your code to preserve its formatting.

  3. #3
    Klahking is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    What is code tags?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

  5. #5
    Klahking is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    Ok, I read the BB code list.

    The next question I have, which is related to the previous post before the BB code question.


    I have changed Customer.java to this:

    Java Code:
     
    
    public String createHistory()
    	{
    		String output = "";
    		
    		for(int i = 0; i < productList.size(); i++)output += productList.get(i);
    		
    		return output;
    	}
    	
    	public void calculateCharge(Double charge)// unsure walks through product list of customers and adds it.
    	{
    		this.calculateCharge(charge);
    	}
    	
    	public void addToProductList(Product product)
    	{
    		productList.add(product);
    	}
    I have changed the test case Sales.java to this:

    Java Code:
    String statement = "";
    		
    		ArrayList<Customer> customerArray = new ArrayList<Customer>();
    		
    		Customer c1 = new Customer("Davis ", new Address("Cooper Street", "Arlington", "Texas", 76019), 65783);
    		
    		Customer c2 = new Customer("Jones ", new Address("Fifth Street", "Dallas", "Texas", 76000), 11123);
    			
    		c1.addToProductList(new Music("Meet Bach", .99, new Date(1, 5, 2010), Music.GenreType.CLASSICAL, "Bach", 5));
    		c1.addToProductList(new Music("Rihanna", 1.29, new Date(6, 5, 2010), Music.GenreType.ROCK, "Rihanna", 8));
    		c1.addToProductList(new App("Tennis", 9.99, new Date(3, 18, 2010), App.Type.GAME, "Ubisoft"));
    		
    		c2.addToProductList(new Music("For the Road", .99, new Date(2, 8, 2010), Music.GenreType.COUNTRY, "Swift", 12));
    		c2.addToProductList(new App("Learn Java", 5.99, new Date(9, 1, 2010), App.Type.EDUCATION, "Adobe"));
    		c2.addToProductList(new App("Pages", 9.99, new Date(5, 30, 2010), App.Type.PRODUCTIVITY, "Apple"));
    		
    		customerArray.add(c1);
            customerArray.add(c2); 
            
    		for(Customer c:customerArray)
            {
                //c.calculateCharge(null);
    
                statement += c.createHistory();
            }
    						
    		System.out.println(c1.getName() + c1.getAddress() + c1.productList);
    		System.out.println(c2.getName() + c2.getAddress() + c2.productList);
    		
    		System.out.println(c1 + c1.createHistory());
    		System.out.println(c2 + c2.createHistory());
    		
    		System.out.println(statement);
    		
    		JOptionPane.showMessageDialog(null, null, "Title",JOptionPane.PLAIN_MESSAGE);// DEFAULT_OPTION
    	}
    
    }
    How do I traverse ArrayList<Product> productList with the calculateCharge() method, I am stuck. I traversed the productList with the String createHistory() which is called from an interface purchaseHistory. The method calculateCharge is a double, do I have to parse that to return calculated data into a String? I have included a few System.out.println to show my iterations.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default

    How do I traverse ArrayList<Product> productList with the calculateCharge() method
    Use a for loop. Get each element from the list and do whatever calculations you need with it.
    Something like this:
    for(Product aProd : productList) {
    .. use the element: aProd to do calcs here
    }

    The method calculateCharge is a double
    That means that you must return a double value.
    Last edited by Norm; 09-08-2010 at 03:47 AM.

  7. #7
    Klahking is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    I am confused on the elements. Could you give me an example with access one of the elements.

    This is the code for the print out below it.

    [code]
    public String createHistory()
    {
    String output = "";

    for(int i = 0; i < productList.size(); i++)output += productList.get(i);

    return output;
    }

    [\code]


    Print out:

    Davis
    Cooper Street, Arlington, Texas 76019
    account #65783
    Meet Bach, $0.99, 1/5/2010
    CLASSICAL, Bach, 5
    Rihanna, $1.29, 6/5/2010
    ROCK, Rihanna, 8
    Tennis, $9.99, 3/18/2010
    GAME, Ubisoft

    How would I traverse that and pull out just the price, $9.99 or the number of songs purchased like the 8 in "Rihanna". I have two customers with different names.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default

    Could you give me an example with access one of the elements.
    Which element?
    See the API doc for ArrayList class. It has methods to get elements.
    To get the elements one at a time in order, see my last post.

    How would I traverse that and pull out just the price,
    Get the elements/objects one at a time and compare the contents of the object against the desired price.

  9. #9
    Klahking is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    Sorry for the typos. I looked at the API. I am still confused. when I wrote the following code:

    [code]
    public String createHistory()
    {
    String output = "";

    for(int i = 0; i < productList.size(); i++)output += productList.get(i);

    return output;
    }
    [\code]

    I create this:

    Meet Bach, $0.99, 1/5/2010
    CLASSICAL, Bach, 5
    Rihanna, $1.29, 6/5/2010
    ROCK, Rihanna, 8
    Tennis, $9.99, 3/18/2010
    GAME, Ubisoft
    For the Road, $0.99, 2/8/2010
    COUNTRY, Swift, 12
    Learn Java, $5.99, 9/1/2010
    EDUCATION, Adobe
    Pages, $9.99, 5/30/2010
    PRODUCTIVITY, Apple

    Now how do I traverse that to pull how the price or the number of purchases? Will I put the for loop in my method inside Customer.java? I am supposed to traverse the productList to calculate charge. I am then supposed to use the createHistory method to call calculateCharge and returns the output String for one customer.

    I am all upside down on this. A real example would help. Lets say that I have a productList that has:

    ACDC, $0.99, 1/5/2010, ROCK, Highway to Hell, 5.

    Does that have six elements? How do I get to number two, the price for instance within a method? I know I need a for loop. How would I modify the line below to pull element three out.

    [code]
    for(int i = 0; i < productList.size(); i++)output += productList.get(i);
    [\code]

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default

    Why are you concatenating the contents of the ArrayList in the variable: output?
    This doesn't make any sense.

    Lets start with a simple program and add to it.
    First create the ArrayList and fill it with the customer records.
    Next write a loop that accesses each customer's record and prints it out.

    When that is working then, move on to the next step.

  11. #11
    Klahking is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    How do I set the decimal place two places in the following method?

    Java Code:
    public Double calculateCharge() 
    	{
    		Double output = 0.00;
    
    		for (int i = 0; i < productList.size(); i++) 
    		{
    			if ((productList.get(i).title == "Meet Bach")
    					|| (productList.get(i).title == "Rihanna")
    					|| (productList.get(i).title == "For the Road"))
    
    				output = output
    						+ ((productList.get(i).price) * ((Music) productList.get(i)).numberOfSongsPurchased);
    
    			else
    				output = output + ((productList.get(i).price));
    
    		}
    		
    		return output;
    	}

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default

    What number do you want to print out with the 2 decimal places?

    Or do you want to preform some math on a double to remove all significance past the hundredths position. This can be done by multiplying by 100, convert to int to drop the fractional part then divide by 100 to restore the number.

  13. #13
    Klahking is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    I would like to take the return value "output" and set it to two decimal places. I and printing in a JOptionpane using the createHistory method, so I think I need to set the decimal place in the method calculateCharge.

    Is it a parsing issue, where the double needs to be a string?

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default

    set it to two decimal places
    Is the for when you print it?
    There are several classes/methods for formatting a double so it only shows two decimal places. The printf method and the extensions of the NumberFormat class.

    the double needs to be a string
    You view a double after it is converted to a String. Viewing it as binary is pretty useless.

  15. #15
    Klahking is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    Is the for when you print it?
    No, I am not sure I have never had to do it like this.

    First, I created a double here in the calculateCharge() in Customer.java:

    Java Code:
    public Double calculateCharge() 
    	{
    		[B]Double output = 0.00;[/B]
    
    		for (int i = 0; i < productList.size(); i++) 
    		{
    			if ((productList.get(i).title == "Meet Bach")
    					|| (productList.get(i).title == "Rihanna")
    					|| (productList.get(i).title == "For the Road"))
    
    				output = output + ((productList.get(i).price) * ((Music)productList.get(i)).numberOfSongsPurchased);
    
    			else
    				output = output + ((productList.get(i).price));
    
    		}
    		
    		[B]return output;[/B]
    	}
    Then I called the method with the double here in Customer.java:

    Java Code:
    public String createHistory() 
    	{
    		[B]Double charge = calculateCharge();[/B]
    		
    		int numberOfMusicProducts = 0;
    		int numberOfApps = 0;
    		
    		for(int i = 0; i < productList.size(); i++)
    		{
    			if ((productList.get(i).title == "Meet Bach")
    					|| (productList.get(i).title == "Rihanna")
    					|| (productList.get(i).title == "For the Road"))
    				
    		numberOfMusicProducts++;
    			
    			else
    				
    				numberOfApps++;
    			
    		}
    
    		return (name + "  " + numberOfMusicProducts + " " + "music products " + numberOfApps + " " + "apps" + " $" + charge + "\n");
    	}
    Finally, I used the createHistory() here in Sales.java, my test case:

    Java Code:
    for(Customer c:customerArray)
            {
    			statement += c.createHistory();
            }
    						
    		JOptionPane.showMessageDialog(null, statement, "Purchase History", JOptionPane.PLAIN_MESSAGE);// DEFAULT_OPTION

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    Default

    ...+ " " + "apps" + " $" + charge + "\n")
    Instead of using the default Double toString method, use one of the formatting methods I mentioned to format the Double for displaying.

  17. #17
    Klahking is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    Thanks for making me work for it Norm.

    Java Code:
    public String createHistory() 
    	{
    		Double charge = calculateCharge();
    		DecimalFormat df = new DecimalFormat("#.##");
    Java Code:
    + "apps" + " $" + df.format(charge) + "\n");
    Bingo, it worked! Wow, Java is really hard.

    Thank you for all your help. I am sure we will visit again.

    Kerry

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

Similar Threads

  1. Creating ArrayList
    By dewitrydan in forum New To Java
    Replies: 9
    Last Post: 08-08-2010, 04:16 PM
  2. Regarding arrayList
    By kishan in forum Advanced Java
    Replies: 7
    Last Post: 08-07-2009, 12:48 PM
  3. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  4. ArrayList
    By kizilbas1 in forum New To Java
    Replies: 11
    Last Post: 12-05-2007, 07:30 PM
  5. Replies: 0
    Last Post: 12-05-2007, 02:09 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
  •