| | |
Convert User Supplied Int to App Generated
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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:
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 ...
and
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.
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)
public class SimplePrimaryKeyGenerator implements PrimaryKeyGenerator { private int key; public synchronized int give() { return key++; } public void take() { } } // end class
[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)
public interface PrimaryKeyGenerator { public int give(); public void take(); }
Java Syntax (Toggle Plain Text)
// 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
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 never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
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().
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().
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.
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.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
•
•
•
•
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.
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.
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.
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.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
![]() |
Similar Threads
- Simple ASP.Net Login Page using C# (C#)
- Encrypt File using User Supplied Password (VB.NET)
- Generate User ID (PHP)
- Artillery Duel Question (Computer Science)
Other Threads in the Java Forum
- Previous Thread: Can JAVA create DOS directories?
- Next Thread: java or mysql
| Thread Tools | Search this Thread |
Tag cloud for Java
addressbook android api apple applet application arguments array arrays automation binary bluetooth button calculator chat class classes client code columns component converter database draw eclipse error errors event exception file fractal ftp game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inetaddress input integer j2me japplet java javaprojects jme jmf jni jpanel julia link linux list loop map method methods midlethttpconnection mobile netbeans newbie number objects openjavafx oracle php print problem program programming project projects recursion rim scanner screen server set signing size smart sms socket sort sql storm string support swing test threads time tree unlimited variablebinding webservices windows






