You have been asked to develop an application that will read book information from a text file to determine which publishers you use. The application will print a report of all unique publishers used.
Johnny Smith xxxxxxxxxx Harper Collins
Freedie Jones xxxxxxxxxx Harper Collins
Lilly Laine xxxxxxxx Penquin
Bobby Crosby xxxxxxx Atlantic
Troy James xxxxxxxx Atlantic
Output:
Atlantic
Penquin
HarperCollins
Question: Using the TreeSet class, how do I accomplish this, we just started and not sure of how to approach, orginally I developed with the ArrayList, but Prof wants us to use the TreeSet class. 5 1/2 weeks not enough time to absorb it all. Thanks in advance....destined2bbless!:S If I have posted this wrong please instruct on the correct way....thanks
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Publishers;
/**
*
* @author xxxx
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.IllegalFormatException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Scanner;
import java.util.TreeSet;
import java.util.Set;
public class ProcessPublisher
{
private static Scanner inputPublisherRecord;
private static FileWriter outputPublisherList;
private static PublisherRecord publisherRecord;
private ArrayList publisherList = new ArrayList();
private TreeSet<String> publisherSet = new TreeSet <String>();
public void openFiles()
{
try
{
inputPublisherRecord = new Scanner(new File("BookRecords.txt"));
outputPublisherList = new FileWriter("PublisherList.txt", true);
}
catch(FileNotFoundException fileNotFoundException)
{
System.err.println("File not found, could not be open.");
System.exit(1);;
}
catch(SecurityException securityException)
{
System.err.println("Restricted file access, cannot be opened.");
System.exit(1);
}
catch(IOException ioException)
{
Logger.getLogger(ProcessPublisher.class.getName()).log(Level.SEVERE, null
,ioException);
System.exit(1);
}
}
public PublisherRecord getPublisherRecord()
{
PublisherRecord record = new PublisherRecord();
if(inputPublisherRecord.hasNext())
{
record.setFirstName(inputPublisherRecord.next());
record.setLastName(inputPublisherRecord.next());
record.setISBN(inputPublisherRecord.nextInt());
record.setPublisherName(inputPublisherRecord.next());
}
else
{
record = null;
}
return record;
}
public void processBookRecord()
{
publisherRecord = getPublisherRecord();
{
try
{
while(publisherRecord != null)
{
if(!publisherSet.contains(publisherRecord.getPublisherName()))
{
this.publisherSet.add(publisherRecord.getPublisherName());
}
{
Set<String> set = new TreeSet<String>();
//publisherList.add(publisherRecord.getPublisherName());
//StringBuffer tempRecord = new StringBuffer();
//tempRecord.append(publisherRecord.getPublisherName());
//tempRecord.append(System.getProperty("line.separator"));
//outputPublisherList.write(tempRecord.toString().toCharArray());
}
}
}
catch(IllegalFormatException formatException)
{
System.err.println("There is an error with the output.");
System.exit(1);
}
catch(IOException ioException)
{
System.err.println("Error creating the file.");
System.exit(1);
}
}
}
public void closeFiles()
{
try
{
if(inputPublisherRecord !=null)
{
inputPublisherRecord.close();
}
if(outputPublisherList !=null)
{
outputPublisherList.close();
}
}
catch(IOException ioException)
{
System.err.println("Error closing file(s)");
System.exit(1);
}
}
}