-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteRandFile.java
More file actions
107 lines (90 loc) · 3.12 KB
/
WriteRandFile.java
File metadata and controls
107 lines (90 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package Records_Combined;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class WriteRandFile extends JFrame {
JTextField acct, fName, lName, bal;
JButton enter, done;
JLabel acctLabel, fNameLabel, lNameLabel, balLabel;
RandomAccessFile output; // file for output
Record data;
public WriteRandFile() {
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 );
}
Container c = getContentPane();
c.setLayout( new GridLayout( 5, 2 ) );
acct = new JTextField( 20 );
acctLabel = new JLabel( "Account Number" );
fName = new JTextField( 20 );
fNameLabel = new JLabel( "First Name" );
lName = new JTextField( 20 );
lNameLabel = new JLabel( "Last Name" );
bal = new JTextField( 20 );
balLabel = new JLabel( "Balance" );
enter = new JButton( "Enter" );
done = new JButton( "Done" );
c.add( acctLabel ); // add label
c.add( acct ); // add TextField
c.add( fNameLabel ); // add label
c.add( fName ); // add TextField
c.add( lNameLabel ); // add label
c.add( lName ); // add TextField
c.add( balLabel ); // add label
c.add( bal ); // add TextField
c.add( enter ); // add button
c.add( done ); // add button
done.addActionListener( new ActionListener () {
public void actionPerformed( ActionEvent event ){
System.exit( 0 );
}
}
);
enter.addActionListener( new ActionListener () {
public void actionPerformed( ActionEvent event ){
addRecord();
} }
);
setSize( 300, 150 );
setVisible( true );
}
public static void main( String args[] ) {
WriteRandFile accounts = new WriteRandFile();
}
public void addRecord() {
int acctNum = 0;
acctNum = (new Integer(acct.getText())).intValue();
// output the values to the file
try {
if ( acctNum > 0 && acctNum <= 100 ) {
data.setAccount(acctNum);
data.setFirstName(fName.getText());
data.setLastName(lName.getText());
data.setBalance(Double.parseDouble(bal.getText()));
output.seek( (long) ( acctNum-1 ) * data.size() );
data.write( output );
// clear the TextFields
acct.setText( "" );
fName.setText( "" );
lName.setText( "" );
bal.setText( "" );
}
else {
acct.setText( "Enter valid account (1-100)" );
acct.selectAll();
}
}
catch ( IOException e ) {
System.err.println( "Error during write to file\n" +
e.toString() );
System.exit( 1 );
}
}
}