2 Replies - 4505 Views - Last Post: 05 November 2009 - 06:59 PM Rate Topic: -----

#1 Geetanjalid   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 31-December 08

updating a master file from a transaction file

Posted 05 November 2009 - 11:10 AM

I need to write a DeleteEmployees class that reverses the below operation by removing any records in the transaction file from the master file. The transaction file should contain only id numbers.
One way is to use the new_master.txt file supplied/created by the last program and a transaction file that looks like this:
to_delete.txt
1001
1002
1010
So 3 records should be deleted from new_master.

import java.io.*;
/**
 * Illustrate updating a master file from a transaction file
  */
public class AddEmployees
{
  private static final String MASTER = "master.txt";
  private static final String TRANS = "transaction.txt";
  private static final String NEW_MASTER = "new_master.txt";
  private static final String SPLITTER = "@";
  // Files to read and write.
  private TextFileReader mr;  // master
  private TextFileReader tr;  // transaction
  private TextFileWriter mw;  // new master
  
  public static void main(String[] args){ new AddEmployees(); }
  
  public AddEmployees()
  {
	openFiles();
	doUpdate();
	closeFiles();
  }
  private void openFiles()
  {
	mr = new TextFileReader(MASTER);
	tr = new TextFileReader(TRANS);
	mw = new TextFileWriter(NEW_MASTER);
  }
  // Assumes neither file is completely empty
  private void doUpdate()
  {
	// While there are more transaction records
	String trecord = readRecord(tr);
	boolean endMaster = false;
	while (!trecord.equals("EOF") && !endMaster )
	{
	  // extract the ID
	  int tid = Integer.parseInt(trecord.substring(0,4));
	  // Read records from master until EOF reached
	  String mrecord = readRecord(mr);
	  while (!mrecord.equals("EOF"))
	  {
		// extract the id
		int mid = Integer.parseInt(mrecord.substring(0,4));
		// comapare id's
		while (mid > tid)
		{
		  // insert new record, read new transaction record
		  writeRecord(mw, trecord);
		  trecord = readRecord(tr);
		  tid = Integer.parseInt(trecord.substring(0,4));
		}
		// write current record to the new master
		writeRecord(mw, mrecord);
		// Read another master record
		mrecord = readRecord(mr);
	  }
	  endMaster = true;
	}
	// End of master, could still be more records to write from transaction
	while (!trecord.equals("EOF"))
	{ 
	  writeRecord(mw, trecord); 
	  trecord = readRecord(tr);
	}
  }
  private void closeFiles()
  {
   // Could do file renaming here if desired
	mr.close();
	tr.close();
	mw.close();
  }
  // reads four lines from a text file
  // represents an employee record
  private String readRecord(TextFileReader tf)
  {
	String result = "EOF";
	String line = tf.readLine();
	if (line != null){ result = line; }  // read the id
	line = tf.readLine();
	if (line != null){ result = result + SPLITTER + line; }  // read the name
	line = tf.readLine();
	if (line != null){ result = result + SPLITTER + line; }  // read the hours worked
	line = tf.readLine();
	if (line != null){ result = result + SPLITTER + line; }  // read the hourly rate
	return result;	
  }
  // Assumes 4 bits of text in the String, with @ as the separator
  private void writeRecord(TextFileWriter tf, String s)
  {
	// Unfortunately println to a file strips out
	// any "/n" new lines so we have to split the String
	// back into its components.
	String[] result = s.split(SPLITTER);
	tf.println(result[0]);
	tf.println(result[1]);
	tf.println(result[2]);
	tf.println(result[3]);
  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: updating a master file from a transaction file

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: updating a master file from a transaction file

Posted 05 November 2009 - 02:20 PM

Create two of these and use nested loops:

http://technojeeves..../74-string-list
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: updating a master file from a transaction file

Posted 05 November 2009 - 06:59 PM

g00se has the right idea, but I think we can tweak it to shorten the code.

Since the transaction file contains only integers, that might be a good place to start instead of reading them in as Strings. For this reason, a Scanner is probably the better choice instead of a TextFileReader b/c it can read in values as ints.

ArrayList<Integer> toRemove = new ArrayList<Integer>();
Scanner scan = new Scanner(new File(TRANS));

//meaning Scanner will stop reading once it reaches the EOF String
while(scan.hasNextInt()) toRemove.add(scan.nextInt());




Now since I don't know the format of your master file, I am going to make the assumption that the first token is the ID. If not, you will have to go through x tokens on the tokenizer to get to the ID. I am also going to make the assumption that each category in the line is separated by whitespace. If I am wrong, then instantiate the StringTokenizer using the parameters (scan.nextLine(), String yourDelimiter).
scan = new Scanner(new File(MASTER));

StringTokenizer temp; 
String hold;
ArrayList<String> toWrite = new ArrayList<String>(); //holds values that pass inspection

while(scan.hasNextLine()){
	  hold = scan.nextLine();
	  temp = new StringTokenizer(hold);
	  if(temp.nextToken().equals("EOF")) break;
	  if(!toRemove.contains(Integer.parseInt(temp.nextToken()))) toWrite.add(hold);
}

scan.close();



From here, just write each String stored in the ArrayList toWrite. In this way, you are filtering as you are going, which is more efficient than gathering everything and then going back and filtering. Also, Scanner tends to be a lot easier for reading and parsing Files than the other Java tools, though it may not be as efficient as some of the others. However, in this case, I feel the ease of use is worth the tradeoff.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1