Hello, I have a project to complete where I need to create a system that stores Clients, Stock and Invoice data through a GUI.

I need to be able to store multiple unique records of 'Client' in an array that allows me to edit, delete & navigate through them one by one using GUI buttons Next/Previous to display each record in JTextFields.

The biggest issue for me, is I do not understand how I create a unique record each time, then save it, once saved how do I retrieve this information?

Here is what I have so far it's not a lot but as this is a new concept for me I just want it to work for clientID & Forename first as there is a vast amount of client information that needs to be stored, I am very stuck and do not know where to go from here, a point in the right direction or sample code would be fantastic.

class clientData{
        //VARIABLES
        int clientID;
        String clientForename;
        
        
        
        public clientData(int cID, String cFName){//DISPLAY METHOD
            
        }
        
 //<------------------------------------------------------------------------------------------------       
        public void setClientID(int cID){
           clientID = cID;
        }     
        public int getClientID(){
            return clientID;
        }
//<------------------------------------------------------------------------------------------------  
        public void setClientForename(String cFName){
            clientForename = cFName;
        }
        public String getClientForename(){
            return clientForename;
        }
 //<------------------------------------------------------------------------------------------------              
        public static void main(String[] args){
            clientData[] firstClient = new clientData[0];
           
        }
    }

Thank you for your time.

clientData[] firstClient = new clientData[0];
.... defines an array of size 0, not big enough to be useful!
You may prefer something like
clientData[] manyClients = new clientData[100]; // array to hold up to 100 clients
Then you can simply do stuff like
manyClients[0] = new Client(...) // create new client and store in array
System.out.println(manyClients[0].getClientForename()); //retrieve Client from array and use it

or you could go thinking along the lines of a List. that way, you wouldn't have to know the number of elements up front.

public static void main(String[] args){
            //creates an array to hold 1,000 clients.
            clientData[] manyClients = new clientData[1000];
           
        }
    }

I've done this, could you briefly explain how I go about assigning a unique index to each client created?

If I use:

manyClients[0] = new Client(...) // create new client and store in array

Would that not just continually create an object in the array index 0 over and over again? This is the part I am struggling with the most.

Thank you for your help so far.

When you're filling up an array like this you need to maintain a count of how many Clients you have stored (initial value 0). The value of that count will also be the index of the next available array element. You just need to increment it each time you add a client.
(Like stultuske said, there are Java classes to do all this stuff for you, eg ArrayList, but your teacher probably wants you to learn about ordinary arrays first?)

They've specified as long as the end result is the same you have complete freedom of how you choose to get there, but this is my 4th month in an introductory course to Java, but if it is easier to achieve using ArrayList's I will read up on it.

So the pseduo code:

int numberOfClientIndexes = 0;
'new client'
loop to find number of indexes in array
numberOfClientIndexes++
save new record into array, index = numberofClientIndexes

Is that roughly what needs to happen? If so, that makes a lot more sense!

its a bit simpler that that - you don't need to "loop to find number of indexes in array"
when count = 0, you store the first Client into array[0], increment count by 1.
Count is now 1, you store the next Client into array[1], increment count by 1.
Count is now 2, you store the next Client into array[2], increment count by 1.
see the pattern? :-)

So count tells you how many Clients you have, but because arrays are zero-based, that's also the right index to store the next Client in.

If you use an ArrayList you just call its add method and it does all that stuff for you (plus it makes the array bigger if you run out of available elements)

import java.util.*;
 
    class clientData{

   
        //VARIABLES
        int clientID = 1;
        String clientForename = "Dave";
       
//<------------------------------------------------------------------------------------------------
        public clientData(int cID, String cFName) {
            
           
    
        }
 //<------------------------------------------------------------------------------------------------       
        public void clientSave(int cID, String cFName){
          
            al.add(getClientID());
            al.add(getClientForename());
        }
 //<------------------------------------------------------------------------------------------------       
        public void setClientID(int cID){
           clientID = cID;
        }     
        public int getClientID(){
            return clientID;
        }
//<------------------------------------------------------------------------------------------------  
        public void setClientForename(String cFName){
            clientForename = cFName;
        }
        public String getClientForename(){
            return clientForename;
        }
 //<------------------------------------------------------------------------------------------------              
        public static void main(String[] args){
           
            //creates an array to hold 1,000 clients.
            ArrayList al = new ArrayList();
            
        }
    }

Why does 'al' in the clientSave() method produce the error:

cannot find symbol

It's declared in the main method.

Bar this error, am I on the right path?

If something is declared in a method, its scope is that method - ie it cannot be seen from anywhere else. If you want to use variables in multiple methods you need to declare them outside any method (like clientID)

You have the manyClients array and is associated indexCount variable. These two always need to be used together so they should be declared together (in the same place). More seriously, if you make them instance variables (like clientID) then you will have a different one of them for every Client, which is not what you want. You want just one of each of those variables that is shared between all the instances of the class - remember static?

Finally your clientSave method is a bit of a mess. It gets some data related to a client, and then tries to retrieve the same data from the array (?).
What you need is that when you create a new Client you simply add that Client object to the array (then increment the count afterwards, not before).

I'm going out now, so I probably won't post again until tomorrow. Good luck!

Since no. of records are unknown, instead of array of objects you can go for java.util.ArrayList.

ArrayList<clientData> clientList = new ArrayList<clientData>();

To add data in ArrayList, you can use add() method.

void addClientData(int id, String name){
     clientList.add(new clientData(id,name));
}

To get client data,you can use get() method.

clientData getClientData(int id){
       int clientIndex = 0;
       for(int i=0; i<clientList.size(); i++){
           clientData cd = (clientData)clientList.get(i);
           if(cd.getClientID() == id){
              clientIndex = i;
              break;
       }
       return (clientData)clientList.get(clientIndex);
}

The use of ArrayList has been suggested twice already in this thread. And it you want to retrieve by client id then you wouldn't use a clunky method like that (complete with redundant cast and no error handling), you would use a Map rather than a List.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.