Hedging my bets, I have coded a simpler version of the program Ezzaral is helping me with. I fear I am in over my head over there and may just begin to annoy him with silly questions.
Most of what he and I are working on is beyond the scope of my class anyways, so I did not mind trying to learn more as it made class simpler. I do not want to end up without a working program to turn in though, so I set out on this one this morning.
I am only having one issue at the moment, and it is with "super".
I am not sure why it is not working in this class.
I am getting cannot find symbol, and I do not understand why.
Can anyone offer assistance?
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.lang.*;
class CdwArtist extends Compactdisk
{
private String artist;
// Compactdisk constructor
public CdwArtist(String Name, int item, int nstock, double price, String artist)
{
super(Name, item, nstock, price);
artist = artist;
}
// get values
public void setArtist(String artist)
{
artist=artist;
}
// return value
public String getArtist()
{
return (artist);
}
// returns indivudual inventory value for a disk
public double calcRestock()
{
return price * nstock * 0.05;
}
} //End Class
which extends this class
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.lang.*;
class Compactdisk //implements Comparable
{// begin class
//InventoryCD class has 5 fields
protected String Name; // Name of cd
protected double price; // price of cd
protected int itemno; // item number of cd
protected int nstock; // how many units in stock
protected double value; // value for single cd inventory
//Compact disk class constructor
public Compactdisk(String diskName, int cdItemno, int Stock, double cdValue, double cdPrice)
// 4 fields need to be set up
{
Name = diskName;
price = cdPrice;
itemno = cdItemno;
nstock = Stock;
value = cdValue;
}
// set values
public void setName(String diskName)
{
Name = diskName;
}
public void setPrice(double cdprice)
{
price = cdprice;
}
public void setItemno(int cditemno)
{
itemno = cditemno;
}
public void setNstock(int Stock)
{
nstock = Stock;
}
public void setValue(double cdValue)
{
value = cdValue;
}
// return values
public String getName()
{
return Name;
}
public double getPrice()
{
return price;
}
public int getItemno()
{
return itemno;
}
public int getNstock()
{
return nstock;
}
// returns indivudual inventory value for a disk
public double calcValue()
{
return price * nstock;
}
}// end class which compiles fine. If you need to see the primary class just let me know, but everything looks fine to me here.
Any help would be welcome.
Crap. Just when I say I am doing fine I get these messages when I compile my main class.
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.lang.*;
public class Inventory3 extends JFrame
{
private JLabel cdNameLabel; // name label
private JLabel artistLabel; // item number label
private JLabel nstockLabel; // units in stock label
private JLabel priceLabel; // price each label
private JLabel itemLabel; // item number label
private JLabel valueLabel; // value of that item label
private JLabel rstkLabel; // cost to restock label
private JLabel totalLabel; // total value of inventory label
private JTextField cdNameField; // name display
private JTextField artistField; // artist display
private JFormattedTextField nstockField; // units in stock display
private JFormattedTextField priceField; // price each display
private JTextField itemField; // item number display
private JFormattedTextField valueField; // value of that item display
private JFormattedTextField rstkField; // cost to restock display
private JFormattedTextField totalField; // value of all inventory combine
private NumberFormat nstockFormat; // format field and parse numbers
private NumberFormat priceFormat; // format field and parse numbers
private NumberFormat valueFormat; // format field and parse numbers
private NumberFormat rstkFormat; // format field and parse numbers
private NumberFormat totalFormat;
private JButton btnAdd; // first button
private JButton btnPrev; // previous button
private JButton btnNext; // next button
private JButton btnDel; // delete button
private JButton btnLast; // last button
private JButton btnFirst; // first button
private JButton btnModify; // modify button
private JPanel buttonJPanel; // JPanle to hold buttons
private JPanel fieldJPanel; // JPanel to hold labels and displays
private JPanel fontJPanel; // JPanel to display logo
private int currCD; // current cd number
private int i; // iterator
private double total = 0; // variable for total inventory
private JScrollPane jScrollPanel;
public Inventory3() // create class and method to perform GUI build
{
initComponents();
}
private void initComponents()
{
// create label names
cdNameLabel = new JLabel("CD Name:");
artistLabel = new JLabel("Artist:");
nstockLabel = new JLabel("In Stock:");
priceLabel = new JLabel("Each Item Cost:$");
itemLabel = new JLabel("Item Number:");
valueLabel = new JLabel("Value of Item Inventory:$");
rstkLabel = new JLabel("Cost to Re-Stock Item:$");
totalLabel = new JLabel("Total Value of Inventory:$");
// initial fields
cdNameField = new JTextField(25);
cdNameField.setEditable(true);
artistField = new JTextField(15);
artistField.setEditable(true);
nstockField = new JFormattedTextField(nstockFormat);
nstockField.setEditable(true);
nstockField.setColumns(10);
priceField = new JFormattedTextField(priceFormat);
priceField.setEditable(true);
priceField.setColumns(10);
itemField = new JTextField(5);
itemField.setEditable(true);
valueField = new JFormattedTextField(valueFormat);
valueField.setEditable(true);
valueField.setColumns(10);
rstkField = new JFormattedTextField(rstkFormat);
rstkField.setEditable(true);
rstkField.setColumns(5);
totalField = new JFormattedTextField(totalFormat);
totalField.setColumns (10);
totalField.setEditable(true);
// buttons
btnAdd = new JButton("Add");
btnNext = new JButton("Prev");
btnPrev = new JButton("Next");
btnLast = new JButton("Last");
btnDel = new JButton("Delete");
btnModify = new JButton("Modify");
btnLast = new JButton ("Last");
// add buttons to panel
JPanel buttons = new JPanel(); // new panel
buttons.setLayout(new GridLayout(1,4)); // set layout
// add buttons
buttons.add(btnAdd);
buttons.add(btnPrev);
buttons.add(btnNext);
buttons.add(btnDel);
buttons.add(btnLast);
buttons.add(btnModify);
buttons.add(btnLast);
// create cd object
CdwArtist[] newCD;
newCD = new CdwArtist[5];
// create array objects
for (currCD = 0; currCD < 5; currCD++)
{
newCD[0] = new CdwArtist("Disturbed", 1, 10, 15.00, "Sickness");
newCD[1] = new CdwArtist("Seether", 2, 5, 10.00, "Disclaimer");
newCD[2] = new CdwArtist("Tupac", 3, 2, 7.00, "All Eyes on Me");
newCD[3] = new CdwArtist("WASP", 4, 3, 20.00, "Crimson Idol");
newCD[4] = new CdwArtist("Mearle Haggard", 5, 6, 10.00, "Mama Tried");
}
for (currCD = 0; currCD < 5; currCD++)
total += newCD[currCD].calcValue(); // total value of inventory
// add values to textFields
cdNameField.setText(currCD.getcdName());
artistField.setText(currCD.getArtist());
nstockField.setValue(new Float(currCD.getValue()));
priceField.setValue(new Float("$"+currCD.getPrice()));
itemField.setText(currCD.getItem());
valueField.setValue(new Float("$" + currCD.calcValue()));
rstkField.setValue(new Float(currCD.calcRestock()));
//totalField.setValue(new Float("$" + currCD.getValue()));
// add labels to panel
JPanel labels = new JPanel();
labels.setLayout(new GridLayout(0, 1));
labels.add(cdNameLabel);
labels.add(artistLabel);
labels.add(itemLabel);
labels.add(priceLabel);
labels.add(nstockLabel);
labels.add(valueLabel);
labels.add(rstkLabel);
labels.add(totalLabel);
// add fields to panel
JPanel fields = new JPanel();
fields.setLayout(new GridLayout(0, 1));
fields.add(cdNameField);
fields.add(artistField);
fields.add(itemField);
fields.add(priceField);
fields.add(nstockField);
fields.add(valueField);
fields.add(rstkField);
// put fields and names together
cdNameLabel.setLabelFor(cdNameField);
artistLabel.setLabelFor(artistField);
itemLabel.setLabelFor(itemField);
priceLabel.setLabelFor(priceField);
featureLabel.setLabelFor(featureField);
valueLabel.setLabelFor(valueField);
rstkLabel.setLabelFor(rstkField);
// add labels and fields to panel; labels left, fields right
JPanel display = new JPanel();
display.add(labels, BorderLayout.CENTER);
display.add(fields, BorderLayout.LINE_END);
// create display
JFrame invFrame = new JFrame();
invFrame.setLayout( new BorderLayout());
invFrame.setTitle("CD Inventory");
invFrame.add(fontJPanel, BorderLayout.NORTH);
invFrame.add(buttons, BorderLayout.SOUTH);
invFrame.add(display, BorderLayout.CENTER);
invFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // termination command
invFrame.setSize(800, 800); // set size of JPanel
invFrame.setLocationRelativeTo(null); // set screem location
invFrame.setVisible(true);
// add listeners
// first button
btnFirst.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
goFirst();
}
});
// last button
btnLast.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
goLast();
}
});
// previous button
btnPrev.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
goPrev();
}
});
// next button
btnNext.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
goNext();
}
});
}// close
// button action methods
// first
private void goFirst()
{
currCD = 0;
cdNameField.setText(currCD.getName());
artistField.setText(currCD.getArtist());
itemField.setValue(new Integer(currCD.getItemno()));
priceField.setValue("$"+new Float(currCD.getPrice()));
nstockField.setValue(new Integer(currCD.getNstock()));
valueField.setValue("$"+new Float(currCD.calcValue()));
rstkField.setValue("$"+new Double(currCD.calcRestock()));
}
// last
private void goLast()
{
currCD = 4;
cdNameField.setText(currCD.getName());
artistField.setText(currCD.getArtist());
itemField.setValue(new Integer(currCD.getItemno()));
priceField.setValue("$"+new Float(currCD.getPrice()));
nstockField.setValue(new Integer(currCD.getNstock()));
valueField.setValue("$"+new Float(currCD.calcValue()));
rstkField.setValue("$"+new Double(currCD.calcRestock()));
}
// Previous
private void goPrev()
{
if (currCD > 0)
{
currCD--;
cdNameField.setText(currCD.getName());
artistField.setText(currCD.getArtist());
itemField.setValue(new Integer(currCD.getItemno()));
priceField.setValue("$"+new Float(currCD.getPrice()));
nstockField.setValue(new Integer(currCD.getNstock()));
valueField.setValue("$"+new Float(currCD.calcValue()));
rstkField.setValue("$"+new Double(currCD.calcRestock()));
}
}
// Next
private void goNext()
{
if (currCD < 4)
{
currCD++;
cdNameField.setText(currCD.getName());
artistField.setText(currCD.getArtist());
itemField.setValue(new Integer(currCD.getItemno()));
priceField.setValue("$"+new Float(currCD.getPrice()));
nstockField.setValue(new Integer(currCD.getNstock()));
valueField.setValue("$"+new Float(currCD.calcValue()));
rstkField.setValue("$"+new Double(currCD.calcRestock()));
}
}
// run it
public static void main(String args[])
{
Inventory3 inventory3 = new Inventory3();
}
// end of main method
} // end of class
Inventory3.java:128: int cannot be dereferenced
cdNameField.setText(currCD.getcdName());
^
Inventory3.java:129: int cannot be dereferenced
artistField.setText(currCD.getArtist());
I think some of my logic may be off. I have taken the day off work tomorrow to try and work through one or both of these programs to completion. The more coding experience I can get under my best with either application the better. Finals are coming up in a couple weeks.
Thanks again to anyone willing to help.
Here is the panel to that class just in case you want to see it.
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.lang.*;
class FontJPanel extends JPanel
{
// display welcome message
public void paintComponent( Graphics g )
{
super.paintComponent( g ); //call superclass's paintComponent
// set font to Monospaced (Courier), italic, 12pt and draw a string
g.setFont( new Font( "Monospaced", Font.ITALIC, 12 ) );
g.drawString( "min", 90, 70 );
// set font to Serif (Times), bold, 24pt and draw a string
g.setColor( Color.RED );
g.setFont( new Font( "Serif", Font.BOLD, 24 ) );
g.drawString( "OFFICE", 60, 60 );
} // end method paintComponent
} // end class FontJPanelCount the number of params in your super() call and take a look at the base constructor.
Inventory3.java:128: int cannot be dereferenced cdNameField.setText(currCD.getcdName()); ^ Inventory3.java:129: int cannot be dereferenced artistField.setText(currCD.getArtist());
I think some of my logic may be off. I have taken the day off work tomorrow to try and work through one or both of these programs to completion. The more coding experience I can get under my best with either application the better. Finals are coming up in a couple weeks.
Again, you are trying to use the integer index value like an object. You need to actually get that object (using currCD as the index) from your listModel as a CdwArtist object.
I knew that was the same error. I do not know why I cannot get that straight in my head.
I will work on that this evening.
Other than that, how does this look, as you are so familiar with the other version.
It looks like you are getting a lot closer to something that will work out. I don't see any way for a user to add a CD though and you have already filled your array to the hard-coded max of 5 items. I assume you have put that part in to test the workings of your first(), last(), etc. methods?
You are correct. With this one I am not going above and beyond anything that is being requested.
All I want with this is to be able to display the elements of the array one at a time, and perform those pesky calculations of course.
I will put an add and del button in sometime over the next few weeks if I stick with this one.
I am closing this out. I have put too much time into the other code to abandon it now.