944,129 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 948
  • Java RSS
Aug 3rd, 2007
0

Convert User Supplied Int to App Generated

Expand Post »
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:
Java Syntax (Toggle Plain Text)
  1. public class SimplePrimaryKeyGenerator implements PrimaryKeyGenerator
  2. {
  3. private int key;
  4.  
  5. public synchronized int give()
  6. {
  7. return key++;
  8. }
  9.  
  10. public void take()
  11. {
  12. }
  13. } // end class
in my CdwArtist class, I put the objects (since that is where I think they should be)
[CODE][ // generates automotic number for itemno
public synchronized int give()
{
return key++;
}
// takes back number from itemno
public void take()
{
} /CODE]
and in my primary class I did the initializing and tried to bring it to life ...
Java Syntax (Toggle Plain Text)
  1. public interface PrimaryKeyGenerator
  2. {
  3. public int give();
  4. public void take();
  5. }
and
Java Syntax (Toggle Plain Text)
  1. // ADD cd
  2. private void btnAddActionPerformed(ActionEvent evt)
  3. {
  4. // Create cd to add
  5. CdwArtist newCD = new CdwArtist();
  6. newCD.setArtist(artistField.getText());
  7. newCD.setName(cdNameField.getText());
  8. //newCD.setItemno(get.give());
  9. newCD.setItemno(Integer.parseInt(itemField.getText()));
  10. newCD.setNstock(Integer.parseInt(nstockField.getText()));
  11. newCD.setPrice(Float.parseFloat(priceField.getText()));
  12.  
  13. // Add cd to list
  14. listModel.addElement(newCD);
  15. currCD = listModel.size()-1; // sets currCD to added index
  16.  
  17.  
  18. // Clear the text fields after add
  19. artistField.setText(null);
  20. cdNameField.setText(null);
  21. itemField.setText(null);
  22. nstockField.setText(null);
  23. priceField.setText(null);
  24.  
  25. }// 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.
Similar Threads
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Aug 3rd, 2007
0

Re: Convert User Supplied Int to App Generated

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().
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 3rd, 2007
0

Re: Convert User Supplied Int to App Generated

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 3rd, 2007
0

Re: Convert User Supplied Int to App Generated

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.
Last edited by no1zson; Aug 3rd, 2007 at 6:26 pm.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Aug 3rd, 2007
1

Re: Convert User Supplied Int to App Generated

Click to Expand / Collapse  Quote originally posted by no1zson ...
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.
Last edited by Ezzaral; Aug 3rd, 2007 at 6:41 pm.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 3rd, 2007
0

Re: Convert User Supplied Int to App Generated

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.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Can JAVA create DOS directories?
Next Thread in Java Forum Timeline: java or mysql





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC