Storing an array into an array
I have an array of customers, and I want to keep track of the DVD titles each customer borrowed, so I am guessing that I will create an array for each customer. Can someone help me set it up or explain how I can execute this. This is what I have so far:
Code :
public class Customer
{
public Customer(String name, String number){ }
}
public class CustomerDatabase
{
Customer[] theCustomer = new Customer[100];
public void addCustomer{ }
...
......
........
}
Suggestions/help would be appreciated. Thanks.
Re: Storing an array into an array
For database of this type, I'd strongly suggest implementing a Hash Table. The reason is I'm guessing you want to look up a certain person, and to do that an array would have to look through every element in the array, where as a hash table more-or-less will get you O(1) performance (depending on how many collisions there are, and what your hash function is).
Re: Storing an array into an array
Thanks for the suggestion. I actually haven't learned hash codes, yet, so I'm still trying to figure this out. I understand that my program won't be very efficient, but right now, I just want to focus on completing the assignment.
Re: Storing an array into an array
In your CustomerDatabase you have an array of Customer objects.
Each Customer object can have an array of Dvds it have borrowed.
So all you need to do is to add an array of dvds to your Customer class (you already have name and number there)
So it would look something like this. However if you can I would suggest using a list instead of array.
Code :
public class Customer {
private String name;
private long id;
private Dvd[] dvdList;
public Customer(String name, long id){
this.name = name;
this.id = id;
}
public void loan(Dvd dvd){
//add dvd to dvdList
}
public void deliver(Dvd dvd){
//remove dvd from dvdList
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
Oh and for that you will need a Dvd class, unless you want to store pure strings..
Dvd class would look something like this:
Code :
public class Dvd {
private String name;
private long id;
public Dvd(String name, long id){
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
Re: Storing an array into an array
Will try that! I have something very similar set up, but I will make some changes. Thanks.