Hi there!
I have a little project for school that is due today. It meets all of the "requirements" but I am not completely happy with the output. (This is an online course and my
Java resources are limited to my textbook, the internet and this forum.) In my non-GUI program the doubles appeared at 2 digits, now when I do the math, my dollars and cents come out with any variety of decimal places. I have done some reading on Big Decimal, but am confused as to how to apply it here. I am used to the primitive data types and can't seem to find clarity on how to use it. One of my classmates suggested using int for everything and dividing by 100... well it works kind of... and leaves me with no decimal places at all! Below is my code, since I have read too many posts that say they can't help without it.... Thanks in advance!
class InventoryTest
{
public void buildGUI()
{
Item itemObjects[] = new Item[ 6 ];
itemObjects[ 0 ] = new Item( "Air Care Filter", 01624, 5, 12.50, 1 );
itemObjects[ 1 ] = new Item( "Clean Effects 24\"", 10024, 12, 734.56, 1 );
itemObjects[ 2 ] = new Item( "Clean Effects 21\"", 10021, 24, 726.15, 1 );
itemObjects[ 3 ] = new Item( "Clean Effects 17.5\"", 10017, 3, 697.89, 1 );
itemObjects[ 4 ] = new Item( "Thermostat TCONT", 802, 18, 98.57, 1 );
itemObjects[ 5 ] = new Item( "Paper Filter Package", 11224, 6, 2.35, 6 );
Vector colNames = new Vector(Arrays.asList(new
String[]{"Item Name","Product Number","Item Count","Cost Each","Bundled Quantity","Restocking Fee","Inventory Value"}));
Vector rowData = new Vector();
for(int x = 0, y = itemObjects.length; x < y; x++)
{
Vector row = new Vector();
row.add(itemObjects[x].getitemName());
row.add(itemObjects[x].getproductNumber());
row.add(itemObjects[x].getitemCount());
row.add(itemObjects[x].getcostEach());
row.add(itemObjects[x].getquantityInBundle());
row.add(itemObjects[x].getrestockingFee());
row.add(itemObjects[x].getitemTotalValue());
rowData.add(row);
}
DefaultTableModel dtm = new DefaultTableModel(rowData,colNames);
final JTable table = new JTable(dtm);
JScrollPane sp = new JScrollPane(table);
sp.setPreferredSize(new Dimension(800,600));
JButton btn = new JButton("Calculate Total Value");
final JLabel lbl = new JLabel("TotalValue = $");
JPanel p = new JPanel(new GridLayout(1,2));
p.add(btn);
p.add(lbl);
JFrame f = new JFrame();
f.getContentPane().add(sp);
f.getContentPane().add(p,BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
double total = 0.0;
for(int x = 0, y = table.getRowCount(); x < y; x++)
{
total += new Double(""+table.getValueAt(x,6)).doubleValue();
}
lbl.setText("TotalValue = $" + new java.text.DecimalFormat("0.00").format(total));
}
});
}
}
[/code]
Again, thanks for any advice you can offer... Its not critical, but I would like to submit a more professional looking product.