Startpunkten
This commit is contained in:
184
Java_Database/Linked.java
Normal file
184
Java_Database/Linked.java
Normal file
@@ -0,0 +1,184 @@
|
||||
import java.awt.*;
|
||||
import java.applet.*;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* This is the mainfile containing functions for
|
||||
* this linked list with books. It also contains
|
||||
* the constructor List that generates a new list
|
||||
* called: myList.
|
||||
*
|
||||
*
|
||||
* @author Christian Ohlsson, Karlstad university 1999
|
||||
* @version 1.3.4
|
||||
*/
|
||||
public class Linked {
|
||||
static List myList = new List();
|
||||
|
||||
/**
|
||||
* This function opens up a file and reads it
|
||||
* line by line into a Stringbuffer. This
|
||||
* buffer gets later on splitted into different
|
||||
* categories by the StringTokenizer. The
|
||||
* token I have choosed is the ; character,
|
||||
* which separates the different fields apart
|
||||
* in the file containing the data. After a
|
||||
* line is read and tokenized, different
|
||||
* variables is set in the node called myBook.
|
||||
* Every new node, book, is inserted into myList.
|
||||
*/
|
||||
public static int loadBooks(){
|
||||
String s, token = ";";
|
||||
int rows = 0;
|
||||
try {
|
||||
BufferedReader infil = new BufferedReader(new FileReader("fil.dat"));
|
||||
do {
|
||||
Book myBook = new Book();
|
||||
s = infil.readLine();
|
||||
StringTokenizer st = new StringTokenizer(s, token);
|
||||
myBook.subject = st.nextToken(token);
|
||||
myBook.title = st.nextToken(token);
|
||||
setAuthors(myBook, st.nextToken(token));
|
||||
myBook.publisher = st.nextToken(token);
|
||||
myBook.year = st.nextToken(token);
|
||||
myBook.into(myList);
|
||||
rows++;
|
||||
}while(true);
|
||||
}
|
||||
catch(NoSuchElementException e1){System.err.println("No Such Element.");}
|
||||
catch(FileNotFoundException e2){System.err.println("File Not Found.");}
|
||||
catch(IOException e3){System.err.println("I/O Error.");}
|
||||
catch(NullPointerException e4){}
|
||||
System.out.println("This database contains " + rows + " books.");
|
||||
return rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function reads a line from STDIN by using
|
||||
* DataInputStream, and if the beginning character
|
||||
* is an 'x', the program quits, if no character
|
||||
* is given, the function restarts.
|
||||
* If everything went OK, the search-string is sent
|
||||
* to a function called inCatalog(String s) for
|
||||
* further processing.
|
||||
*/
|
||||
public static void search(int rows) {
|
||||
DataInputStream in = new DataInputStream(new BufferedInputStream(System.in));
|
||||
String s;
|
||||
try {
|
||||
for (int i = 0; i < rows ; i++) {
|
||||
System.out.print("\nBook register\n" +
|
||||
"Press 'x' to exit.\n" +
|
||||
"############################################\n" +
|
||||
"Search for: ");
|
||||
s = in.readLine();
|
||||
if(s.charAt(0) == 'x')
|
||||
break;
|
||||
else
|
||||
inCatalog(s, rows);
|
||||
}
|
||||
}
|
||||
catch(IOException e1) { e1.printStackTrace(); }
|
||||
catch(StringIndexOutOfBoundsException e2) {
|
||||
System.out.println("Enter a value, or press 'x' to exit");
|
||||
search(rows);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function calls the function myList.getElement
|
||||
* and gets every node, book, in the database.
|
||||
* It then compares every value of the node to the
|
||||
* given String, and if it finds a match: the whole
|
||||
* node is printed out along with the number of entries
|
||||
* the search value found matches with. Otherwise, a
|
||||
* message is printed out on the screen.
|
||||
*/
|
||||
public static void inCatalog(String soktVarde, int rows) {
|
||||
int found = 0;
|
||||
try {
|
||||
for (int i=0 ; i<rows ; i++) {
|
||||
Book myBook = new Book();
|
||||
myBook = (Book)myList.getElement(i);
|
||||
if((myBook.subject.compareTo(soktVarde) == 0) ||
|
||||
(myBook.title.compareTo(soktVarde) == 0) ||
|
||||
(isAuthor(myBook.author, soktVarde) ) ||
|
||||
(myBook.publisher.compareTo(soktVarde) == 0) ||
|
||||
(myBook.year.compareTo(soktVarde) == 0)) {
|
||||
found++;
|
||||
System.out.println("\nSubject \t" + myBook.subject);
|
||||
System.out.println("Title \t\t" + myBook.title);
|
||||
System.out.print("Author \t\t" + myBook.allAuthors);
|
||||
System.out.println("\nPublisher \t" + myBook.publisher);
|
||||
System.out.println("Year \t\t" + myBook.year);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(NullPointerException e) {}
|
||||
if (found > 0)
|
||||
System.out.println("There were " + found +
|
||||
" entries in the database about " +
|
||||
soktVarde + ".");
|
||||
else
|
||||
System.out.println("There's no information about " +
|
||||
soktVarde + " in this database");
|
||||
}
|
||||
/**
|
||||
* This function recives an array of authors and a
|
||||
* search string. If then loops through the array
|
||||
* of authors and tries to locate the search string
|
||||
* in the array. Because I use indexOf for the search,
|
||||
* the author Kernighan A. B will be found if the search
|
||||
* string is Kerni, rnighhan, or n A. B.
|
||||
* It returns true if the author was found, otherwise false.
|
||||
*/
|
||||
public static boolean isAuthor(String author[], String soktVarde) {
|
||||
try {
|
||||
int i=0;
|
||||
while(author[i] != null) {
|
||||
if(author[i].indexOf(soktVarde) >= 0)
|
||||
return true;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
catch(NullPointerException e4){System.out.println("Null ptr in isAthor");}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function recieves a Book, and a String
|
||||
* containing all of the authors of a Book. It
|
||||
* the splits up the String, using the StringTokenizer
|
||||
* and puts the different authors in different
|
||||
* places in the array of authors of each Book.
|
||||
*/
|
||||
public static void setAuthors(Book myBook, String inString) {
|
||||
String token = ",";
|
||||
int i=0;
|
||||
myBook.allAuthors = inString;
|
||||
try {
|
||||
StringTokenizer st = new StringTokenizer(inString, token);
|
||||
while(st.hasMoreTokens()) {
|
||||
myBook.author[i] = st.nextToken(token);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
catch(NoSuchElementException e1){System.err.println("No Such Element.");}
|
||||
catch(NullPointerException e4){System.err.println("Null Pointer In setAuthors.");}
|
||||
}
|
||||
|
||||
/**
|
||||
* The main function really does'nt do much.
|
||||
* It just calls the functions loadBooks()
|
||||
* and gets the number of Books in the database
|
||||
* from there and then calls the search-function
|
||||
* with the number of Books as a parameter.
|
||||
*/
|
||||
public static void main(String args[]) {
|
||||
int rows = loadBooks();
|
||||
search(rows);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user