import java.io.*;
import java.util.*;
import javax.swing.*;
public class CSCFinal
{
static LinkList newList = new LinkList();
public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner(System.in);
while(true)
{
System.out.println(" Hello user, welcome to the student application");
System.out.println(" Please make a selection below.");
System.out.println(" Option 1: Add a plant");
System.out.println(" Option 2: Remove a plant");
System.out.println(" Option 3: Sort plant");
System.out.println(" Option 4: filter plants");
System.out.println(" Option 5: Save plant file");
System.out.println(" Option 6: Load plant file");
System.out.println(" Option 7: Analyze plant");
System.out.println(" Option 8: Display plant");
System.out.println(" Option 0: Exit application");
System.out.print(" Ans:");
int ans = scan.nextInt();
switch(ans)
{
case 1://add plant
{
String iD,name,color,smell,thorns,poisonous,edible,medicinal, flavor, seasonal;
System.out.println("Option 1: Add Flower?");
System.out.println("Option 2: Add Fungus?");
System.out.println("Option 3: Add Weed?");
System.out.println("Option 4: Add Herb?");
System.out.print("Ans:");
int answer = scan.nextInt();
switch(answer)
{
case 1:// add flower
{
boolean flag = true;
while(flag)
{
System.out.print(" Please enter the flower id: ");
iD = scan.next();
System.out.print(" Please enter the name of your flower? ");
name = scan.next();
System.out.print(" What's the color of your flower?: ");
color = scan.next();
System.out.print(" Does your flower have thorns? ");
thorns = scan.next();
System.out.print(" Does your flower have a smell? ");
smell = scan.next();
Flower newFlower = new Flower(iD,name,color,thorns,smell);
newList.add(newFlower);
flag = false;
}
break;//end of flower
}
case 2:// add Fungus
{
boolean flag = true;
while(flag)
{
System.out.print(" Please enter the fungus id: ");
iD = scan.next();
System.out.print(" Please enter the name of your fungus? ");
name = scan.next();
System.out.print(" What's the color of your fungus?: ");
color = scan.next();
System.out.print(" Is your fungus poisonous? ");
poisonous = scan.next();
Fungus newFungus = new Fungus(iD,name,color,poisonous);
newList.add(newFungus);
flag = false;
}
break;
} //end of Fungus
case 3:// add weed
{
boolean flag = true;
while(flag)
{
System.out.print(" Please enter the weed id: ");
iD = scan.next();
System.out.print(" Please enter the name of your weed? ");
name = scan.next();
System.out.print(" What's the color of your weed?: ");
color = scan.next();
System.out.print(" Is your weed poisonous? ");
poisonous = scan.next();
System.out.print(" Is your weed edidble? ");
edible = scan.next();
System.out.print(" Is your weed medicinal? ");
medicinal = scan.next();
Weed newWeed = new Weed(iD,name,color,poisonous,edible,medicinal);
newList.add(newWeed);
flag = false;
}
break;
} //end of weed plant
case 4:// add herb
{
boolean flag = true;
while(flag)
{
System.out.print(" Please enter the herb id: ");
iD = scan.next();
System.out.print(" Please enter the name of your herb? ");
name = scan.next();
System.out.print(" What's the color of your herb?: ");
color = scan.next();
System.out.print(" Does your herb have flavor? ");
flavor = scan.next();
System.out.print(" Is your herb medicinal? ");
medicinal = scan.next();
System.out.print(" Is your herb seasonal? ");
seasonal = scan.next();
Herb newHerb = new Herb(iD,name,color,flavor,medicinal,seasonal);
newList.add(newHerb);
flag = false;
}
break;
} //end of herb plant
}
break;
}
case 2: //remove plant
{
System.out.println( " Please enter the plant name you would like to remove.");
System.out.print(" Ans:");
String key = scan.next();
for(int i = 0; i < newList.size(); i++)
{
String y = newList.get(i).toString();
//if(removePlant.getName().equalsIgnore(s))
{
// newList.get(i).remove();
System.out.println(" Plant has been removed");
break;
}
}
}
break;
case 3: //sort student from list
{
int i, j;
for(i = 0; i < newList.size(); i++ )
{
//String currentMinimum = flowerPack[i];
int currentMinIndex = i;
for( j = i +1; j < newList.size()-1; j++)
{
// int ans = flowerPack[i].compareToIgnoreCase(flowerPack[j]);
if( ans > 0)
{
// currentMinimum = flowerPack[j];
currentMinIndex = j;
}
if(currentMinIndex != i)
{
// flowerPack[currentMinIndex] = flowerPack[i];
// flowerPack[i] = currentMinimum;
}
}
}
}
break;
case 4: //filter student from list
{
String keyWord;
boolean checkAns = false;
System.out.println(" Please enter the character or word your would like to search for.");
System.out.println(" This application will search through the list for your result. ");
System.out.print(" Ans:");
keyWord = scan.nextLine();
for(int i = 0; i < newList.size(); i++)
{
//Plantclass newPlantPack = plantPack.get(i);
// if(newPlantPack.getName().contains(keyWord))
{
System.out.println();
// System.out.println(" " +newPlantPack.getName());
checkAns = true;
}
}
if(checkAns == false)
{
System.out.println( " Sorry user, but are database could not any results.");
}
if(checkAns == true)
{
System.out.println( " Above is the queer of results we located.");
}
System.out.println("");
}
break;
case 5: //create file list
{
Plantclass testjr = new Plantclass();
File file = new File("C:/Users/gshavers/Desktop/plantFile.txt ");
FileOutputStream outFileStream = new FileOutputStream(file);
PrintWriter outStream = new PrintWriter(outFileStream);
for(int i = 1; i <= newList.size(); i++ )
{
outStream.println(newList.get(i).toString());
System.out.println();
}
System.out.println(" File created.....PLEASE check your desktop for plantFile.txt.");
System.out.println();
outStream.close();
}
break;
case 6: //Load list
{
String iD,name,color,smell,thorns,poisonous,edible,medicinal,flavor,seasonal;
JFileChooser fileChooser = new JFileChooser();
if( fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
File file = fileChooser.getSelectedFile();
Scanner input = new Scanner(file);
while(input.hasNext())
{
String test = input.nextLine();
StringTokenizer st = new StringTokenizer(test);
int count = st.countTokens();
if(count == 4)//fungus object
{
iD= st.nextToken();
name = st.nextToken();
color = st.nextToken();
poisonous = st.nextToken();
Fungus newFungus = new Fungus( iD, name, color, poisonous);
newList.add(newFungus);
}
if(count == 5)//flower object
{
iD = st.nextToken();
name = st.nextToken();
color = st.nextToken();
thorns = st.nextToken();
smell = st.nextToken();
Flower newFlower = new Flower(iD, name, color, thorns, smell);
newList.add(newFlower);
}
if(count == 6){//weed object
iD = st.nextToken();
name = st.nextToken();
color = st.nextToken();
poisonous = st.nextToken();
edible = st.nextToken();
medicinal = st.nextToken();
Weed newWeed = new Weed(iD,name,color,poisonous,edible,medicinal);
newList.add(newWeed);
}
if(count == 6){//herb object
iD = st.nextToken();
name = st.nextToken();
color = st.nextToken();
flavor = st.nextToken();
medicinal = st.nextToken();
seasonal = st.nextToken();
Herb newHerb = new Herb(iD,name,color,flavor,medicinal,seasonal);
newList.add(newHerb);
}
}
System.out.println(" File has beeen uploaded.");
System.out.println();
}
}
break;
case 7: //analyze list
{
System.out.println("Please enter a character or set of characters for analyzing");
System.out.print(" input:");
String s = scan.next();
for(int x = 1; x < newList.size(); x++)
{
// Plantclass newplantjr = plantPack.get(x);
// String in = newplantjr.getName();
// substrings(0,1, s, in);
}
}
break;
case 8: //display student list
{
newList.print();
}
break;
case 0: //Exit application
{
System.out.println(" Good Bye!");
System.exit(0);
}
}
System.out.println();
}
}
}
public class LinkList
{
public Node start;
public static int listCount;
public LinkList()
{
start = new Node(null);
listCount = 0;
}
public void add(Object data)
{
Node temp = new Node(data);
Node current = start;
while( current.getNext() != null )
{
current = current.getNext();
}
current.setNext(temp);
listCount++;
}
public Object get(int index)
{
if(index <= 0)
return null;
Node current = start.getNext();
for(int i = 1; i < index; i++)
{
if(current.getNext() == null)
return null;
current = current.getNext();
}
return current.getData();
}
public void remove(String firstLastName)
{
Node prev, del;
String name = firstLastName;
del = start.getNext();
prev = start.getNext();
while (del != null )
{
if (del.getData().toString().contains(firstLastName)) break;
prev = del;
del = del.getNext();
if (del == null)
{
System.out.println( "String Not Found" );
}
else
{
if (del == start)
{
start = start.getNext();
}
else
{
System.out.println(prev);
prev.setNext(del.getNext());
}
}
}
}
public void print()
{
Node current = start.getNext();
while(current != null)
{
String output = "";
output += current.getData().toString();
System.out.println(output);
current = current.getNext();
}
}
public static int size()
{
return listCount;
}
public class Node
{
Node next;
Object data;
public Node(Object data)
{
next = null;
this.data = data;
}
public Node(Object data, Node next)
{
this.next = next;
this.data = data;
}
public Object getData()
{
return data;
}
public void setData(Object data)
{
this.data = data;
}
public Node getNext()
{
return next;
}
public void setNext(Node next)
{
this.next = next;
}
}
}
public class Plantclass
{
public String ID;
public String Name;
public Plantclass()
{
}
public Plantclass(String SetID, String SetName)
{
ID = SetID;
Name = SetName;
}
public void setId(String setId)
{
ID = setId;
}
public String getId()
{
return ID;
}
public void setName(String setName)
{
Name = setName;
}
public String getName()
{
return Name;
}
}
public class Flower extends Plantclass
{
public String color;
public String thorns;
public String smell;
public String setIdJr;
public String setNameJr;
public Flower()
{
}
public Flower(String SetID, String SetName, String color, String thorns, String smell)
{
super(SetID,SetName);
this.color = color;
this.thorns = thorns;
this.smell = smell;
setIdJr = SetID;
setNameJr = SetName;
}
public void setColor(String color)
{
this.color = color;
}
public void setThorns(String thorns)
{
thorns = thorns;
}
public void setSmell(String smell)
{
this.smell = smell;
}
public String getColor()
{
return color;
}
public String getThorns()
{
return thorns;
}
public String getSmell()
{
return smell;
}
public String toString()
{
return setIdJr + " " + setNameJr + " " + color + " " + thorns + " " + smell;
}
}
public class Fungus extends Plantclass
{
public String color;
public String poisonous;
public String setIdJr;
public String setNameJr;
public Fungus()
{
}
public Fungus(String SetID, String SetName, String color, String poisonous)
{
super(SetID,SetName);
this.color = color;
this.poisonous = poisonous;
setIdJr = SetID;
setNameJr = SetName;
}
public void setColor(String color)
{
this.color = color;
}
public void setPoisonous(String poisonous)
{
this.poisonous = poisonous;
}
public String getColor()
{
return color;
}
public String getPoisonous()
{
return poisonous;
}
public String toString()
{
return setIdJr + " " + setNameJr + " " + color + " " + poisonous;
}
}
public class Weed extends Plantclass
{
public String color;
public String poisonous;
public String edible;
public String medicinal;
public String setIdJr;
public String setNameJr;
public Weed()
{
}
public Weed(String SetID, String SetName, String color, String poisonous, String edible, String medicinal)
{
super(SetID,SetName);
this.color = color;
this.poisonous = poisonous;
this.edible = edible;
this.medicinal = medicinal;
setIdJr = SetID;
setNameJr = SetName;
}
public void setColor(String color)
{
this.color = color;
}
public void setPoisonous(String poisonous)
{
this.poisonous = poisonous;
}
public void setEdible(String edible)
{
this.edible = edible;
}
public void setmedicinal(String medicinal)
{
this.medicinal = medicinal;
}
public String getColor()
{
return color;
}
public String getPoisonous()
{
return poisonous;
}
public String setEdible()
{
return edible;
}
public String setMedicinal()
{
return medicinal;
}
public String toString()
{
return setIdJr + " " + setNameJr + " " + color + " " + poisonous + " " + edible + " " + medicinal;
}
}
public class Herb extends Plantclass
{
public String color;
public String flavor;
public String seasonal;
public String medicinal;
public String setIdJr;
public String setNameJr;
public Herb()
{
}
public Herb(String SetID, String SetName, String color, String flavor, String medicinal , String seasonal)
{
super(SetID,SetName);
setIdJr = SetID;
setNameJr = SetName;
this.color = color;
this.flavor = flavor;
this.medicinal = medicinal;
this.seasonal = seasonal;
}
public void setColor(String color)
{
this.color = color;
}
public void setFlavor(String flavor)
{
this.flavor = flavor;
}
public void setMedicinal(String medicinal)
{
this.flavor = flavor;
}
public void setSeasonal(String seasonal)
{
this.seasonal = seasonal;
}
public String getColor()
{
return color;
}
public String getSeasonal()
{
return seasonal;
}
public String setFlavor()
{
return flavor;
}
public String medicinal()
{
return medicinal;
}
public String toString()
{
return setIdJr + " " + setNameJr + " " + color + " " + seasonal + " " + flavor + " " + medicinal + " " + seasonal;
}
}