Robert Wyatt
Posts: 2
Nickname: novice37
Registered: Apr, 2006
problem with HardwareStore.java
Posted: Apr 22, 2006 3:32 PM
Advertisement
I'm having trouble with HardwareStore.Java. I'm trying to add an additional column called total cost. There will be a total of five columns: record number, quantity, toolname, cost, and total cost. I'm posting the code below...any suggestions on getting this to work are greatly appreciated. /* * HardwareStore.java * Program simulates hardware store inventory checking. import java.awt.*; import java.io.*; import java.awt.event.*; import javax.swing.*; public class HardwareStore extends JFrame { private UpdateDialog updateDialog; private NewDialog newDialog; private DeleteDialog deleteDialog; private InventoryDialog inventoryDialog; private JMenuItem newItem, updateItem, deleteItem, inventoryItem, exitItem; private JDesktopPane desktop; private RandomAccessFile file; private Record records[]; private final int SIZE = 46; private int recordSize; public HardwareStore(int size) { super( "Hardware Store" ); recordSize = size; records = new Record[ recordSize ]; // open file try { file = new RandomAccessFile( "hardware.dat", "rw" ); int numRecords = (int)(file.length()/SIZE); if (numRecords == 0) { // new file for ( int i = 0; i < recordSize; i++ ) { file.seek(i * SIZE); records[ i ] = new Record(); records[ i ].write( file ); } } else { // existing fle if (recordSize != numRecords) System.out.println("Caution: Existing file has " + numRecords + " records, which is different " + "from the number: " + recordSize + " that you entered. You can only create or " + "update within the range of records that is the " + "smaller of the two"); int minimum = Math.min(recordSize, numRecords); for ( int i = 0; i < minimum; i++ ) { file.seek(i * SIZE); records[ i ] = new Record(); records[ i ].read(file); } if (recordSize > minimum) // Increase the file size with blank records for ( int i = minimum; i < recordSize; i++ ) { file.seek(i * SIZE); records[ i ] = new Record(); records[ i ].write( file ); } } } catch ( IOException ioException ) { System.err.println( ioException.toString() ); System.exit( 1 ); } newDialog = new NewDialog( file, recordSize ); updateDialog = new UpdateDialog( file, recordSize ); deleteDialog = new DeleteDialog( file, recordSize ); inventoryDialog = new InventoryDialog( file, recordSize ); // set up GUI Container container = getContentPane(); desktop = new JDesktopPane(); container.add( desktop ); desktop.add( newDialog ); desktop.add( updateDialog ); desktop.add( deleteDialog ); desktop.add( inventoryDialog ); JMenuBar menuBar = new JMenuBar(); setJMenuBar( menuBar ); JMenu fileMenu = new JMenu( "File" ); menuBar.add( fileMenu ); newItem = new JMenuItem( "New Record" ); newItem.addActionListener( new ActionListener() { // anonymous inner class public void actionPerformed( ActionEvent event ) { newDialog.setVisible( true ); } } // end anonymous inner class ); // end call to addActionListener updateItem = new JMenuItem( "Update Record" ); updateItem.addActionListener( new ActionListener() { // anonymous inner class public void actionPerformed( ActionEvent event ) { updateDialog.setVisible( true ); } } // end anonymous inner class ); // end call to addActionListener deleteItem = new JMenuItem( "Delete Record" ); deleteItem.addActionListener( new ActionListener() { // anonymous inner class public void actionPerformed( ActionEvent event ) { deleteDialog.setVisible( true ); } } // end anonymous inner class ); // end call to addActionListener inventoryItem = new JMenuItem( "Show Inventory" ); inventoryItem.addActionListener( new ActionListener() { // anonymous inner class public void actionPerformed( ActionEvent event ) { inventoryDialog.setVisible( true ); inventoryDialog.display(); } } // end anonymous inner class ); // end call to addActionListener exitItem = new JMenuItem( "Exit" ); exitItem.addActionListener( new ActionListener() { // anonymous inner class public void actionPerformed( ActionEvent event ) { try { file.close(); } catch ( IOException ioException ) { System.exit( 1 ); } System.exit( 0 ); } } // end anonymous inner class ); // end call to addActionListener fileMenu.add( newItem ); fileMenu.add( updateItem ); fileMenu.add( deleteItem ); fileMenu.addSeparator(); fileMenu.add( inventoryItem ); fileMenu.addSeparator(); fileMenu.add( exitItem ); setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE ); setSize( 370, 330 ); setVisible( true ); } // end constructor public static void main( String args[] ) { if (args.length != 1) { System.out.println("Synopsis: java HardwareStore record_size"); System.exit(-1); } new HardwareStore(Integer.parseInt(args[0])); } } // end class HardwareStore // class for updating records class UpdateDialog extends JInternalFrame implements ActionListener{ // add declarations here public UpdateDialog( RandomAccessFile updateFile, int size ) { super( "Update Record" ); // add the body here } // process events public void actionPerformed( ActionEvent event ) { // add body here } // end method actionPerformed // clear text fields private void clear() { // add body here } } // end class UpdateDialog // class for creating new records class NewDialog extends JInternalFrame implements ActionListener { private RandomAccessFile file; private JTextField recordField, toolField, quantityField, costField, totalcostField; private JLabel recordLabel, toolLabel, quantityLabel, costLabel, totalcostLabel; private JButton saveButton, cancelButton; private Record data; private int recordNumber; private int recordSize; public NewDialog( RandomAccessFile newFile, int size ) { super( "New Record" ); recordSize = size; file = newFile; data = new Record(); // set up GUI recordLabel = new JLabel( "Record Number" ); recordField= new JTextField( 10 ); toolLabel = new JLabel( "Tool Name" ); toolField = new JTextField( 10 ); quantityLabel = new JLabel( "Quantity" ); quantityField = new JTextField( 10 ); costLabel = new JLabel( "Cost" ); costField = new JTextField( 10 ); totalcostLabel = new JLabel ( "TotalCost"); totalcostField = new JTextField( 12); saveButton = new JButton( "Save Changes" ); cancelButton = new JButton( "Cancel" ); saveButton.addActionListener( this ); cancelButton.addActionListener( this ); Container container = getContentPane(); container.setLayout( new GridLayout( 5, 2 ) ); container.add( recordLabel ); container.add( recordField ); container.add( toolLabel ); container.add( toolField ); container.add( quantityLabel ); container.add( quantityField ); container.add( costLabel ); container.add( costField ); container.add( totalcostLabel); container.add( totalcostField); container.add( saveButton ); container.add( cancelButton ); setSize( 300, 150 ); setVisible( false ); } // process events public void actionPerformed( ActionEvent event ) { // save button if ( event.getSource() == saveButton ) { recordNumber = Integer.parseInt( recordField.getText() ); // check if record number is within range if ( recordNumber < 1 || recordNumber > recordSize ) { JOptionPane.showMessageDialog( this, "Invalid record number", "Account Error", JOptionPane.ERROR_MESSAGE ); clear(); return; } // set file-pointer to appropriate location try { file.seek( ( recordNumber - 1 ) * data.SIZE ); data.read( file ); } catch ( IOException ioException ) { JOptionPane.showMessageDialog( this, "Error while reading from file", "Read Error", JOptionPane.ERROR_MESSAGE ); } // record number already exists if ( data.getRecordNumber() != 0 ) { JOptionPane.showMessageDialog( this, "Record number already exists", "Duplicate record number", JOptionPane.ERROR_MESSAGE ); clear(); return; } // set values of new record and write to file try { data.setRecordNumber( recordNumber ); data.setToolName( toolField.getText() ); data.setQuantity( Integer.parseInt( quantityField.getText() ) ); data.setCost( ( new Double( costField.getText() ) ).doubleValue() ); file.seek( ( recordNumber - 1 ) * data.SIZE ); data.write( file ); } catch ( IOException ioException ) { JOptionPane.showMessageDialog( this, "Error while writing to file", "Write Error", JOptionPane.ERROR_MESSAGE ); return; } setVisible( false ); clear(); } // cancel button else if ( event.getSource() == cancelButton ) { setVisible( false ); clear(); } } // end method actionPerformed // clear text fields private void clear() { recordField.setText( "" ); toolField.setText( "" ); quantityField.setText( "" ); costField.setText( "" ); totalcostField.setText( ""); } } // end class NewDialog // class for deleting records class DeleteDialog extends JInternalFrame implements ActionListener { // add declarations here public DeleteDialog( RandomAccessFile deleteFile, int size ) { super( "Delete Record" ); // add body here } // process events public void actionPerformed( ActionEvent event ) { // add body here } // end method actionPerformed } // end class DeleteDialog // class for displaying records class InventoryDialog extends JInternalFrame { private RandomAccessFile file; private JTextArea displayArea; private JButton closeButton; private JScrollPane scroller; private Record data; private int recordSize; public InventoryDialog( RandomAccessFile deleteFile, int size ) { super( "Inventory" ); recordSize = size; file = deleteFile; data = new Record(); displayArea = new JTextArea( 300, 200 ); displayArea.setEditable( false ); scroller = new JScrollPane( displayArea ); closeButton = new JButton( "Close" ); closeButton.addActionListener( new ActionListener() { // anonymous inner class public void actionPerformed( ActionEvent event ) { setVisible( false ); } } // end anonymous inner class ); // end call to addActionListener Container container = getContentPane(); container.setLayout( new BorderLayout() ); container.add( scroller, BorderLayout.CENTER ); container.add( closeButton, BorderLayout.SOUTH ); setSize( 350, 275 ); setVisible( false ); } // display tool inventory public void display() { displayArea.setText( "Record #\tTool Name\tQuantity\tCost\tTotalCost\n" ); try { for ( int i = 0; i < recordSize; i++ ) { file.seek( ( i ) * data.SIZE ); data.read( file ); if ( data.getRecordNumber() != 0 ) displayArea.append( data.getRecordNumber() + "\t" + data.getToolName() + "\t" + data.getQuantity() + "\t" + data.getCost() + "\t" + data.getTotalCost() + "\n" ); } } // error reading from file catch ( IOException ioException ) { JOptionPane.showMessageDialog( this, "Error during read from file", "Read Error", JOptionPane.ERROR_MESSAGE ); } } } // end class InventoryDialog