Project 4 – Average 2

import java.util.*;

import java.io.*;

class MyNode implements Comparable, Cloneable{

private MyNode link;

private Comparable data;

public void MyNode(){

link = null;

data = null;

}

public void MyNode(Comparable data, MyNode link){

this.link = link;

this.data = data;

}

public void setData(Comparable data){

this.data = data;

}

public void setLink(MyNode link){

this.link = link;

}

public Comparable getData(){

return this.data;

}

public MyNode getLink(){

return this.link;

}

public int compareTo(Object object){

MyNode node = new MyNode();

if (object instanceof MyNode){

node = (MyNode) object;

}

return this.data.compareTo(node.getData());

}

public MyNode clone(){

MyNode node = new MyNode();

node.setData(this.data);

node.setLink(this.link);

return node;

}

}

class MySLL implements Cloneable{

private MyNode head;

private MyNode tail;

public void MySLL(){

head = null;

tail = null;

}

public void MySLL(MyNode head, MyNode tail){

this.head = head;

this.tail = tail;

}

public MyNode getHead(){

return head;

}

public MyNode getTail(){

return tail;

}

public void setHead(MyNode head){

this.head = head;

}

public void setTail(MyNode tail){

this.tail = tail;

}

public boolean isEmpty() {

return ((head == null));

}

public void append(MyNode node){

if(isEmpty()){

head = node;

node.setLink(null);

}

else{

tail.setLink(node);

}

tail = node;

}

public void append(Comparable data){

MyNode node = new MyNode();

node.setData(data);

if(isEmpty()){

head = node;

}

else{

tail.setLink(node);

}

tail = node;

}

public void add(String string){

append(string);

} //adds to tail

public void add(MyNode node){

append(node);

} // adds to tail

public void insert(String string){

MyNode current = new MyNode();

MyNode temp = new MyNode();

current = head;

if(isEmpty()){

append(string);

}

else if (head.getData().compareTo(string) < 1){

while(current.getData().compareTo(string) < 1){

current = current.getLink();

}

temp = current.getLink();

current.setLink(temp);

temp.setData(string);

}

else if(head.getData().compareTo(string) ==0){

temp = head.getLink();

head.setLink(temp);

temp.setData(string);

}

else{

temp.setData(string);

temp.setLink(head);

}

}

public void remove(String s){

MyNode current = head;

if(current.getData().compareTo(s) == 0){

head = head.getLink();

if(current == tail){

tail = current.getLink();

}

current.setLink(null);

}

else{

MyNode previous = head;

current = head.getLink();

while((current.getData().compareTo(s)) != 0 & current !=null){

previous = current;

current = current.getLink();

}

if(current !=null){

previous.setLink(current.getLink());

current.setLink(null);

}

else if(current == tail){

previous.setLink(null);

tail = previous;

}

else{

System.out.print("The string was not found in the list");

}

}

}

public void printList(){

MyNode current = head;

if( current == null){

System.out.println("The list is empty");

System.out.println();

}

else{

while (current.getLink() != null){

System.out.print(current.getData());

current = current.getLink();

}

System.out.print(current.getData());

}

}

public Object clone(){

MySLL sll = new MySLL();

sll.setHead(this.head.clone());

sll.setTail(this.tail.clone());

return sll;

}

public int compareTo(Object object){

MySLL sll = new MySLL();

if (object instanceof MySLL){

sll = (MySLL) object;

}

MyNode temp1 = new MyNode();

temp1 = this.getHead();

MyNode temp2 = new MyNode();

temp2 = sll.getHead();

int counter = 1;

int result = 0;

MyNode filler = new MyNode();

while(temp1.getData()!= null & temp2.getData()!=null){

if(temp1.getData() == null){

result += (counter * filler.getData().compareTo(temp2.getData()));

counter++;

temp2 = temp2.getLink();

}

else if(temp2.getData() == null){

result += (counter * temp1.getData().compareTo(filler.getData()));

counter++;

temp1 = temp1.getLink();

}

else {

result += (counter * temp1.getData().compareTo(temp2.getData()));

counter++;

temp1 = temp1.getLink();

temp2 = temp2.getLink();

}

}

return result;

}

public MySLL subList(int start, int end){ // change from void

MyNode temp = new MyNode();

MyNode perv = new MyNode();

MySLL sll = new MySLL();

int counter = 0;

temp = head;

perv = head;

while(perv != null & start != counter){

if(counter == 0){

temp.getLink();

counter++;

}

else{

perv = perv.getLink();

temp = temp.getLink();

counter++;

}

}

while(temp.getLink()!= null & counter != end +1){

sll.append(temp.getData());

if(start == counter){

sll.setHead(temp);

}

temp = temp.getLink();

if (counter == end){

sll.setTail(temp);

}

counter++;

}

if(temp.getLink() == null){

tail = temp;

}

perv.setLink(temp);

return sll;

}

public void readFile(MySLL sll) throws FileNotFoundException, BadFileDataException {

System.out.print("whats the name of the file you wish to open(include .txt at the end):");

String fileName = UserInput.readString();

Scanner sc = new Scanner(new File(fileName));

char d[] = {'"'};

String str = new String(d);

String dataInput = new String("");

String line;

try{

if(sc.next()== "break"){

System.out.print("hey3");

throw new BadFileDataException();//exception

}

sc.next();

while(sc.hasNextLine()){

line = sc.nextLine();

StringTokenizer st = new StringTokenizer(line, str);

if(line.equals("break")){

sll.add(line);

line = "";

}

else{

dataInput += line;

System.out.print(st.nextToken());

}

}

}

catch(Exception exception){}

System.out.println();

}

}

class BadFileDataException extends Exception{

}

class Driver{

public static void main(String args[]){

boolean on = true;

while(on){

System.out.print("1)open a file, then populate the SLL" +

"\n 2)compare two SLLs" +

"\n 3)Add to tail of SLL"+

"\n 4)Insert data" +

"\n 5)subList" +

"\n 6)Remove data" +

"\n 7)Print List" +

"\n 8) quit:");

System.out.print("enter your selection:");

int selection = UserInput.readInt();

int a = 0;

int b = 0;

int answer = 0;

String ss = "";

MySLL sll1 = new MySLL();

MySLL sll2 = new MySLL();

MySLL subsll = new MySLL();

if(selection ==1){

try {

readFile(sll1);

}

catch (Exception exception){}

}

if(selection == 2){

sll1.compareTo(sll2);

}

if(selection == 3){

System.out.println("Which would you like to add to list 1 or 2?");

answer = UserInput.readInt();

System.out.println("Enter the string you would like to add");

ss = UserInput.readString();

if(answer == 1){

sll1.add(ss);

}

else if(answer ==2){

sll2.add(ss);

}

}

if(selection == 4){

System.out.println("Which would you like to insert data into list 1 or 2?");

answer = UserInput.readInt();

System.out.println("Enter the string you would like to add");

ss = UserInput.readString();

if(answer == 1){

sll1.insert(ss);

}

else if(answer ==2){

sll2.insert(ss);

}

}

if(selection == 5){

System.out.println("Which would you like to take nodes from list 1 or 2?");

answer = UserInput.readInt();

System.out.println("what is your starting point choose between 0 and the end of the list");

a = UserInput.readInt();

System.out.println("what is your ending point choose between the starting point and the end of the list");

b = UserInput.readInt();

if(answer == 1){

sll1.subList(a, b);

}

else if(answer ==2){

sll2.subList(a, b);

}

}

if(selection == 6){

System.out.println("Which would you like to rome data from list 1 or 2?");

answer = UserInput.readInt();

System.out.println("enter the string you would like to remove");

ss = UserInput.readString();

if(answer == 1){

sll1.remove(ss);

}

else if(answer ==2){

sll2.remove(ss);

}

}

if(selection == 7){

System.out.println("Which list would you like to print list 1 or 2?");

answer = UserInput.readInt();

if(answer == 1){

sll1.printList();

}

else if(answer ==2){

sll2.printList();

}

}

if(selection == 8){

on = false;

}

}

}

public static void readFile(MySLL list) throws BadFileDataException{

System.out.print("Load File: ");

BufferedReader reader;

try{

reader = new BufferedReader (new FileReader(UserInput.readString()));

String data = new String();

data = "";

String temp = reader.readLine();

if(!temp.equals("break")){

throw new BadFileDataException();

}

//MySLL thing = new MySLL();

//list = thing;

while(temp != null){

try{

temp = reader.readLine();

if(!temp.equals("break")){

data = data + temp;

//System.out.print(data);

}

else{

list.add(data);

data = "";

}

}

catch(NullPointerException e){}

}

System.out.print(data);

list.add(data);

}

catch(FileNotFoundException exception){System.out.println("File not found");}

catch(IOException exception){}

}

}