Hey folks,
plugging away at a new program for class and I have run into a little snag, or rather a fairly major one for my pea sized brain. We are working on constructing a sorted linked listed that will store author names as well as a sorted book list for each author. The sorted book list will contain relevant information to books sales for that author and book. I believe I have properly set up my Node and sortedList classes and that the sorted list will function fine. My issue is with instantiating a sortedList with they type <Author> and being able to use my methods from the sortedList class as I populate the list. When I try to find and insert an author's name using my sortedList methods(line45) it says: find(Author) in SortedList can not be applied to (java.lang.string) I am concerned that I am missing some casting issue or something else and I am just plain stuck. Any advice would be helpful, with my current issue or just with my code in general. Anything that might help me advance a bit on the assignment and figure out my issue. Thanks.
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class Main<Author extends Comparable<Author>> {
private TextFileReader authorFile;
private Record currentRecord;
String tempAuthor,tempAction, tempIsbn = "";
int tempBooksSold,bookCount = 0;
double tempRoyalty = 0.0;
Author author;
private Author authorName;
public static void main (String args[])
{
(new Main()).ReadRecord ();
}
private void ReadRecord()
{
SortedList <Author> authorList = new SortedList<Author>();
authorFile = new TextFileReader("bubbling.txt");
//Loop to process book sales, remove or report records from the bubbling.txt file
while (authorFile.moreData())
{
currentRecord = new Record (authorFile.readLine ());
tempAuthor = currentRecord.getAuthor();
tempIsbn = currentRecord.getIsbn();
tempBooksSold = currentRecord.getBooksSold();
tempRoyalty = currentRecord.getRoyalty();
tempAction = currentRecord.getTransaction();
System.out.println(tempAction);
System.out.println(tempAuthor);
System.out.println(tempIsbn);
System.out.println(tempBooksSold);
System.out.println(tempRoyalty);
if (tempAction.equals("SAVE")){
authorList.find(tempAuthor);
// authorList.insert(author);
}
if ( tempAction.equals("REPORT")){
// authorList.displayData();
}
}
authorList.displayData();
}
}
class Node <TYPE extends Comparable<TYPE>>
{
TYPE data;
Node<TYPE>link;
Node (TYPE newData, Node<TYPE> newLink)
{
data=newData;
link=newLink;
}
}
class SortedList <TYPE extends Comparable<TYPE>>
{
Node<TYPE> head, current, previous;
{ head = new Node<TYPE> (null,null);
}
boolean find(TYPE search)
// This method looks for search in the list. If found, current points to its node
// and previous points to the node before. If not found, search could be inserted between previous
// and current. True is returned if search is found.
{
previous = head;
current= head.link;
while(current!=null && current.data.compareTo(search)<0)
{ previous =current;
current=current.link;
}
return current!=null && current.data.compareTo(search) == 0;
}
void insert(TYPE newData)
//This method inserts the newData into the list. It assumes that a find was executed to locate the
// position of newData.
{
Node <TYPE> newNode = new Node<TYPE>(newData, current);
previous.link = newNode;
//Used to back up current so it points to the correct node.
current= newNode;
}
void displayData(){
findFirst();
System.out.println("\nReport");
System.out.println("----------------------");
while(current!=null){
System.out.println(current.data);
findNext();
}
}
void remove()
//This method removes from the list the node pointed to by current. It assumes that a find was done to
//locate the node.
{
previous.link = current.link;
current = current.link;
}
void findFirst()
//This method locates previous and current at the front of the list.
{
previous=head;
current = head.link;
}
void findNext()
//This method moves current and previous one node farther down the list.
{
previous = current;
current =current.link;
}
}
class Author implements Comparable<Author>
/*************************************************************************
* This constructor intializes the data fields with the string information
* provided by one line from the cars.txt file.
*************************************************************************/
{
String authorName = "";
SortedList<Book>bookList = new SortedList<Book>();
public SortedList getBookList() {
return bookList;
}
public void setBookList(SortedList <Book> bookList) {
this.bookList = bookList;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
public Author (String author)
{
// bookList = Book;
authorName = author;
}
public int compareTo (Author other)
{
if ( authorName.equals(other.authorName))
return authorName.compareTo(other.authorName);
else return authorName.compareTo(other.authorName);
}
} //end author class
/*************************************************************************
*Class for the members file information.
*************************************************************************/
class Book implements Comparable<Book>
{
public void setIsbnNumber(String isbnNumber) {
this.isbnNumber = isbnNumber;
}
public String getIsbnNumber() {
return isbnNumber;
}
public void setNetRoyalty(double netRoyalty) {
this.netRoyalty = netRoyalty;
}
public double getNetRoyalty() {
return netRoyalty;
}
public void setBookSold(int booksSold) {
this.booksSold = booksSold;
}
public int getBooksSold() {
return booksSold;
}
String isbnNumber = "";
int booksSold = 0;
double netRoyalty = 0.00;
public Book (String isbn, int quantity, double royalty)
{
isbnNumber = isbn;
booksSold = quantity;
netRoyalty = royalty;
}
public int compareTo(Book other) {
if (isbnNumber.equals(other.isbnNumber))
return isbnNumber.compareTo(other.isbnNumber);
else return isbnNumber.compareTo(other.isbnNumber);
}
}
class Record
/*************************************************************************
* This constructor intializes the data fields with the string information
* provided by one line from the cars.txt file.
*************************************************************************/
{
public String getAuthor()
{
return author;
}
public String getTransaction()
{
return transaction;
}
public String getIsbn()
{
return isbn;
}
public int getBooksSold()
{
return booksSold;
}
public double getRoyalty()
{
return royalty;
}
String transaction = "";
String author = "";
String isbn = "";
int booksSold = 0;
double royalty = 0.0;
public Record (String inputLine)
{
try
{
if (inputLine == null)
{
}
else
{
transaction = inputLine.substring(0,6).trim();
author = inputLine.substring(6,30).trim();
isbn = inputLine.substring(30,43).trim();
booksSold= Integer.parseInt(inputLine.substring(43,47).trim());
royalty = Double.parseDouble(inputLine.substring(47,57).trim());
}
}
catch( StringIndexOutOfBoundsException e )
{
booksSold=0;
royalty=0.0;
}
}
} //end author class
//end book class
class TextFileReader
/***************************************************************************
* Class TextFileReader controls reading from a text file by opening the file,
* reading one line at a time, keeping track
* of the end of file, and handling I/O errors.
***************************************************************************/
{
private BufferedReader inputFile;
private String fileName;
private boolean dataLeft;
public TextFileReader (String name)
/***********************************************************************
* This constructor opens the file given by the name passed in the parameter.
***********************************************************************/
{
fileName = name;
try
{
inputFile = new BufferedReader(new FileReader(fileName));
}
catch (IOException error)
{
error.printStackTrace();
System.err.println ("An error occured opening file " + fileName + "!\n");
System.exit(1);
}
dataLeft = true;
} // end TextFileReader
public String readLine ()
/****************************************************************************
* This method reads and returns one line of the text file. if the file is
* empty, an empty string is returned.
****************************************************************************/
{
String inputLine;
try
{
if ( (inputLine = inputFile.readLine()) == null )
dataLeft = false;
else return inputLine;
}
catch (IOException error)
{
System.err.println ("An error occured reading from file " + fileName +
"!\n");
System.exit(1);
}
return null;
} // end readLine
public boolean moreData ()
/**************************************************************************
* This function returns true if the end has not
**************************************************************************/
{ return dataLeft; } // end moreData
}// end class TextFileReader