Startpunkten

This commit is contained in:
2026-03-05 13:43:17 +01:00
commit f54dd53c73
13 changed files with 679 additions and 0 deletions

20
Java_Database/Book.java Normal file
View File

@@ -0,0 +1,20 @@
/**
* This is a class called Book wich is
* a part of Element. So, each Element
* is a Book containing all the info
* about every Book.
*
* @author Christian Ohlsson, Karlstad university 1999
* @version 1.0.0
*/
public class Book extends Element {
int MAXAUTHORS = 5;
String subject;
String title;
String allAuthors;
String []author = new String [MAXAUTHORS];
String publisher;
String year;
}

View File

@@ -0,0 +1,86 @@
/**
* This is a class containing functios
* for the Elements in the list.
*
*
* @author Christian Ohlsson, Karlstad university 1999
* @version 1.0.0
*/
public class Element {
Element suc;
/**
* This is just the constructor.
*/
public Element() { suc = null; }
/**
* Returns the current Element in the list
*/
public Element suc() { return suc; }
/**
* This function inserts a new Element
* at the end of the List.
* It first checks out if the List is empty,
* i.e the first Element is null, and then
* the first Element is this Element.
* Otherwise, it inserts the new Element
* at the end of the List.
*/
public void into(List theList) {
if (theList.first==null)
theList.first = theList.last = this;
else {
theList.last.suc = this;
theList.last = this;
}
suc = null;
}
/**
* This function inserts a new Element at
* the beginning of the list.
* It first checks out if the List is empty,
* i.e the first Element is null, and then
* the first Element is this Element.
* Otherwise, the Element that were in the
* beginning, now is in second place and
* the new Element is in front of the List.
*/
public void intoAsFirst(List theList) {
if (theList.first==null) {
theList.first = theList.last = this;
suc = null;
}
else {
suc = theList.first;
theList.first = this;
}
}
/**
* This function deletes an Element in
* the List by traversing through the
* List until it gets to the Element
* that should be deleted.
*/
public void out(List theList) {
Element prev = null;
Element e = theList.first;
while (e!=null && e!=this) {
prev = e;
e = e.suc;
}
if (e!=null) {
if (theList.last==this)
theList.last = prev;
if (theList.first==this)
theList.first = suc;
else
prev.suc = suc;
suc = null;
}
}
}

184
Java_Database/Linked.java Normal file
View 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);
}
}

70
Java_Database/List.java Normal file
View File

@@ -0,0 +1,70 @@
/**
* This class contains the linked list.
* It also contains the constructor, wich
* generates a new list called myList
*
* @author Christian Ohlsson, Karlstad university 1999
* @version 1.0.0
*/
public class List {
Element first, last;
/**
* This is the constructor of the list.
* What it basically do is to set
* the two Elements constructed before
* to null.
*/
public List() { first = last = null; }
/**
* This function returns the first Element
* in the list.
*/
public Element first() { return first; }
/**
* This function returns the last Element
* in the list.
*/
public Element last() { return last; }
/**
* If the first element in the List is null,
* it returns true, that is, there's no Elements
* in the List.
*/
public boolean empty() { return first==null; }
/**
* This function recives a number and it traverses
* though the List until that number is reached
* and then returns the Element on that location
*/
public Element getElement(int number) {
Element e = first;
int antal = 0;
while (antal<number) {
antal++;
e = e.suc;
}
return e;
}
/**
* This function initialize a number to zero
* and the traverses though the List until
* it reaches the end and than returns
* the number of elements.
*/
public int cardinal() {
int antal = 0;
Element e = first;
while (e!=null) {
antal++;
e = e.suc;
}
return antal;
}
}

10
Java_Database/fil.dat Normal file
View File

@@ -0,0 +1,10 @@
C Lang;The C Programming Language;Kernighan B.W.,Ritchie D.M.;Prentice Hall;1988
C Lang;From C to C: An Intro to ANSI Standard C;Gardner J.;Harcourt, Brace Jovanovich;1989
Compilers;Compilers: Principles Techniques and Tools;Aho A.V.,Sethi R.,Ullman J.D.;Addison Wesley;1986
Compilers;Compiler Design in C;Holub A.I.;Prentice Hall;1990
Databases;An Introduction to Database Systems;Date C.J.;Addison Wesley;1995
Databases;The Theory of Relational Databases;Maier D.;Computer Science Press;1983
HCI;Human-Computer Interaction;Dix A.,Finlay J.,Abowd G.,Beale R.;Prentice Hall;1993
Prog Langs;Programming Languages, Concepts & Constructs;Sethi R.;Addison Wesley;1996
Prog Langs;Concepts of Programming Languages;Sebesta R.W.;Addison Wesley;1996
Prog Langs;Programming Languages;Louden K.C.;PWS Publishing;1993

11
Java_Database/read_me Normal file
View File

@@ -0,0 +1,11 @@
Readme fil för Lab2 i Programspråk
-------------------------------------------
Sökbar länkad lista i Java.
Kompilera med: javac Linked.java
Kör därefter programmet med: java Linked
Labben är provkörd och klar.
-------------------------------------------
Christian Ohlsson, di7chro@cse.kau.se