473,513 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Integer Field in JList ... renumbering

38 New Member
I do not even know how to correctly ask this question.
I have an item field in the code I am about to post. Simple intger meant to be an item number for a cd. The user enters this number.
Over the last week I have played with Add buttons, Del buttons, Modify and everything else and it brought to light a problem I want to address today.
I have always just used 1 for the first item, 2 for the second and so on ...
well after I am done will all the button nonsence the item numbers are all over the place.
How could I make it so that the item number field would be restructured and stay in ascending order no matter what was added or deleted?
If I delete cd number 4, then make 5 number 4, 6 number 5 and so on ... the next cd added should just take the next highest number ...
Does that make sense?
Here is my primary class. I can post the others if needed.

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import javax.swing.event.*;
  6. import java.text.*;
  7. import java.lang.*;
  8. import javax.swing.JFrame;
  9.  
  10. public class Inventory2 extends JFrame
  11. {
  12.     private JLabel cdNameLabel; // name label 
  13.     private JLabel artistLabel; // item number label 
  14.     private JLabel nstockLabel; // units in stock label 
  15.     private JLabel priceLabel; // price each label 
  16.     private JLabel itemLabel; // item number label 
  17.     private JTextField cdNameField; // name display 
  18.     private JTextField artistField; // artist display 
  19.     private JFormattedTextField nstockField; // units in stock display 
  20.     private JFormattedTextField priceField; // price each display 
  21.     private JTextField itemField; // item number display 
  22.     private NumberFormat nstockFormat; // format field and parse numbers 
  23.     private NumberFormat priceFormat; // format field and parse numbers 
  24.     private JButton btnAdd; // first button 
  25.     private JButton btnPrev; // previous button 
  26.     private JButton btnNext; // next button 
  27.     private JButton btnDel; // last button 
  28.     private JButton btnFirst; // first button
  29.     private JButton btnLast; // last button
  30.     private JButton btnModify; // modify button
  31.     private JButton btnSave; // save button
  32.     private JButton btnSearch; // search button
  33.     private JPanel buttonJPanel; // JPanle to hold buttons 
  34.     private JPanel fieldJPanel; // JPanel to hold labels and displays 
  35.     private JPanel fontJPanel; // JPanel to display logo 
  36.     private int currCD; 
  37.     private double total = 0; // variable for total inventory 
  38.     private JList Inventorylist; // JList to take place of old array
  39.     private DefaultListModel listModel;
  40.     private JScrollPane jScrollPanel;  
  41.     private float Invtotal = .00f;
  42.  
  43.      DecimalFormat formatter = new DecimalFormat("0.00");
  44.  
  45.     public Inventory2() // create class and method to perform GUI build 
  46.     { 
  47.         initComponents(); 
  48.     } 
  49.  
  50.     private void initComponents() 
  51.     { 
  52.  
  53.         // create label names 
  54.         cdNameLabel = new JLabel("CD Name:"); 
  55.         artistLabel = new JLabel("Artist:");
  56.         nstockLabel = new JLabel("In Stock:"); 
  57.         priceLabel = new JLabel("Each Item Cost:$"); 
  58.         itemLabel = new JLabel("Item Number:"); 
  59.  
  60.  
  61.         // initial fields
  62.         cdNameField = new JTextField(25);
  63.         cdNameField.setEditable(true);
  64.         artistField = new JTextField(15);
  65.         artistField.setEditable(true);
  66.         nstockField = new JFormattedTextField(nstockFormat);
  67.         nstockField.setEditable(true);
  68.         nstockField.setColumns(5);
  69.         priceField = new JFormattedTextField(priceFormat);
  70.         priceField.setEditable(true);
  71.         priceField.setColumns(5);
  72.         itemField = new JTextField(4);
  73.         itemField.setEditable(true);
  74.  
  75.         // JList
  76.         jScrollPanel = new JScrollPane();
  77.         Inventorylist = new JList(); 
  78.         currCD = 0;
  79.  
  80.  
  81.         // buttons 
  82.         btnAdd = new JButton(); 
  83.         btnNext = new JButton(); 
  84.         btnPrev = new JButton(); 
  85.         btnDel = new JButton();
  86.         btnLast = new JButton();
  87.         btnFirst = new JButton();
  88.         btnModify = new JButton(); 
  89.         btnSave = new JButton();
  90.         btnSearch = new JButton();
  91.  
  92.         getContentPane().setLayout(new FlowLayout());
  93.         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  94.  
  95.         // add shapesJPanel to frame    
  96.         ShapesJPanel logo = new ShapesJPanel();
  97.         logo.setPreferredSize(new Dimension(200,200));
  98.         getContentPane().add( logo );
  99.  
  100.         // place textFields and labels
  101.  
  102.         //artist
  103.         artistLabel.setText("Artist"); 
  104.         getContentPane().add(artistLabel); 
  105.  
  106.         artistField.setMinimumSize(new Dimension(70,20)); 
  107.         artistField.setPreferredSize(new Dimension(70,20));
  108.         getContentPane().add(artistField); 
  109.  
  110.         // cd name
  111.         cdNameLabel.setText("CD Name"); 
  112.         getContentPane().add(cdNameLabel); 
  113.  
  114.         cdNameField.setMinimumSize(new Dimension(70,20)); 
  115.         cdNameField.setPreferredSize(new Dimension(70,20));
  116.         getContentPane().add(cdNameField);
  117.  
  118.         // copies in stock
  119.         nstockLabel.setText("Copies In Stock"); 
  120.         getContentPane().add(nstockLabel); 
  121.  
  122.         nstockField.setMinimumSize(new Dimension(5,20)); 
  123.         nstockField.setPreferredSize(new Dimension(5,20));
  124.         getContentPane().add(nstockField); 
  125.  
  126.         //price of cd
  127.         priceLabel.setText("Price: $"); 
  128.         getContentPane().add(priceLabel); 
  129.  
  130.         priceField.setMinimumSize(new Dimension(20,20)); 
  131.         priceField.setPreferredSize(new Dimension(20,20));
  132.         getContentPane().add(priceField); 
  133.  
  134.         //item number of cd
  135.         itemLabel.setText("Item Number"); 
  136.         getContentPane().add(itemLabel); 
  137.  
  138.         itemField.setMinimumSize(new Dimension(5,20)); 
  139.         itemField.setPreferredSize(new Dimension(5,20));
  140.         getContentPane().add(itemField); 
  141.  
  142.  
  143.  
  144.         // add listeners
  145.  
  146.         btnAdd.setText("Add");
  147.         btnAdd.addActionListener(new ActionListener()
  148.         {
  149.             public void actionPerformed(ActionEvent evt)
  150.             {
  151.                 btnAddActionPerformed(evt);
  152.             }
  153.         });
  154.         getContentPane().add(btnAdd);
  155.  
  156.         // PREVIOUS
  157.         btnPrev.setText("Previous");
  158.         btnPrev.addActionListener(new ActionListener()
  159.         {
  160.             public void actionPerformed(ActionEvent evt)
  161.             {
  162.                 btnPrevActionPerformed(evt);
  163.             }
  164.         });
  165.         getContentPane().add(btnPrev);
  166.  
  167.         // NEXT
  168.         btnNext.setText("Next");
  169.         btnNext.addActionListener(new ActionListener()
  170.         {
  171.             public void actionPerformed(ActionEvent evt)
  172.             {
  173.                 btnNextActionPerformed(evt);
  174.             }
  175.         });
  176.         getContentPane().add(btnNext);
  177.  
  178.         // SEARCH
  179.         btnSearch.setText("Search");
  180.         btnSearch.addActionListener(new ActionListener()
  181.         {
  182.             public void actionPerformed(ActionEvent evt)
  183.             {
  184.                 btnAddActionPerformed(evt);
  185.             }
  186.         });
  187.         getContentPane().add(btnSearch);
  188.  
  189.         // FIRST
  190.         btnFirst.setText("First");
  191.         btnFirst.addActionListener(new ActionListener()
  192.         {
  193.             public void actionPerformed(ActionEvent evt)
  194.             {
  195.                 btnFirstActionPerformed(evt);
  196.             }
  197.         });
  198.         getContentPane().add(btnFirst);
  199.  
  200.         // LAST
  201.         btnLast.setText("Last");
  202.         btnLast.addActionListener(new ActionListener()
  203.         {
  204.             public void actionPerformed(ActionEvent evt)
  205.             {
  206.                 btnLastActionPerformed(evt);
  207.             }
  208.         });
  209.         getContentPane().add(btnLast);
  210.  
  211.         // MODIFY
  212.         btnModify.setText("Modify");
  213.         btnModify.addActionListener(new ActionListener()
  214.         {
  215.             public void actionPerformed(ActionEvent evt)
  216.             {
  217.                 btnModifyActionPerformed(evt);
  218.             }
  219.         });
  220.         getContentPane().add(btnModify);
  221.  
  222.         // SAVE
  223.         btnSave.setText("Save");
  224.         btnSave.addActionListener(new ActionListener()
  225.         {
  226.             public void actionPerformed(ActionEvent evt)
  227.             {
  228.                 btnAddActionPerformed(evt);
  229.             }
  230.         });
  231.         getContentPane().add(btnSave);
  232.  
  233.         // DELETE
  234.         btnDel.setText("Delete");
  235.         btnDel.addActionListener(new ActionListener()
  236.         {
  237.             public void actionPerformed(ActionEvent evt)
  238.             {
  239.                 btnDeleteActionPerformed(evt);
  240.             }
  241.         });
  242.         getContentPane().add(btnDel);
  243.  
  244.  
  245.         // new Jlist model
  246.         listModel = new DefaultListModel();
  247.         Inventorylist.setModel(listModel);
  248.  
  249.         jScrollPanel.setViewportView(Inventorylist);
  250.  
  251.         getContentPane().add(jScrollPanel);
  252.  
  253.         pack();
  254.     }// close
  255.  
  256.  
  257.         private void btnAddActionPerformed(ActionEvent evt)
  258.         {
  259.             // Create cd to add
  260.             CdwArtist newCD = new CdwArtist();
  261.             newCD.setArtist(artistField.getText());
  262.             newCD.setName(cdNameField.getText());    
  263.             newCD.setItemno(Integer.parseInt(itemField.getText()));
  264.             newCD.setNstock(Integer.parseInt(nstockField.getText()));
  265.             newCD.setPrice(Float.parseFloat(priceField.getText()));
  266.  
  267.             // Add cd to list
  268.             listModel.addElement(newCD);
  269.             currCD = listModel.size()-1;  // sets currCD to added index
  270.  
  271.  
  272.             // Clear the text fields after add
  273.             artistField.setText(null);
  274.             cdNameField.setText(null);    
  275.             itemField.setText(null);
  276.             nstockField.setText(null);
  277.          priceField.setText(null);
  278.  
  279.             }// end ADD
  280.  
  281.         private void btnPrevActionPerformed(ActionEvent evt)
  282.         {
  283.             // Grab Previous cd 
  284.             if (--currCD<0) currCD = listModel.size()-1;
  285.             CdwArtist newCD = (CdwArtist) listModel.get( currCD );
  286.  
  287.  
  288.             artistField.setText(newCD.getArtist());
  289.             cdNameField.setText(newCD.getName());    
  290.             itemField.setText(String.valueOf(newCD.getItemno()));
  291.             nstockField.setText(String.valueOf(newCD.getNstock()));
  292.             priceField.setText(formatter.format(newCD.getPrice()));
  293.  
  294.  
  295.             }// end PREV
  296.  
  297.                 private void btnNextActionPerformed(ActionEvent evt)
  298.             {
  299.             // Grab Next cd 
  300.             if (++currCD >= listModel.size()) currCD= 0;
  301.             CdwArtist newCD = (CdwArtist) listModel.get( currCD );
  302.  
  303.  
  304.             artistField.setText(newCD.getArtist());
  305.             cdNameField.setText(newCD.getName());    
  306.             itemField.setText(String.valueOf(newCD.getItemno()));
  307.             nstockField.setText(String.valueOf(newCD.getNstock()));
  308.             priceField.setText(formatter.format(newCD.getPrice()));
  309.  
  310.  
  311.             }// end NEXT
  312.  
  313.  
  314.                 private void btnFirstActionPerformed(ActionEvent evt)
  315.             {
  316.             // Grab First cd 
  317.             CdwArtist newCD = (CdwArtist) listModel.get(0);
  318.             currCD = 0;
  319.  
  320.             artistField.setText(newCD.getArtist());
  321.             cdNameField.setText(newCD.getName());    
  322.             itemField.setText(String.valueOf(newCD.getItemno()));
  323.             nstockField.setText(String.valueOf(newCD.getNstock()));
  324.             priceField.setText(formatter.format(newCD.getPrice()));
  325.  
  326.  
  327.             }// end FIRST
  328.  
  329.  
  330.                 private void btnLastActionPerformed(ActionEvent evt)
  331.             {
  332.             // Grab Last cd 
  333.             CdwArtist newCD = (CdwArtist) listModel.lastElement();
  334.             currCD = listModel.size()-1;
  335.  
  336.  
  337.             artistField.setText(newCD.getArtist());
  338.             cdNameField.setText(newCD.getName());    
  339.             itemField.setText(String.valueOf(newCD.getItemno()));
  340.             nstockField.setText(String.valueOf(newCD.getNstock()));
  341.             priceField.setText(formatter.format(newCD.getPrice()));
  342.  
  343.  
  344.             }// end LAST
  345.  
  346.                 private void btnDeleteActionPerformed(ActionEvent evt)
  347.             {
  348.             // Delete cd 
  349.             listModel.remove(currCD);
  350.  
  351.  
  352.             // Clear the text fields after delete
  353.             artistField.setText(null);
  354.             cdNameField.setText(null);    
  355.             itemField.setText(null);
  356.             nstockField.setText(null);
  357.          priceField.setText(null);
  358.  
  359.  
  360.             }// end DELETE
  361.  
  362.                 private void btnModifyActionPerformed(ActionEvent evt)
  363.             {
  364.             // Modify cd
  365.             listModel.remove(currCD);
  366.  
  367.             // Create cd to add
  368.             CdwArtist newCD = new CdwArtist();
  369.             newCD.setArtist(artistField.getText());
  370.             newCD.setName(cdNameField.getText());    
  371.             newCD.setItemno(Integer.parseInt(itemField.getText()));
  372.             newCD.setNstock(Integer.parseInt(nstockField.getText()));
  373.             newCD.setPrice(Float.parseFloat(priceField.getText()));
  374.  
  375.             // Add cd to list
  376.             listModel.addElement(newCD);
  377.             currCD = listModel.size()-1;  // sets currCD to added index
  378.  
  379.  
  380.             // Clear the text fields after add
  381.             artistField.setText(null);
  382.             cdNameField.setText(null);    
  383.             itemField.setText(null);
  384.             nstockField.setText(null);
  385.          priceField.setText(null);
  386.  
  387.  
  388.             }// end Modify
  389.  
  390.             // Search the Name Field
  391.             cdNameField.addCaretListener(new CaretListener()
  392.             {
  393.             public void caretUpdate(CaretEvent e)
  394.                 {
  395.                 JTextField f = (JtextField)e.getSource();
  396.                 final String s = f.getText();
  397.  
  398.  
  399.         // run it
  400.         public static void main(String args[])
  401.         {
  402.         JFrame frame = new JFrame( "CD Inventory Logo" );
  403.       frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  404.  
  405.          java.awt.EventQueue.invokeLater(new Runnable()
  406.             {
  407.             public void run()
  408.                 {
  409.                 new Inventory2().setVisible(true);
  410.                }
  411.             });
  412.         }
  413.  
  414. } // close class
Aug 1 '07 #1
21 2875
JosAH
11,448 Recognized Expert MVP
Don't allow the user to enter that (integer) key him/herself. Users are stupid
beings that make mistakes all the time. Have you had a look at the Bitset
class yet? I might come in handy.

kind regards,

Jos
Aug 1 '07 #2
no1zson
38 New Member
I did. It is what I thought of useing, but the API just confuses me.
I do not yet understand how to use the API properly, so I have been scanning the internet for code examples of one that is actually being used, but I cannot find a model to try and use to set mine up.
Aug 1 '07 #3
JosAH
11,448 Recognized Expert MVP
I did. It is what I thought of useing, but the API just confuses me.
It just allows you to set bits or reset them; either single bits or a range of them.
You can also go fancy and 'or', 'and', 'xor' and whatever with two entire bit sets.
You just need the simple stuff: get the first bit that wasn't set to true and set
a bit to true or false. Compare it with a boolean[] array except that a BitSet is
more clever than that.

kind regards,

Jos
Aug 1 '07 #4
no1zson
38 New Member
wow. Now I feel dumb. I know nothing about arrays, and boolean is just a term to me at this point for true or false.

I understand the basic concept, but have no idea how to code it.

Am I reformatting the field? Am I creating a whole new field? Am I creating this bit and then applying it to my itemno field?

I am lost.
Aug 1 '07 #5
JosAH
11,448 Recognized Expert MVP
wow. Now I feel dumb. I know nothing about arrays, and boolean is just a term to me at this point for true or false.

I understand the basic concept, but have no idea how to code it.

Am I reformatting the field? Am I creating a whole new field? Am I creating this bit and then applying it to my itemno field?

I am lost.
Read all about that BitSet class and judge for yourself whether you can use it
or not; maybe my suggestion was all wrong. You won't know until you've understood
what that set is all about ;-)

kind regards,

Jos
Aug 1 '07 #6
no1zson
38 New Member
I have read it, and I do think it will help, but reading about it does not show me how to code it in to my existing program. That is what I do not understand.

I understand that you do not wish to just hand out code,

but could you show code examples of what I might try? I have never seen this implemented anywhere, it is hard for me to see how I would put it in action with what I already have.
Aug 1 '07 #7
JosAH
11,448 Recognized Expert MVP
I have read it, and I do think it will help, but reading about it does not show me how to code it in to my existing program. That is what I do not understand.

I understand that you do not wish to just hand out code,

but could you show code examples of what I might try? I have never seen this implemented anywhere, it is hard for me to see how I would put it in action with what I already have.
There's a saying: "code to the interface, not to the implementation". You want a
'thing' that gives you a unique integer number when you ask for it and you can
return a number (previously given to you by that 'thing') telling it that it can be
used again in the future if you ask for another number again.

Those numbers are your primary keys for your CD collection.

Here goes:

Expand|Select|Wrap|Line Numbers
  1. public interface PrimaryKeyGenerator {
  2.    public int give();
  3.    public void take();
  4. }
  5.  
If you pass a PrimaryKeyGenerator (an interface!) to your CD collection at
construction time (store it as a member variable), you can use it all over the
place, i.e. when you insert a new CD you ask it to give() you a number; when
you delete a CD you let that object take() it back.

You should be able to program and compile your CD collection class using this
interface; of course it won't run (yet) because there is no implementation of this
interface yet.

Here's a stupid implementation: i.e. it generates the numbers 0, 1, 2 ... and
it doesn't do anything when you make it take() a number back again. You
obviously don't want this implementation as a final implementation but it is
enough to get you started. We'll implement a clever PrimaryKeyGenerator
later when you understand that BitSet API. Here's the stupid interface implementation:

Expand|Select|Wrap|Line Numbers
  1. public class StupidPrimaryKeyGenerator implements PrimaryKeyGenerator {
  2.    private int key;
  3.    public synchronized int give() { return key++; }
  4.    public void take() { }
  5.  
kind regards,

Jos
Aug 2 '07 #8
no1zson
38 New Member
OK, that is an explaination I THINK I understand.

So, I do not want the user entering anything in to my itemField, I should probably just set editable to false and be done with it right off the bat.

I should then impliment the public int give(); every time a cd is added, which stupidkeygenerator will give the next available number?

Is that about the long and short of it, or am I twisted?

If all that is right, my only remaining question (at the moment) would be how does int give() know to give its number to itemField?
should I not also have to change the way the field is built and initialize?

currently I have this:

Expand|Select|Wrap|Line Numbers
  1. private JTextField itemField;
  2. and
  3. itemField = new JTextField(4);
  4. itemField.setEditable(true);
  5. and
  6. newCD.setItemno(Integer.parseInt(itemField.getText()));
  7. when a cd is added
If I am to code the interface, and not the implementation, will not most of that need to change also?
Aug 2 '07 #9
r035198x
13,262 MVP
OK, that is an explaination I THINK I understand.

So, I do not want the user entering anything in to my itemField, I should probably just set editable to false and be done with it right off the bat.

I should then impliment the public int give(); every time a cd is added, which stupidkeygenerator will give the next available number?

Is that about the long and short of it, or am I twisted?

If all that is right, my only remaining question (at the moment) would be how does int give() know to give its number to itemField?
should I not also have to change the way the field is built and initialize?

currently I have this:

Expand|Select|Wrap|Line Numbers
  1. private JTextField itemField;
  2. and
  3. itemField = new JTextField(4);
  4. itemField.setEditable(true);
  5. and
  6. newCD.setItemno(Integer.parseInt(itemField.getText()));
  7. when a cd is added
If I am to code the interface, and not the implementation, will not most of that need to change also?
But now in that class you have a KeyGenerator object, so instead of setting itemNo with value entered by the user, you simply call the give method and use the number that it has generated as the itemNo
Aug 2 '07 #10
JosAH
11,448 Recognized Expert MVP
OK, that is an explaination I THINK I understand.

So, I do not want the user entering anything in to my itemField, I should probably just set editable to false and be done with it right off the bat.

I should then impliment the public int give(); every time a cd is added, which stupidkeygenerator will give the next available number?
Erm, you should just invoke the give() method if you want a new primary key
for a new CD. The method is implemented by the implementation of that interface
(here it's the stupid implementation which you pass to the constructor of your
CD collection object just once).

Is that about the long and short of it, or am I twisted?
Yep, I think it's right: you pass a key generator to the constructor of your CD
collection class and the methods of your class use that thing when they want
a new number or if they want to give a number back to it.

If all that is right, my only remaining question (at the moment) would be how does int give() know to give its number to itemField?
should I not also have to change the way the field is built and initialize?
It doesn't know that: all it does is give a new unique number; your methods
are supposed to stick that number in a text field or in some other place.

currently I have this:

Expand|Select|Wrap|Line Numbers
  1. private JTextField itemField;
  2. and
  3. itemField = new JTextField(4);
  4. itemField.setEditable(true);
  5. and
  6. newCD.setItemno(Integer.parseInt(itemField.getText()));
  7. when a cd is added
If I am to code the interface, and not the implementation, will not most of that need to change also?
Nope, don't parse anything from that textfield; use that number the generator
give()s you. It's a unique number and that's what you want. If you want to display
the data given a currCD, simply retrieve it from your JList and display it in the
text field (the user can't edit it anyway).

kind regards,

Jos
Aug 2 '07 #11
no1zson
38 New Member
OK. after two hours, several curse words I think I am pretty close.
I tried to take all that and weave it into the fabric of my current code.
This is what I have.
I took the generator and put it into my primary Inventory class
Expand|Select|Wrap|Line Numbers
  1. public interface PrimaryKeyGenerator
  2.     {
  3.     public int give();
  4.     public void take();
  5.     }
No compile issues so I moved forward.
I then took the code from your implementor class in put it in my objects class
cdwartist where I already have several object defined.
Expand|Select|Wrap|Line Numbers
  1.  // returns indivudual inventory value for a disk
  2.  public float getTotal()
  3.  {
  4.  return(price * nstock)+((price * nstock)* restock);
  5.  }    
  6.  
  7.  // generates automotic number for itemno
  8.  public synchronized int give()
  9.  {
  10.  return key++;
  11.  }
  12.  // takes back number from itemno 
  13.  public void take()
  14.  {
  15.  }
and
Expand|Select|Wrap|Line Numbers
  1.  private float invtotal; // total inventory value of all cds
  2.  private int key; // generator for itemno field
No compile issues there, so I think I am getting somewhere. I move back to my primary inventory class to make the itemno field use this key everytime a cd is added ...
Expand|Select|Wrap|Line Numbers
  1.     // Create cd to add
  2.             CdwArtist newCD = new CdwArtist();
  3.             newCD.setArtist(artistField.getText());
  4.             newCD.setName(cdNameField.getText());
  5.             newCD.setItemno(give());    
  6.             //newCD.setItemno(Integer.parseInt(itemField.getText()));
  7.             newCD.setNstock(Integer.parseInt(nstockField.getText()));
  8.             newCD.setPrice(Float.parseFloat(priceField.getText()));
I left the old one in (commented out) until I get this one working, but this is where I hit a snag, and it errors saying
Inventory2.java:289: cannot find symbol
symbol : method give()
location: class Inventory2
newCD.setItemno(give());


I think I am close but just missing a simple step somewhere ... any suggestions?
Aug 2 '07 #12
JosAH
11,448 Recognized Expert MVP
OK. after two hours, several curse words I think I am pretty close.
I tried to take all that and weave it into the fabric of my current code.
You did what? You should have made at least three separate files:

1) PrimaryKeyGenerator.java
2) StupidPrimaryKeyGenerator.java
3) CDInventory.java

and your add and delete functionality should just have invoked the give() and
take() methods. The give() call on the PrimaryKeyGenerator should be the
substitute for parsing that text field.

kind regards,

Jos
Aug 3 '07 #13
r035198x
13,262 MVP
Object-oriented programming tries to run away from all that weaving and lumping together of functionality.
Code that achieves different functionality should be kept as separate as possible so that different functionalities can be worked on at different times (and possibly by different people too).
In your case the unique number generating functionality should not be mixed with the rest of the inventory management code. Always be on the look out for this when designing your programs. If you require new functionality that consists of many actions(e.g generation and management of primary keys) you must add a new object (read class) to your system that can do this functionality and implements the required methods without affecting the rest of your code.
Aug 3 '07 #14
no1zson
38 New Member
OK, my post was a little ambiguous.
I did use three classes, two of them I already had.
the first, as you pointed out was Inventory.java (my application)
I initialized the components
Expand|Select|Wrap|Line Numbers
  1. public interface PrimaryKeyGenerator
  2.         {
  3.         public int give();
  4.         public void take();
  5.         }
the second was simpleprimarykeygenerator.java
here is pretty self explainatory, i put
public class SimplePrimaryKeyGenerator implements PrimaryKeyGenerator
Expand|Select|Wrap|Line Numbers
  1. {
  2. private int key;
  3.  
  4.     public synchronized int give()
  5.     {
  6.     return key++;
  7.     }
  8.  
  9.     public void take()
  10.     {
  11.     }
  12. } // end class
and the third was cdwartist.java that I already had, and where I define many of my objects. I put
Expand|Select|Wrap|Line Numbers
  1. // generates automotic number for itemno
  2.  public synchronized int give()
  3.  {
  4.  return key++;
  5.  }
  6.  // takes back number from itemno 
  7.  public void take()
  8.  {
  9.  }
Is that not acceptable? I did not make a new class for each one because I thought it would be better to group like objects.
Aug 3 '07 #15
JosAH
11,448 Recognized Expert MVP
OK, my post was a little ambiguous.
I did use three classes, two of them I already had.
the first, as you pointed out was Inventory.java (my application)
I initialized the components
Expand|Select|Wrap|Line Numbers
  1. public interface PrimaryKeyGenerator
  2.         {
  3.         public int give();
  4.         public void take();
  5.         }
the second was simpleprimarykeygenerator.java
here is pretty self explainatory, i put
public class SimplePrimaryKeyGenerator implements PrimaryKeyGenerator
Expand|Select|Wrap|Line Numbers
  1. {
  2. private int key;
  3.  
  4.     public synchronized int give()
  5.     {
  6.     return key++;
  7.     }
  8.  
  9.     public void take()
  10.     {
  11.     }
  12. } // end class
and the third was cdwartist.java that I already had, and where I define many of my objects. I put
Expand|Select|Wrap|Line Numbers
  1. // generates automotic number for itemno
  2.  public synchronized int give()
  3.  {
  4.  return key++;
  5.  }
  6.  // takes back number from itemno 
  7.  public void take()
  8.  {
  9.  }
Is that not acceptable? I did not make a new class for each one because I thought it would be better to group like objects.
Erm, no, I don't find that acceptable because you're defining those methods
again which you defined before in a class that implements the PrimaryKeyGenerator
interface. What's the use of that? Never repeat yourself. All you need is that
SimplePKG (acronym alert!) as the implementation for the interface; there's
no need to repeat the implementation anywhere else.

Think of an interface as a 'promiss' or a 'contract'; think of the SimplePKG class
as an implementation of that contract or a fulfillment of that contract. You don't
need anything else.

kind regards,

Jos
Aug 3 '07 #16
no1zson
38 New Member
Well, as I was going through and coding everything it dawned on me, I am already using a counter in my app, currCD that is tied to my list index.

I simply changed two lines of code.

I set itemno=(currCD+1) so that it would not start with 0 as the index does
and I added ++currCD to my ADD button so that it would keep up with my new cds.

It actually took longer to clean out all the stuff I put in today than it did to get that to work! :o)

Thanks guys. have a nice weekend.
Aug 3 '07 #17
JosAH
11,448 Recognized Expert MVP
Well, as I was going through and coding everything it dawned on me, I am already using a counter in my app, currCD that is tied to my list index.

I simply changed two lines of code.

I set itemno=(currCD+1) so that it would not start with 0 as the index does
and I added ++currCD to my ADD button so that it would keep up with my new cds.

It actually took longer to clean out all the stuff I put in today than it did to get that to work! :o)

Thanks guys. have a nice weekend.
currCD cannot be the primary key for a new CD, because currCD is some sort
of cursor in your table, e.g. it could be 0 (first CD in the list) and you don't want
another CD with that value as its primary key.

kind regards,

Jos
Aug 4 '07 #18
no1zson
38 New Member
I do not know about all that, but it is working as planned, so I am leaving it alone before I break it. :o)

Thanks Jos
Aug 4 '07 #19
JosAH
11,448 Recognized Expert MVP
I do not know about all that, but it is working as planned, so I am leaving it alone before I break it. :o)

Thanks Jos
You're welcome of course but using the currCD member variable as a primary
key is not going to work; your heading off for the wrong track if you try it that
way; you'll see (this is not a threat ;-)

kind regards,

Jos
Aug 5 '07 #20
no1zson
38 New Member
I think the term "primary number" is our problem. I am not sure what that means or implies, I simply wanted itemno field to be automatically kept in ascending order instead of user entered.
Each new disk has a number higher than the previous last numbered disk.

From all my testing and retesting this solution satisfies that requirement.
Aug 5 '07 #21
JosAH
11,448 Recognized Expert MVP
I think the term "primary number" is our problem. I am not sure what that means or implies, I simply wanted itemno field to be automatically kept in ascending order instead of user entered.
Each new disk has a number higher than the previous last numbered disk.

From all my testing and retesting this solution satisfies that requirement.
Maybe I misunderstood your description of your new algorithm. If it ain't broke,
don't fix it ;-)

kind regards,

Jos
Aug 6 '07 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

1
5425
by: Roman Thurnherr | last post by:
Hi all I'm using two JLists with the same MouseListener in a JFrame. How can I find out on which JList the MouseEvent was released? Is it generally possible or should I use a MouseListener for each JList? Thanx for your help Roman
20
2034
by: Prakash | last post by:
Hi ! I have a field "sub_tran_no" in my form in continuous view. When the user presses a button "Re-Number", I'd like to: 1) Save the current record pointer position 2) Save the current field which has focus 3) go to the top of the recordset & start re-numbering the "sub_tran_no" field with the values 1,2,3, until eof.
1
2397
by: Salha | last post by:
Hello there, Can you help me in following problem: Am working on a chemistry dictionary. I want to have all the chemistry terms in a JList and whenever i select a term from a JList, it displays the meaning in a text area (as any encyclopedia) and the term in a textfield. My questions: 1. How can i load the chemistry terms in to the JList...
0
1372
by: paoparaopao | last post by:
Hi, I want to drag rows up/down in a JList; for example, drag 3rd row to 1st row. I have setDragEnabled(true) for JList, but it doesn't work! How do I drag rows in JList? Thanks!
4
2057
Nepomuk
by: Nepomuk | last post by:
Hi! I'm trying to use a JList. Here's the code: String languages = { "deutsch", // german "english", // english "castellano", // castilian "français", // french "italiano", // italian "日本語", // japanese
8
3155
by: sukatoa | last post by:
I came from List in awt.... Im currently having an experiment about JList.... say like 140,280 ( h,w ) i have 4 values that have been set on JList using addElement from DefaultListModel....
2
1941
by: wishbone34 | last post by:
Hi, here is some of my code public class ControlIL extends JFrame implements ActionListener, ListSelectionListener { private JList Items; //builds the UI public ControlIL(){ DefaultListModel model = new DefaultListModel();
2
2862
by: Gangreen | last post by:
Hi all, I've bumped into an odd problem creating a GUI in swing. As you will see in the screenshots, I have a tabbed layout. Each tab is a Jpanel. On this panel I have put a JList, containing a series of objects. My intention is to have a single column of objects in the list, and have it to scroll when the objects exceed the vertical space. ...
2
6274
by: Gangreen | last post by:
hi, I have an arrayList in my program that I want to display in a Jlist. I need the JList to update his contents when the elements in the arraylist change/ an element is added/deleted/... I'm lost on how to do this. Passing the arrayList as an array into the Jlistconstructor will just display the elements, but no updates will be made...
0
7397
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7563
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7125
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5703
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4757
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3252
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3239
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.