•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 392,047 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,270 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 623 | Replies: 1
![]() |
•
•
Join Date: Jun 2008
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
Hey... I present myself.. Youssef, 17 years old and i'm currently on my second year of the IB Program ... Computer IB HL i took. As some of you might know our IA = Internal Assessement is a Java Database and now mine is due wednesday... [PART I] and i need your guys help... I have a skeleton... you create a random acces file, and open it in a notepad.. then u run my program.. Team (only team needs to work for now) you can ADD a team ...click next next.. then you can click View team... (yet there there is a problem it only displays the first record tehn displays the message "No More Records" knowing that i have inputed more records iny my Add.. I would be eternally grateful if some one could help me by tonight plzzzzzzzzzz ..... so i could show my teacher tomorow.. If someone could fix me the View (READ RANDOM FILE) error.... and then have me a script thats working for EDIT & DELETE... cuz they're not in the book and i have no clue how to open then delete my record
... Delete is due tomorow, and the rest wednesday... i know all this for tonight is alot but some of few are experts and could do this in a 20-30 min ... so if some one could save me by correcting my VIEW, and writing me a script for EDIT/DELETE that would really help... if you want just give me an example of delete.. and ill try adapting it with my variables... anw... plz guyz help would be nice... Here are my codes..
... Delete is due tomorow, and the rest wednesday... i know all this for tonight is alot but some of few are experts and could do this in a 20-30 min ... so if some one could save me by correcting my VIEW, and writing me a script for EDIT/DELETE that would really help... if you want just give me an example of delete.. and ill try adapting it with my variables... anw... plz guyz help would be nice... Here are my codes.. Java Syntax (Toggle Plain Text)
//BASKETBALL SKELETON// package Basketball; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Basketball extends JFrame { private final Color colorValues[] = { Color.black, Color.blue, Color.red, Color.green }; private JRadioButtonMenuItem colorItems[], fonts[]; private JCheckBoxMenuItem styleItems[]; private JLabel displayLabel; private ButtonGroup fontGroup, colorGroup; private int style; // set up GUI public Basketball() { super( "BBGS SOFTWARE" ); // set up team menu and its menu items JMenu teamMenu = new JMenu( "Team" ); // set up Add team menu item JMenuItem addTeam = new JMenuItem( "Add" ); teamMenu.add( addTeam ); addTeam.addActionListener( new ActionListener() { // anonymous inner class public void actionPerformed( ActionEvent event ) { new WriteRF(); } } ); JMenuItem editTeam = new JMenuItem( "Edit Team" ); teamMenu.add( editTeam ); editTeam.addActionListener( new ActionListener() { // anonymous inner class // display message dialog when user selects About... public void actionPerformed( ActionEvent event ) { } } // end anonymous inner class ); // end call to addActionListener JMenuItem deleteTeam = new JMenuItem( "Delete Team" ); teamMenu.add( deleteTeam ); deleteTeam.addActionListener( new ActionListener() { // anonymous inner class // display message dialog when user selects About... public void actionPerformed( ActionEvent event ) { } } // end anonymous inner class ); // end call to addActionListener // set up List team menu item JMenuItem listTeam = new JMenuItem( "View Teams" ); teamMenu.add( listTeam ); listTeam.addActionListener( new ActionListener() { // anonymous inner class // display message dialog when user selects About... public void actionPerformed( ActionEvent event ) { new ReadRF(); } } // end anonymous inner class ); // end call to addActionListener // create menu bar and attach it to Basketball window JMenuBar bar = new JMenuBar(); setJMenuBar( bar ); bar.add( teamMenu ); // set up teamship menu and its menu items JMenu teamshipMenu = new JMenu( "Game" ); // set up Add team menu item JMenuItem addGame = new JMenuItem( "Add Game" ); teamshipMenu.add( addGame ); addGame.addActionListener( new ActionListener() { // anonymous inner class // display message dialog when user selects About... public void actionPerformed( ActionEvent event ) { JOptionPane.showMessageDialog( Basketball.this, "Add a Game", "Adding", JOptionPane.PLAIN_MESSAGE ); } } ); JMenuItem editGame = new JMenuItem( "Edit/Delete Game" ); teamshipMenu.add( editGame ); editGame.addActionListener( new ActionListener() { // anonymous inner class // display message dialog when user selects About... public void actionPerformed( ActionEvent event ) { JOptionPane.showMessageDialog( Basketball.this, "Edit a Game", "Editing", JOptionPane.PLAIN_MESSAGE ); } } // end anonymous inner class ); // end call to addActionListener // set up List team menu item JMenuItem listGame = new JMenuItem( "List Games" ); teamshipMenu.add( listGame ); listGame.addActionListener( new ActionListener() { // anonymous inner class // display message dialog when user selects About... public void actionPerformed( ActionEvent event ) { JOptionPane.showMessageDialog( Basketball.this, "Lisiting Games", "View", JOptionPane.PLAIN_MESSAGE ); } } // end anonymous inner class ); // end call to addActionListener // create menu bar and attach it to Basketball window bar.add( teamshipMenu ); // set up Exit menu item JMenu exitMenu = new JMenu( "Exit" ); exitMenu.addActionListener( new ActionListener() { // anonymous inner class // terminate application when user clicks exitItem public void actionPerformed( ActionEvent event ) { System.exit( 0 ); } } // end anonymous inner class ); // end call to addActionListener bar.add( exitMenu ); displayLabel = new JLabel( "Welcome", SwingConstants.CENTER ); displayLabel.setForeground( colorValues[ 0 ] ); displayLabel.setFont( new Font( "Serif", Font.PLAIN, 52 ) ); //getContentPane().setBackground( Color.CYAN ); getContentPane().add( displayLabel, BorderLayout.PAGE_END ); setSize(800, 600 ); setLocation(200, 240); setVisible( true ); } public static void main( String args[] ) { Basketball application = new Basketball(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } } // end class Basketballs
Java Syntax (Toggle Plain Text)
//WRITE RANDOM FILE package Basketball; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; public class WriteRF extends JFrame { private RandomAccessFile output; private GUI userInterface; private JButton enterButton, openButton; private static final int NUMBER_RECORDS = 100; // set up GUI public WriteRF() { super( "Write to random access file" ); // create instance of reusable user interface BankUI userInterface = new GUI( 2 ); // four textfields getContentPane().add( userInterface, BorderLayout.CENTER ); openButton = userInterface.getDoTask1Button(); openButton.setText( "Get File..." ); // register listener to call openFile when button pressed openButton.addActionListener( // anonymous inner class to handle openButton event new ActionListener() { // allow user to select file to open public void actionPerformed( ActionEvent event ) { openFile(); } } // end anonymous inner class ); // end call to addActionListener // register window listener for window closing event addWindowListener( // anonymous inner class to handle windowClosing event new WindowAdapter() { // add record in GUI, then close file public void windowClosing( WindowEvent event ) { if ( output != null ) addRecord(); closeFile(); } } // end anonymous inner class ); // end call to addWindowListener // get reference to generic task button doTask2 in BankUI enterButton = userInterface.getDoTask2Button(); enterButton.setText( "Next" ); enterButton.setEnabled( false ); // register listener to call addRecord when button pressed enterButton.addActionListener( // anonymous inner class to handle enterButton event new ActionListener() { // add record to file public void actionPerformed( ActionEvent event ) { addRecord(); } } // end anonymous inner class ); // end call to addActionListener setSize( 300, 150 ); setVisible( true ); } // enable user to choose file to open private void openFile() { // display file dialog so user can select file JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY ); int result = fileChooser.showOpenDialog( this ); // if user clicked Cancel button on dialog, return if ( result == JFileChooser.CANCEL_OPTION ) return; // obtain selected file File fileName = fileChooser.getSelectedFile(); // display error if file name invalid if ( fileName == null || fileName.getName().equals( "" ) ) JOptionPane.showMessageDialog( this, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE ); else { // open file try { output = new RandomAccessFile( fileName, "rw" ); enterButton.setEnabled( true ); openButton.setEnabled( false ); } // process exception while opening file catch ( IOException ioException ) { JOptionPane.showMessageDialog( this, "File does not exist", "Invalid File Name", JOptionPane.ERROR_MESSAGE ); } } // end else } // end method openFile // close file and terminate application private void closeFile() { // close file and exit try { if ( output != null ) output.close(); System.exit( 0 ); } // process exception while closing file catch( IOException ioException ) { JOptionPane.showMessageDialog( this, "Error closing file", "Error", JOptionPane.ERROR_MESSAGE ); System.exit( 1 ); } } // end method closeFile // add one record to file private void addRecord() { int teamidnumber = 0; String fields[] = userInterface.getFieldValues(); RaRecord record = new RaRecord(); // ensure account field has a value if ( ! fields[ GUI.TEAMID ].equals( "" ) ) { // output values to file try { teamidnumber = Integer.parseInt( fields[ GUI.TEAMID ] ); if ( teamidnumber > 0 && teamidnumber <= NUMBER_RECORDS ) { record.setTeamid( teamidnumber ); record.setTeam( fields[ GUI.TEAM ] ); output.seek( ( teamidnumber - 1 ) * RaRecord.SIZE ); record.write( output ); } else { JOptionPane.showMessageDialog( this, "Team Id must be between 0 and 100", "ID number Invalid", JOptionPane.ERROR_MESSAGE ); } userInterface.clearFields(); // clear TextFields } // end try // process improper account number or balance format catch ( NumberFormatException formatException ) { JOptionPane.showMessageDialog( this, "Invalid ID", "Invalid Number Format", JOptionPane.ERROR_MESSAGE ); } // process exceptions while writing to file catch ( IOException ioException ) { JOptionPane.showMessageDialog( this, "Error writing to the file", "IO Exception", JOptionPane.ERROR_MESSAGE ); closeFile(); } } } } // END WRITE RANDOM FILE
Java Syntax (Toggle Plain Text)
//READ RANDOM FILE// package Basketball; import java.awt.*; import java.awt.event.*; import java.io.*; import java.text.DecimalFormat; import javax.swing.*; public class ReadRF extends JFrame { private GUI userInterface; private RandomAccessFile input; private JButton nextButton, openButton; // set up GUI public ReadRF() { super( "View Team List" ); // create reusable user interface instance userInterface = new GUI( 2 ); getContentPane().add( userInterface ); // configure generic doTask1 button from BankUI openButton = userInterface.getDoTask1Button(); openButton.setText( "View Files for Listing..." ); // register listener to call openFile when button pressed openButton.addActionListener( // anonymous inner class to handle openButton event new ActionListener() { // enable user to select file to open public void actionPerformed( ActionEvent event ) { openFile(); } } // end anonymous inner class ); // end call to addActionListener // configure generic doTask2 button from BankUI nextButton = userInterface.getDoTask2Button(); nextButton.setText( "Next" ); nextButton.setEnabled( false ); // register listener to call readRecord when button pressed nextButton.addActionListener( // anonymous inner class to handle nextButton event new ActionListener() { // read a record when user clicks nextButton public void actionPerformed( ActionEvent event ) { readRecord(); } } // end anonymous inner class ); // end call to addActionListener // register listener for window closing event addWindowListener( // anonymous inner class to handle windowClosing event new WindowAdapter() { // close file and terminate application public void windowClosing( WindowEvent event ) { closeFile(); } } // end anonymous inner class ); // end call to addWindowListener setSize( 300, 150 ); setVisible( true ); } // end constructor // enable user to select file to open private void openFile() { // display file dialog so user can select file JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY ); int result = fileChooser.showOpenDialog( this ); // if user clicked Cancel button on dialog, return if ( result == JFileChooser.CANCEL_OPTION ) return; // obtain selected file File fileName = fileChooser.getSelectedFile(); // display error is file name invalid if ( fileName == null || fileName.getName().equals( "" ) ) JOptionPane.showMessageDialog( this, "Invalid File Name", "Invalid File Name", JOptionPane.ERROR_MESSAGE ); else { // open file try { input = new RandomAccessFile( fileName, "r" ); nextButton.setEnabled( true ); openButton.setEnabled( false ); } // catch exception while opening file catch ( IOException ioException ) { JOptionPane.showMessageDialog( this, "File does not exist", "Invalid File Name", JOptionPane.ERROR_MESSAGE ); } } // end else } // end method openFile // read one record private void readRecord() { RaRecord record = new RaRecord(); // read a record and display try { do { record.read( input ); } while ( record.getTeamid() == 0 ); String values[] = { String.valueOf( record.getTeamid() ), record.getTeam()}; userInterface.setFieldValues( values ); } // close file when end-of-file reached catch ( EOFException eofException ) { JOptionPane.showMessageDialog( this, "No more records", "End-of-file reached", JOptionPane.INFORMATION_MESSAGE ); closeFile(); } // process exceptions from problem with file catch ( IOException ioException ) { JOptionPane.showMessageDialog( this, "Error Reading File", "Error", JOptionPane.ERROR_MESSAGE ); System.exit( 1 ); } } // end method readRecord // close file and terminate application private void closeFile() { // close file and exit try { if ( input != null ) input.close(); System.exit( 0 ); } // process exception closing file catch( IOException ioException ) { JOptionPane.showMessageDialog( this, "Error closing file", "Error", JOptionPane.ERROR_MESSAGE ); System.exit( 1 ); } } // end method closeFile } //END READ RANDOM FILE
Java Syntax (Toggle Plain Text)
//GUI// package Basketball; import java.awt.*; import javax.swing.*; public class GUI extends JPanel { // label text for GUI protected final static String names[] = { "Team ID", "Team" }; // GUI components; protected for future subclass access protected JLabel labels[]; protected JTextField fields[]; protected JButton doTask1, doTask2; protected JPanel innerPanelCenter, innerPanelSouth; protected int size; // number of text fields in GUI // constants representing text fields in GUI public static final int TEAMID = 0, TEAM = 1; public GUI( int mySize ) { size = mySize; labels = new JLabel[ size ]; fields = new JTextField[ size ]; // create labels for ( int count = 0; count < labels.length; count++ ) labels[ count ] = new JLabel( names[ count ] ); // create text fields for ( int count = 0; count < fields.length; count++ ) fields[ count ] = new JTextField(); // create panel to lay out labels and fields innerPanelCenter = new JPanel(); innerPanelCenter.setLayout( new GridLayout( size, 2 ) ); // attach labels and fields to innerPanelCenter for ( int count = 0; count < size; count++ ) { innerPanelCenter.add( labels[ count ] ); innerPanelCenter.add( fields[ count ] ); } // create generic buttons; no labels or event handlers doTask1 = new JButton(); doTask2 = new JButton(); // create panel to lay out buttons and attach buttons innerPanelSouth = new JPanel(); innerPanelSouth.add( doTask1 ); innerPanelSouth.add( doTask2 ); // set layout of this container and attach panels to it setLayout( new BorderLayout() ); add( innerPanelCenter, BorderLayout.CENTER ); add( innerPanelSouth, BorderLayout.SOUTH ); validate(); // validate layout } // end constructor // return reference to generic task button doTask1 public JButton getDoTask1Button() { return doTask1; } // return reference to generic task button doTask2 public JButton getDoTask2Button() { return doTask2; } // return reference to fields array of JTextFields public JTextField[] getFields() { return fields; } // clear content of text fields public void clearFields() { for ( int count = 0; count < size; count++ ) fields[ count ].setText( "" ); } // set text field values; throw IllegalArgumentException if // incorrect number of Strings in argument public void setFieldValues( String strings[] ) throws IllegalArgumentException { if ( strings.length != size ) throw new IllegalArgumentException( "There must be " + size + " Strings in the array" ); for ( int count = 0; count < size; count++ ) fields[ count ].setText( strings[ count ] ); } // get array of Strings with current text field contents public String[] getFieldValues() { String values[] = new String[ size ]; for ( int count = 0; count < size; count++ ) values[ count ] = fields[ count ].getText(); return values; } } // END OF GUI
