I am currently studying java and i am having trouble with program to simulate a bank account. I Have most of it working but I cannot figure out how to delete records from a Random-Access File. Any help would be appreciated. I have four programs written out - Record, CreateRandomFile, WriteRandomFile and ReadRandomFile.
// Record.java //Record class for the Random Access file program import java.io.*; public class Record { private int account; private String lastName; private String firstName; private double balance; private double overdraftlimit; //Read a record form the specified RandomAccount public void read(RandomAccessFile file)throws IOException { account = file.readInt(); char first []=new char [15]; for (int i=0; i<first.length; i++) first [i] = file.readChar(); firstName = new String (first); char last [] = new char[15]; for (int i = 0; i<last.length; i++) last [i]=file.readChar(); lastName = new String (last); balance = file.readDouble(); overdraftlimit = file.readDouble(); } //Write a record to the specified RandomAccessFile public void write (RandomAccessFile file) throws IOException { StringBuffer buf; file.writeInt(account); if (firstName != null) buf = new StringBuffer (firstName); else buf = new StringBuffer(15); buf.setLength(15); file.writeChars(buf.toString()); if (lastName !=null) buf = new StringBuffer(lastName); else buf=new StringBuffer (15); buf.setLength(15); file.writeChars(buf.toString()); file.writeDouble(balance); file.writeDouble(overdraftlimit); } public void setAccount (int a){account = a;} public int getAccount() {return account;} public void setFirstName(String f) {firstName =f; } public String getFirstName () {return firstName;} public void setLastName (String l) {lastName = l;} public String getLastName () {return lastName;} public void setBalance (double b){ balance =b;} public double getBalance () {return balance;} public void setOverdraftlimit(double o) {overdraftlimit =o;} public double getOverdraftlimit () {return overdraftlimit;} //Note: This method contains a hard coded value //for the size of a record of information public static int size () {return 80;} }
// 23.03.09 // Create Random Access file import java.io.*; public class CreateRandomFile { private Record blank; private RandomAccessFile file; public CreateRandomFile() { blank= new Record(); try { file = new RandomAccessFile ("credit.dat", "rw"); for (int i=0; i<100;i++) blank.write(file); } catch(IOException e) { System.err.println("File not opened properly\n"+ e.toString() ); System.exit(1); } } public static void main(String args[]) { CreateRandomFile accounts = new CreateRandomFile(); } }
import javax.swing.*; import java.io.*; import java.awt.*; import java.awt.event.*; public class WriteRandomFile extends Frame implements ActionListener { private JTextField accountField, firstNameField, lastNameField, balanceField, overdraftlimitField; private JButton enter, done; private RandomAccessFile output; private Record data; public WriteRandomFile() { super("Write to random access file"); data = new Record(); try { output = new RandomAccessFile("credit.dat","rw"); } catch ( IOException e ) { System.err.println(e.toString() ); System.exit(1); } setSize(350,150); setLayout(new GridLayout(6,2)); setLayout(new GridLayout(6,2)); add(new JLabel("Account Number")); accountField = new JTextField(); add(accountField); add(new JLabel("First Name")); firstNameField = new JTextField(20); add(firstNameField); add(new JLabel("Last Name")); lastNameField = new JTextField(20); add(lastNameField); add(new JLabel("Balance")); balanceField = new JTextField(20); add(balanceField); add(new JLabel("Overdraftlimit")); overdraftlimitField = new JTextField(20); add(overdraftlimitField); enter = new JButton("Enter"); enter.addActionListener(this); add(enter); done = new JButton("Done"); done.addActionListener(this); add(done); setVisible(true); } public void addRecord() { int accountNumber = 0; Double d, db; if ( ! accountField.getText().equals("")) { try{ accountNumber = Integer.parseInt(accountField.getText()); if ( accountNumber > 0 && accountNumber <= 1000) { data.setAccount(accountNumber); data.setFirstName(firstNameField.getText()); data.setLastName(lastNameField.getText()); d = new Double(balanceField.getText()); data.setBalance(d.doubleValue()); output.seek( (long) (accountNumber-1) * Record.size()); data.write(output); db = new Double(overdraftlimitField.getText()); data.setOverdraftlimit(db.doubleValue()); output.seek( (long) (accountNumber-1) * Record.size()); data.write(output); } accountField.setText(""); firstNameField.setText(""); lastNameField.setText(""); balanceField.setText(""); overdraftlimitField.setText(""); } catch (NumberFormatException nfe) { System.err.println( "You must enter an integer account number"); } catch ( IOException io){ System.err.println( "Error during write to file\n" + io.toString()); System.exit(1); } } } public void actionPerformed( ActionEvent e) { addRecord(); if (e.getSource() == done) { try { output.close(); } catch ( IOException io ) { System.err.println("File not closed properly\n" + io.toString()); } System.exit(0); } } public static void main(String args[]) { new WriteRandomFile(); } }
import javax.swing.*; import java.io.*; import java.awt.*; import java.awt.event.*; import java.text.DecimalFormat; public class ReadRandomFile extends Frame implements ActionListener { private JTextField accountField, firstNameField, lastNameField, balanceField, overdraftlimitField, withdrawField, lodgementField; private JButton next, done, withdraw, lodge, delete; private RandomAccessFile input; private Record data; private RandomAccessFile output; public ReadRandomFile() { super( "Read Client File"); try { input = new RandomAccessFile("credit.dat","r"); } catch (IOException e ) { System.err.println(e.toString() ); System.exit( 1); } try { output = new RandomAccessFile("credit.dat","rw"); } catch ( IOException e ) { System.err.println(e.toString() ); System.exit(1); } data = new Record(); setSize(350, 150); setLayout(new GridLayout(10,4)); add( new JLabel("Account Number") ); accountField = new JTextField(); accountField.setEditable(false); add(accountField); add(new JLabel("First Name") ); firstNameField = new JTextField(20); firstNameField.setEditable(false); add(firstNameField); add(new JLabel("last Name") ); lastNameField = new JTextField(20); lastNameField.setEditable(false); add(lastNameField); add(new JLabel("Balance") ); balanceField = new JTextField(20); balanceField.setEditable(false); add(balanceField); add(new JLabel("Overdraftlimit")); overdraftlimitField = new JTextField(20); overdraftlimitField.setEditable(false); add(overdraftlimitField); next = new JButton("Next"); next.addActionListener(this); add(next); done = new JButton("Done"); done.addActionListener(this); add(done); add(new JLabel("Withdraw")); withdrawField = new JTextField(20); withdrawField.setEditable(true); add(withdrawField); add(new JLabel("Lodgement")); lodgementField = new JTextField(20); lodgementField.setEditable(true); add(lodgementField); withdraw = new JButton("Withdraw"); withdraw.addActionListener( new ActionListener() // anonymous inner class { // event handler called when WithdrawalJButton is pressed public void actionPerformed( ActionEvent event ) { Withdrawal( event ); } } // end anonymous inner class ); // end call to addActionListener add(withdraw); lodge = new JButton("Lodge"); lodge.addActionListener( new ActionListener() // anonymous inner class { // event handler called when WithdrawalJButton is pressed public void actionPerformed( ActionEvent event ) { Lodgement( event ); } } // end anonymous inner class ); // end call to addActionListener add(lodge); setVisible(true); delete = new JButton("Delete Record"); delete.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event) { DeleteRecord(event); } } add(delete); } public void actionPerformed(ActionEvent e) { int accountNumber; if (e.getSource() == next) readRecord(); else closeFile(); } public void readRecord() { DecimalFormat twoDigits = new DecimalFormat("0.00"); try { do { data.read(input); } while ( data.getAccount() == 0); accountField.setText( String.valueOf( data.getAccount()) ); firstNameField.setText(data.getFirstName() ); lastNameField.setText(data.getLastName() ); balanceField.setText(String.valueOf( twoDigits.format(data.getBalance() ) ) ); overdraftlimitField.setText(String.valueOf( twoDigits.format(data.getOverdraftlimit() ) ) ); } catch ( EOFException eof) { closeFile(); } catch ( IOException e ) { System.err.println("error during read from file\n"+ e.toString() ); System.exit(1); } } private void closeFile() { try { input.close(); System.exit(0); } catch ( IOException e ) { System.err.println("Error closing file\n" + e.toString() ); System.exit(1); } } public static void main(String args[]) { new ReadRandomFile(); } private void Withdrawal( ActionEvent event ) { if (Double.parseDouble(withdrawField.getText()) > Double.parseDouble(balanceField.getText() ) ); balanceField.setText(String.valueOf(Double.parseDouble(balanceField.getText() ) - Double.parseDouble(withdrawField.getText() ) ) ); withdrawField .setText(""); int accountNumber = 0; Double d, db; { try{ accountNumber = Integer.parseInt(accountField.getText()); { data.setAccount(accountNumber); data.setFirstName(firstNameField.getText()); data.setLastName(lastNameField.getText()); d = new Double(balanceField.getText()); data.setBalance(d.doubleValue()); output.seek( (long) (accountNumber-1) * Record.size()); data.write(output); db = new Double(overdraftlimitField.getText()); data.setOverdraftlimit(db.doubleValue()); output.seek( (long) (accountNumber-1) * Record.size()); data.write(output); } } catch ( IOException io){ System.exit(1); } } } // end method SubmitJButtonActionPerformed private void Lodgement( ActionEvent event ) { balanceField.setText(String.valueOf(Double.parseDouble(lodgementField.getText() ) + Double.parseDouble(balanceField.getText() ) ) ); lodgementField .setText(""); int accountNumber = 0; Double d, db; { try{ accountNumber = Integer.parseInt(accountField.getText()); { data.setAccount(accountNumber); data.setFirstName(firstNameField.getText()); data.setLastName(lastNameField.getText()); d = new Double(balanceField.getText()); data.setBalance(d.doubleValue()); output.seek( (long) (accountNumber-1) * Record.size()); data.write(output); db = new Double(overdraftlimitField.getText()); data.setOverdraftlimit(db.doubleValue()); output.seek( (long) (accountNumber-1) * Record.size()); data.write(output); } } catch ( IOException io){ System.exit(1); } } } private void DeleteRecord(ActionEvent event) { } }