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.