import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class tcpServer extends JFrame implements ActionListener {
private int port = 5050 , trdCnt = 0 ;
private double sleepTime = 0 ;
private boolean debug = true , loopCTL = true , loopCTL2 = true ;
private boolean foundRec = true ;
private String messageTokens[] = new String[ 16 ] ,
addrRecord[][] = {
{"Compaq Presario CQ56-111SA", "Laptop", "12", "349.99"},
{"Asus X52F-EX469V", "Laptop", "18", "329.99"},
{"Sony Vaio EA3S1E", "Laptop", "5", "589.99"},
{"Samsung SyncMaster P2450H", "Monitor", "33", "179.99"},
{"Philips Brilliance C-line 226C2SB", "Monitor", "28", "109.99"},
{"Dell Professional P2411H", "Monitor", "26", "214.99"},
{"Kingston 64GB SSDNOW V100", "SSD", "4", "79.99"},
{"Corsair 60GB Force", "SSD", "13", "89.99"},
{"OCZ 960GB IBIS", "SSD", "1", "2099.99"},
{"Antec Dark Fleet DF-85", "Case", "21", "120.00"},
{"Antec One Hundred", "Case", "44", "44.99"},
{"MSI nVidia GTX580", "Graphics Card", "9", "329.99"},
{"nVidia Geforce 3D-Vision Kit", "PC Accessories", "5", "119.99"}};
private ServerSocket server_socket;
//private BufferedReader input;
private PrintWriter output;
private Container c ;
private JTextArea display ;
private JButton cancel , send, exit;
private JPanel buttonPanel ;
private StringTokenizer tokens ;
private Record data ;
private File aFile ;
private RandomAccessFile file ;
private static String pData[] [] = new String [ 300 ] [ 9 ];
private double loopLimit = 0;
private Thread thrd[] ;
public tcpServer() {
super ( "Computer Hardware ltd - SERVERSIDE APPLICATION") ;
setupThreads() ;
setup() ;
RunServer() ;
}
public void setThreadcount( int a ) {
trdCnt = a ;
}
public void setupThreads() {
thrd = new Thread[ 15 ] ;
}
public void setup() {
c = getContentPane();
/** create an address record object */
data = new Record() ;
SetupMenu() ;
SetupToolbar() ;
SetupPanels() ;
SetupArrays() ;
SetupButtons() ;
SetUpTextArea() ;
InitRecord() ;
setSize( 400, 400 );
setLocation( 10, 20 ) ;
show();
}
public void SetupMenu() {
}
public void SetupToolbar() {
}
public void SetupPanels() {
buttonPanel = new JPanel() ;
c.add( buttonPanel , BorderLayout.SOUTH) ;
}
public void SetupArrays() {
}
public void SetUpTextArea() {
display = new JTextArea();
display.setEditable( false );
addWindowListener( new WindowHandler( this ) );
c.add( new JScrollPane( display ),
BorderLayout.CENTER );
}
public void SetupButtons() {
exit = new JButton( "Exit" );
exit.setBackground( Color.gray ) ;
exit.setForeground( Color.white ) ;
buttonPanel.add( exit ) ;
exit.addActionListener( this );
}
public void InitRecord() {
aFile = new File( "stock.dat" ) ;
try {
if ( !aFile.exists() ) {
file = new RandomAccessFile( "stock.dat", "rw" );
for ( int ii = 0 ; ii < 300 ; ii++ ) {
if (ii < 13 ) {
data.setRecID( ii ) ;
data.setItemName( addrRecord[ ii ][ 0 ] ) ;
data.setDeviceType( addrRecord[ ii ][ 1 ] ) ;
data.setCurrentStock( addrRecord[ ii ][ 2 ] ) ;
data.setPrice( addrRecord[ ii ][ 3 ] ) ;
}
else {
data.setRecID( ii ) ;
data.setItemName( " " ) ;
data.setDeviceType( " " ) ;
data.setCurrentStock( " " ) ;
data.setPrice( " " ) ;
}
file.seek( ii * data.getSize() );
data.write( file );
display.append("\nRecords have been written : " +
data.getRecID() + " " +
data.getItemName() + " " +
data.getDeviceType() + " " +
data.getCurrentStock() + " " +
data.getPrice() ) ;
}
}
else {
file = new RandomAccessFile( "stock.dat", "rw" );
}
file.close();
}
catch ( IOException e ) {
System.err.println( e.toString() );
System.exit( 1 );
}
}
public void RunServer() {
int i3 = 0 ;
try {
server_socket = new ServerSocket( 5050, 100,
InetAddress.getLocalHost());
display.setText("=======================================================\n" );
display.append (" Computer Hardware ltd - SERVERSIDE APPLICATION" );
display.append ("\n=======================================================\n");
display.append("\nHosted on : " + InetAddress.getLocalHost());
display.append(" \nPort : " + server_socket.getLocalPort());
while( loopCTL ) {
Socket socket = server_socket.accept();
display.append("\n\n=======================================================\n" );
display.append (" STARTING THREAD #" + (trdCnt+1) + "..." );
display.append ("\n=======================================================\n");
display.append("\nClient connected : " + socket.getInetAddress());
display.append("\nPort : " + socket.getPort() + "\n\n");
try {
MyThread request =
new MyThread( this , socket , trdCnt );
thrd[ trdCnt ] = request ;
thrd[ trdCnt ].start() ;
trdCnt++ ;
}
catch(Exception e) {
sysPrint( "" + e);
}
}
}
catch (IOException e) {
display.append("\n" + e);
}
}
public void actionPerformed( ActionEvent e ) {
if ( e.getSource() == exit )
sysExit( 0 ) ;
}
public void closeConnection() {
try {
server_socket.close();
}
catch (IOException e) {
display.append("\n" + e);
}
}
public void sysExit( int ext ) {
loopCTL = false ;
loopCTL2 = false ;
int thrdLen = thrd.length ;
closeConnection() ;
for( int iii = 0 ; iii < thrdLen ; iii++ ) {
thrd[ iii ] = null ;
}
System.exit( ext ) ;
}
public void sysPrint( String str ) {
if( debug ) {
System.out.println("" + str ) ;
}
}
public static void main(String args[]) {
final tcpServer server = new tcpServer() ;
server.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
server.closeConnection() ;
System.exit( 0 );
}
}
);
}
class WindowHandler extends WindowAdapter {
tcpServer tcpS;
public WindowHandler( tcpServer t ) { tcpS = t; }
public void windowClosing( WindowEvent e ) { tcpS.sysExit( 0 ); }
}
public class MyThread extends Thread {
private Socket socket;
private BufferedReader input2;
private PrintWriter output2;
private PrintWriter outp ;
private int trdCnt , ar , numEntries = 0 ;
private tcpServer tcpS ;
private String getStock = "/servlet/stock" ;
/** *********************************************************
* The purpose of the MyThread() constructor is to used the
* passed parameters to initialize MyThread class level
* variables.
**************************************************************/
public MyThread( tcpServer tps , Socket soc_ket , int trd_Cnt ) throws Exception {
trdCnt = trd_Cnt ;
tcpS = tps ;
socket = soc_ket;
this.input2 = new BufferedReader(new InputStreamReader(soc_ket.getInputStream()));
this.output2 = new PrintWriter(soc_ket.getOutputStream(),true);
}
/** *********************************************************
* The run() method responds to the client's request.
**************************************************************/
public void run() {
int IndexOfEquals = 0 , i3 = 0 , ii = 0 , recLength = addrRecord.length ;
String strName = "" ;
sysPrint( "STARTING THREAD #" + (trdCnt+1) + "..." ) ;
String message = "" ;
// print received data
try { // try #1
while( !message.toUpperCase().equals( "QUIT" )) {
message = (String)input2.readLine();
tokens = new StringTokenizer( message ) ;
if ( tokens.countTokens() >= 1 ) {
int iii = 0 ;
while( tokens.hasMoreTokens() ) {
messageTokens[ iii ] = tokens.nextToken().toString() ;
display.append("\n\nTHREAD #" + (trdCnt+1) + " - MESSAGE : " + messageTokens[ iii ] ) ;
iii++ ;
}
display.append("\nTHREAD #" + (trdCnt+1) + " - MESSAGE : " + messageTokens[ 0 ] + "\n") ;
sysPrint( "THREAD #" + (trdCnt+1) + " - MESSAGE : " + messageTokens[ 0 ] ) ;
sysPrint( "THREAD #" + (trdCnt+1) + " - MESSAGE : " + messageTokens[ 1 ] ) ;
if( messageTokens[ 1 ].equals( getStock ) ) {
sysPrint( "THREAD #" + (trdCnt+1) + " - SERVLET" ) ;
String servName = "http://192.168.100.105:5050/servlet/stock" ;
// create and send HTML page to client
StringBuffer buf = new StringBuffer();
buf.append( "<HTML><HEAD><TITLE>" );
buf.append( "Computer Hard ltd - SERVLET ACCESS" );
buf.append( "</TITLE></HEAD><BODY>\n" );
buf.append( "<FORM METHOD=\"GET\" ACTION=GET" );
buf.append( "<p><b><font size=5>Computer Hardware ltd - SERVLET ACCESS</p>" );
buf.append( "<p><b><font size=5>ITEM BROWSER</p></b>" );
buf.append( "<p>Select an item from the dropdown menu... <br><br> <select name=\"getaName\">" );
listNames( 0 ) ;
//ar = pData.length ;
for ( ii = 0 ; ii < numEntries+1 ; ii++ ) {
buf.append( "<option value=\"" + pData[ ii ][ 2 ] + "\">" +
" " + pData[ ii ][ 2 ] + "</option>" ) ;
sysPrint("<option value=\"" + pData[ ii ][ 1 ] + "\">" +
pData[ ii ][ 2 ] + "</option>" ) ;
sysPrint( "Thread run() 6: The value of ii is " + ii ) ;
}
buf.append( "</select><br><br><br>" ) ;
buf.append( "<p>Press the button to retrieve information on an item... <br><br><input type=\"submit\" value=\"Get Name\">" ) ;
buf.append( "</FORM>" ) ;
buf.append( "</font></BODY></HTML>" );
output2.println( buf.toString() );
}
else if ( messageTokens[ 0 ].toUpperCase().equals( "GET" ) ) {
sysPrint("Thread" + (trdCnt+1) + " run() 8b: messageTokens[ 0 ] equals GET." ) ;
sysPrint("Thread" + (trdCnt+1) + " run() 8c: messageTokens[ 1 ] is " + messageTokens[ 1 ] ) ;
if ( messageTokens[ 1 ].substring(0 , 4).equals( "/ser" ) ) {
IndexOfEquals = messageTokens[ 1 ].lastIndexOf('=') ;
strName = "" + messageTokens[ 1 ].substring( IndexOfEquals+1 ,
messageTokens[ 1 ].length() ) ;
sysPrint("Thread" + (trdCnt+1) + " run() 8d: " + strName + " IndexOfEquals " + IndexOfEquals) ;
if ( IndexOfEquals > 0 ) {
sysPrint("Thread" + (trdCnt+1) + " run() 9: " + strName ) ;
ii = 0 ;
StringBuffer buf = new StringBuffer();
listNames( 0 ) ;
/** **************************************************
* The while logic below has to be changed to look in
* the randomaccessfile for the name
*****************************************************/
while ( ii < recLength ) {
if ( pData[ ii ][ 2 ].toUpperCase().equals( strName.toUpperCase() ) ) {
sysPrint("Thread" + (trdCnt+1) + " run() 8cc: Found record " + strName ) ;
display.append("\n" + "RecordFound" ) ;
buf.append( "<HTML><HEAD><TITLE>" );
buf.append( "Computer Hardware ltd - SERVLET ACCESS - ITEM DETAILS" );
buf.append( "</TITLE></HEAD><BODY>\n" );
buf.append( "" + pData[ ii ][ 1 ] + "<br>" ) ;
buf.append( "" + pData[ ii ][ 2 ] + "<br>" ) ;
buf.append( "" + pData[ ii ][ 3 ] + "<br>" ) ;
buf.append( "" + pData[ ii ][ 4 ] + "<br>" ) ;
buf.append( "</BODY></HTML>" );
output2.println( buf.toString() );
sysPrint("Thread" + (trdCnt+1) + " run() 8cd: Found record " + buf.toString() ) ;
ii = recLength = 10 ;
break ;
}
ii++ ;
} // End of while loop
} // End of outer if
}
}
else if ( messageTokens[ 0 ].toUpperCase().equals( "FIND" ) ) {
; //findName() ;
}
else if ( messageTokens[ 0 ].toUpperCase().equals( "LISTALL" )
|| messageTokens[ 0 ].toUpperCase().equals( "REFRESH" ) ) {
synchronized( MyThread.this ) {
listNames( 1 ) ;
MyThread.this.yield() ;
} // End of synchronized block
}
else if ( messageTokens[ 0 ].toUpperCase().equals( "ADD;;" ) ) {
synchronized( MyThread.this ) {
addName( message ) ;
MyThread.this.yield() ;
} // End of synchronized block
}
else if ( messageTokens[ 0 ].toUpperCase().equals( "UPDATE;;" ) ) {
synchronized( MyThread.this ) {
updateName( message ) ;
MyThread.this.yield() ;
} // End of synchronized block
}
else if ( messageTokens[ 0 ].toUpperCase().equals( "DELETE;;" ) ) {
synchronized( MyThread.this ) {
deleteName() ;
MyThread.this.yield() ;
} // End of synchronized block
}
iii = 0 ;
}
else {
display.append( message );
//message = null; //so the loop will terminate the server
break;
} /** End of if-else */
} /** End of while */
//sendData ( "FROM SERVER==> QUIT" ) ;
} // End of try #1
catch (IOException e) {
display.append("\n" + e);
} // End of try/catch #2
// connection closed by client
try { // try #2
socket.close();
display.append("\n=======================================================\n" );
display.append (" THREAD #" + (trdCnt+1) + " CLOSED..." );
display.append ("\n=======================================================\n");
thrd[ trdCnt ] = null ;
}
catch (IOException e) {
display.append("\n" + e);
} // End of try/catch #2
}
/***************** End of run() method *****************************/
/** *****************************************************************************
* The addName() this method adds a name using the RandomAccessFile API and
* the record ID.
***************************************************************************** */
public void addName( String message ) {
int ii = 0 ;
tokens = new StringTokenizer( message, ";;" ) ;
if ( tokens.countTokens() >= 1 ) {
while( tokens.hasMoreTokens() ) {
messageTokens[ ii ] = tokens.nextToken().trim().toString() ;
display.append("\n" + messageTokens[ ii ] ) ;
ii++ ;
}
}
display.append( "\nThe size of the recID is " +
messageTokens[ 1 ].length() );
data.setRecID( Integer.parseInt( messageTokens[ 1 ].trim() ) ) ;
data.setItemName( messageTokens[ 2 ] ) ;
data.setDeviceType( messageTokens[ 3 ] ) ;
data.setCurrentStock( messageTokens[ 4 ] ) ;
data.setPrice( messageTokens[ 5 ] ) ;
try {
file = new RandomAccessFile( "stock.dat", "rw" );
file.seek( 0 * data.getSize() );
file.seek( Integer.parseInt( messageTokens[ 1 ] ) * data.getSize() );
data.write( file );
file.close();
}
catch (IOException e) {
display.append("\n" + e);
}
display.append( "\nThread #" + (trdCnt+1) + " The item to be updated is : " +
data.getRecID() + " " +
data.getItemName( ) + " " +
data.getDeviceType( ) + " " +
data.getCurrentStock( ) + " " +
data.getPrice( )) ;
}
/** *****************************************************************************
* The listNames() method is used to gather all of the records in the address.dat
* file and send this data back to the client.
***************************************************************************** */
public void listNames( int reason ) {
int ii = 0 , recLength = addrRecord.length, iii = 0 ;
data = new Record();
String str = "" ;
foundRec = false ;
try {
/** Divide the length of the file by the record size to
* determine the number of records in the file
*/
file = new RandomAccessFile( "stock.dat", "rw" );
loopLimit = file.length() / data.getSize() ;
display.append( "\nLoopLimit = " + loopLimit ) ;
if( reason == 1 ) {
sendData ( "ListAll;;" ) ;
sysPrint("Thread" + (trdCnt+1) + "ListAll;; message sent") ;
}
ii = 0 ;
while ( ii < ( (int)loopLimit ) ) {
//file.seek( ( 0 ) * data.getSize() );
file.seek( ( ii ) * data.getSize() );
data.ReadRec( file );
if ( ( data.getRecID() >= 0 ) && ( data.getRecID() < 300 )
&& !data.getItemName().equals( "" ) ) {
display.append( "\n" + data.getRecID() + " " +
data.getItemName().trim() + " " +
data.getDeviceType().trim() + " " +
data.getCurrentStock().trim() + " " +
data.getPrice().trim()) ;
pData[ iii ] [ 0 ] = String.valueOf( data.getRecID() ) ;
pData[ iii ] [ 1 ] = data.getItemName().trim() ;
pData[ iii ] [ 2 ] = data.getDeviceType().trim() ;
pData[ iii ] [ 3 ] = data.getCurrentStock().trim() ;
pData[ iii ] [ 4 ] = data.getPrice().trim() ;
str = "" + ( pData[ iii ] [ 0 ]).trim()
+ ";; " + ( pData[ iii ] [ 1 ]).trim()
+ ";; " + ( pData[ iii ] [ 2 ]).trim()
+ ";; " + ( pData[ iii ] [ 3 ]).trim()
+ ";; " + ( pData[ iii ] [ 4 ]).trim()
+ ";; " + ";;;";
if( reason == 1 ) {
sendData( str) ;
sysPrint("Thread" + (trdCnt+1) + " record sent was " + str ) ;
}
iii++ ;
}
ii++ ;
}
numEntries = iii - 1 ;
if( reason == 1 ) {
sendData ( "ListAllDone;;Thread" + (trdCnt+1) + ";;" ) ;
sendData ( "FILELENGTH;;" + (numEntries+1) + ";;") ;
sysPrint("Thread" + (trdCnt+1) + " ListAllDone;; sent. " ) ;
}
file.close() ;
}
catch ( IOException ex ) {
//part.setText( "Error reading file" );
}
if( reason == 1 ) {
sendData( "GetAllDone;;Thread" + (trdCnt+1) + ";;" ) ;
}
}
/** ************************************************************
* The sendData() method sends record data back to the client
***************************************************************/
private void sendData( String str ) {
if (str != null ) {
output2.println( str );
}
else {
output2.println( " " ) ; // don't send nulls to client
sysPrint( "/********************************************************" ) ;
sysPrint( " * str =s Null . " ) ;
sysPrint( "/********************************************************" ) ;
}
//output2.flush() ;
}
/** *****************************************************************************
* The updateName() method updates a name using the RandomAccessFile API and
* the record ID.
***************************************************************************** */
public void updateName( String message ) {
int ii = 0 ;
display.append( "\nhread" + (trdCnt+1) + " Currrently in updateName() method." ) ;
tokens = new StringTokenizer( message, ";;" ) ;
if ( tokens.countTokens() >= 1 ) {
while( tokens.hasMoreTokens() ) {
messageTokens[ ii ] = tokens.nextToken().trim().toString() ;
display.append("\n" + messageTokens[ ii ] ) ;
ii++ ;
}
}
display.append( "\nThe size of the recID is " +
messageTokens[ 1 ].length() );
data.setRecID( Integer.parseInt( messageTokens[ 1 ].trim() ) ) ;
data.setItemName( messageTokens[ 2 ] ) ;
data.setDeviceType( messageTokens[ 3 ] ) ;
data.setCurrentStock( messageTokens[ 4 ] ) ;
data.setPrice( messageTokens[ 5 ] ) ;
try {
file = new RandomAccessFile( "stock.dat", "rw" );
file.seek( 0 * data.getSize() );
file.seek( Integer.parseInt( messageTokens[ 1 ] ) * data.getSize() );
data.write( file );
file.close();
}
catch (IOException e) {
display.append("\n" + e);
}
display.append( "\nhread" + (trdCnt+1) + " The record to be updated is " +
data.getRecID() + " " +
data.getItemName( ) + " " +
data.getDeviceType( ) + " " +
data.getCurrentStock( ) + " " +
data.getPrice( )) ;
}
/** *****************************************************************************
* The method deleteName() will use the record ID and the RandomAccessFile
* API to delete a record in the file.
***************************************************************************** */
public void deleteName() {
int ii = 0 , recLength = addrRecord.length ;
display.append( "\nThread" + (trdCnt+1) + " Currrently in deleteName() method." ) ;
data.setItemName( " " ) ;
data.setDeviceType( " " ) ;
data.setCurrentStock( " " ) ;
data.setPrice( " " ) ;
ii = messageTokens[ 1 ].indexOf(";") ;
messageTokens[ 1 ] = (messageTokens[ 1 ]).substring( 0 , ii ) ;
data.setRecID( Integer.parseInt( messageTokens[ 1 ] ) ) ;
try {
file = new RandomAccessFile( "stock.dat", "rw" );
file.seek( 0 * data.getSize() );
file.seek( Integer.parseInt( messageTokens[ 1 ] ) * data.getSize() );
data.write( file );
file.close();
}
catch (IOException e) {
display.append("\n" + e);
}
display.append( "\nThread" + (trdCnt+1) + " The record to be deleted is " +
data.getRecID() + "1 " +
data.getItemName( ) + "2 " +
data.getDeviceType( ) + "3 " +
data.getCurrentStock( ) + "4 " +
data.getPrice( ));
}
/***************** End of deleteName() method *****************************/
/** *****************************************************************************
* The findName() this method finds a name using the RandomAccessFile API and
* the record ID.
***************************************************************************** */
public void findName( String message ) {
}
}
/***************** End of MyThread Class *****************************/
/** ***********************************************************************
* The Record class manages the Address.dat file. This file contains the
* address records that the user wants to maintain. The methods in this class
* are:
* 1- fill ()
* 2- getAddress()
* 3- getCity()
* 4- getEmail()
* 5- getFirstName()
* 6- getLastName()
* 7- getRecID()
* 8- getSize()
* 9- getState()
* 10- getTelephone()
* 11- getZip()
* 12- ReadRec(
* 13- setAddress()
* 14- setCity()
* 15- setEmail()
* 16- setFirstName()
* 17- setLastName()
* 18- setRecID()
* 19- setState()
* 20- setTelephone()
* 21- setZip()
* 22- write()
*
*
*
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
************************************************************************* */
public class Record {
private int recID ;
private String ItemName ;
private String DeviceType ;
private String CurrentStock ;
private String Price ;
private int ii = 0 ;
/** ******************************************************************
* ReadRec() reads a record from the specified RandomAccessFile.
* 1- Reads an int for the recid
* 2- Reads chars for the firstname field.
* 3- Reads chars for the lastName field.
* 4- Reads chars for the address field.
* 5- Reads chars for the city field.
* 6- Reads chars for the state field.
* 7- Reads chars for the zip field.
* 8- Reads chars for the telephone field.
* 9- Reads chars for the email field.
*
********************************************************** */
public void ReadRec( RandomAccessFile file ) throws IOException
//public synchronized void ReadRec( RandomAccessFile file ) throws IOException
{
char f[] = new char[ 45 ];
StringTokenizer tokens ;
StringBuffer buf1 = new StringBuffer("");
recID = file.readInt();
for ( int i = 0; i < f.length; i++ ) {
f[ i ] = file.readChar();
if ( !Character.isLetterOrDigit( f[ i ] ) )
f[ i ] = ' ' ;
}
ItemName = new String( f );
ItemName = ItemName.trim() ;
for ( int i = 0; i < f.length; i++ ) {
f[ i ] = file.readChar();
if ( !Character.isLetterOrDigit( f[ i ] ) )
f[ i ] = ' ' ;
}
DeviceType = new String( f );
DeviceType = DeviceType.trim() ;
for ( int i = 0; i < f.length; i++ ) {
f[ i ] = file.readChar();
if ( !Character.isLetterOrDigit( f[ i ] ) )
f[ i ] = ' ' ;
}
CurrentStock = new String( f );
CurrentStock = CurrentStock.trim() ;
for ( int i = 0; i < f.length; i++ ) {
f[ i ] = file.readChar();
if ( !Character.isLetterOrDigit( f[ i ] ) )
f[ i ] = ' ' ;
}
Price = new String( f );
Price = Price.trim() ;
for ( int i = 0; i < f.length; i++ ) {
f[ i ] = file.readChar();
if ( !Character.isLetterOrDigit( f[ i ] ) )
f[ i ] = ' ' ;
}
for ( int i = 0; i < f.length; i++ ) {
f[ i ] = file.readChar();
if ( !Character.isLetterOrDigit( f[ i ] ) )
f[ i ] = ' ' ;
}
for ( int i = 0; i < f.length; i++ ) {
f[ i ] = file.readChar();
if ( !Character.isLetterOrDigit( f[ i ] ) )
if ( f[ i ] != '-' || f[ i ] != '@' )
;
else
f[ i ] = ' ' ;
}
for ( int i = 0; i < f.length; i++ ) {
f[ i ] = file.readChar();
if ( !Character.isLetterOrDigit( f[ i ] ) )
if ( f[ i ] == '-' || f[ i ] == '@' || f[ i ] == '.' )
;
else
f[ i ] = ' ' ;
}
//notifyAll() ;
}
/** **************************************************************
* The fill method is used to fill in the passed string with
* blanks.
*****************************************************************/
public StringBuffer fill ( String str, StringBuffer buf ) {
String strTwo = new String( " " +
" " ) ;
if ( str != null )
buf = new StringBuffer( str + strTwo );
else
buf = new StringBuffer( strTwo );
buf.setLength( 45 );
return buf ;
}
/** ************************************************************
* Write a record to the specified RandomAccessFile file.
****************************************************************/
//public synchronized void write( RandomAccessFile file ) throws IOException
public void write( RandomAccessFile file ) throws IOException
{
StringBuffer buf = new StringBuffer( " " );
String str = "" ;
file.writeInt( recID );
str = "" + recID ;
//System.out.println( "The value of quantity on write is "
// + quantity )
buf = fill ( ItemName, buf ) ;
file.writeChars( buf.toString() );
str = str + buf ;
buf = fill ( DeviceType, buf.delete(0, 44) ) ;
file.writeChars( buf.toString() );
str = str + buf ;
buf = fill ( CurrentStock, buf.delete(0, 44) ) ;
file.writeChars( buf.toString() );
str = str + buf ;
buf = fill ( Price, buf.delete(0, 44) ) ;
file.writeChars( buf.toString() );
str = str + buf ;
sysPrint( "\nFrom write: the length of str is " + str.length()
+ " and the string is " + str ) ;
//notifyAll() ;
}
/** *****************************************************************************
* The getRecID() method is used to obtain and return the record ID.
***************************************************************************** */
public int getRecID() { return recID; }
/** *****************************************************************************
* The getFirstName() method is used to obtain and return the First Name.
***************************************************************************** */
public String getItemName() { return ItemName.trim(); }
/** *****************************************************************************
* The getLastName() method is used to obtain and return the Last Name.
***************************************************************************** */
public String getDeviceType() { return DeviceType.trim(); }
/** *****************************************************************************
* The getAddress() method is used to obtain and return the Address.
***************************************************************************** */
public String getCurrentStock() { return CurrentStock.trim(); }
/** *****************************************************************************
* The getCity() method is used to obtain and return the City.
***************************************************************************** */
public String getPrice() { return Price.trim(); }
/** *****************************************************************************
* The setFirstName() method is used to set the First Name field in this record.
***************************************************************************** */
public void setItemName( String f ) { ItemName = f; }
/** *****************************************************************************
* The setLastName() method is used to set the Last Name field in this record.
***************************************************************************** */
public void setDeviceType( String f ) { DeviceType = f; }
/** *****************************************************************************
* The setAddress() method is used to set the Address field in this record.
***************************************************************************** */
public void setCurrentStock( String f ) { CurrentStock = f; }
/** *****************************************************************************
* The setCity() method is used to set the City field in this record.
***************************************************************************** */
public void setPrice( String f ) { Price = f; }
/** *****************************************************************************
* The setRecID() method is used to set the RecID field in this record.
***************************************************************************** */
public void setRecID( int q ) { recID = q; }
public int getSize() { return 752; }
}
}