This is the last thing I am doing to my app, and I saved the hardest for last. I have been out here all week beating you guys to death with questions, so if I do not get any replies at all I will understand. I do not need this for a grade, I just want to do it, so if I get no help, so be it. I learned alot and appreciate everything.

Now, the task at hand, what I have tried, and where I am stuck. I actually started this two days ago when I almost stopped working on the search part.

I currently have an itemnoField where the user supplies the product number of the cd. I have used it almost as a counter for my tests with NEXT, FIRST, LAST ect by putting in a 1, 2, 3, 4 etc as I created cds.
Well I want to do that now in the app and take it out of the users hand.
Every cd addition I want to make the itemno one larger that the previous last itemno.
Sounded easy until I started. I wanted to do this one correctly, not just shove it in my primary class as I have all the other methods. Here is what I came up with.
I created a new class to house my generator:

public class SimplePrimaryKeyGenerator implements PrimaryKeyGenerator
{
private int key;

    public synchronized int give()
    {
    return key++;
    }

    public void take()
    {
    }
} // end class

in my CdwArtist class, I put the objects (since that is where I think they should be)

 // generates automotic number for itemno
 public synchronized int give()
 {
 return key++;
 }
 // takes back number from itemno 
 public void take()
 {
 }

and in my primary class I did the initializing and tried to bring it to life ...

public interface PrimaryKeyGenerator
    {
    public int give();
    public void take();
    }

and

    // ADD cd   
    private void btnAddActionPerformed(ActionEvent evt)
    {
        // Create cd to add
        CdwArtist newCD = new CdwArtist();
        newCD.setArtist(artistField.getText());
        newCD.setName(cdNameField.getText());
        //newCD.setItemno(get.give());  
        newCD.setItemno(Integer.parseInt(itemField.getText()));
        newCD.setNstock(Integer.parseInt(nstockField.getText()));
        newCD.setPrice(Float.parseFloat(priceField.getText()));

        // Add cd to list
        listModel.addElement(newCD);
        currCD = listModel.size()-1;  // sets currCD to added index


        // Clear the text fields after add
        artistField.setText(null);
        cdNameField.setText(null);  
        itemField.setText(null);
        nstockField.setText(null);
     priceField.setText(null);

    }// end ADD

it is currently commented out because when it is not, I get symbol errors on it. Should it not pick it up from my Cdwartist field?
I have so many thing going through my head right now that I just wanted to see what I should try with this. I have never done it this way before and do not want to go too far down the wrong road before finding out I could have done this a quicker and simpler way.
Any suggestions welcome.

I don't see anywhere that you have initialized an instance of your key generator, unless you have that elsewhere in the code. You'll have to create one before you can call it.

Using a separate class with an extra interface is a bit of overkill for what you are needing here, but it will work if you want to stick with that. I would change the variable names to something that makes sense to you, such as getKey() instead of give().

Keep in mind, you already set currCD to the last index of the list model after you add it - and that index position is unique.

My bad. I left that out, but I do have key initialized in cdwartist. key = 1; because I didnt want the first one to be 0.
I did change the names also, does make more sense.

So are you hinting that I could possibly use currCD to set this value, with little change since it is already coded and working?

I thought about trying something like that, when I started looking at putting key++ in my add button code, but was worried about using the same counter for two different things. Thought that might create some kind of problems somewhere.

So are you hinting that I could possibly use currCD to set this value, with little change since it is already coded and working?

I thought about trying something like that, when I started looking at putting key++ in my add button code, but was worried about using the same counter for two different things. Thought that might create some kind of problems somewhere.

You are only using it to set an item number property on the object. That won't affect your usage of currCD as the pointer to the current list position at all.
listModel.add() adds the item to the end of the list, thus the index of the last items increases by one automatically. You are just capturing that value and saving it to the item id property. You could set it to (currCD+1) if you don't want zero based item ids.

Since you are not using that value to access anything anyway, it doesn't matter what the number is as long as it's unique in the list. A simple int itemCounter and a setItemId(++itemCounter) would work just as well.

commented: Nice explanation +5

wow. that took about 5 minutes.
I was having such a hard time the other day that I figured there was NO way it could be that easy, so I just dropped it.
That was the same day I came up with
searchField.getText()==cdNameField.getText();
:o)
Thanks again Ez.

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.