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());
}