import java.io.*;
import java.util.*;
import javax.swing.*;
public class Assignment5
{
public static void main( String[] args) throws IOException
{
new Assignment5();
}
public Assignment5() throws IOException
{
Scanner input = new Scanner(System.in);
ArrayList<Plantclass> plantPack = new ArrayList<Plantclass>();
System.out.println("Welcome to my plant pack application.");
System.out.println("Please select a options below");
System.out.println("");
while (true)
{
// Give the user a list of their options
System.out.println("1: Add an item to the pack.");
System.out.println("2: Remove an item from the pack.");
System.out.println("3: Search for a plant.");
System.out.println("4: Display the plant in the pack.");
System.out.println("5: Filter plant pack by incomplete name.");
System.out.println("6: Add/Load a plant file. ");
System.out.println("0: Exit the plant pack interface.");
System.out.print( "Answer:");
// Get the user input
int userChoice = input.nextInt();
switch (userChoice)
{
case 1:
addPlant(plantPack);
break;
case 2:
removePlant(plantPack);
break;
case 3:
searchPlant(plantPack);
break;
case 4:
displayPlant(plantPack);
break;
case 5:
filterPlant(plantPack);
break;
case 6:
saveOrLoadPlant(plantPack);
break;
case 0:
System.out.println("Thank you for using the flower pack interface. See you again soon!");
System.exit(0);
}
}
}
public void addPlant( ArrayList<Plantclass> plantPack)
{
String iD,name,color,smell,thorns,poisonous,edible,medicinal;
Scanner scan = new Scanner(System.in);
Plantclass test = new Plantclass();
System.out.println( "Hello please make a selection. ");
System.out.println( "Option 1: Add Flower ");
System.out.println( "Option 2: Add Fungus ");
System.out.println( "Option 3: Add Weed ");
System.out.print( "Answer:");
int ans = scan.nextInt();
switch(ans)
{
case 1:
System.out.print( " Please enter your ID number for your flower.");
iD = scan.next();
System.out.print( " Please enter the name of your flower. ");
name = scan.next();
System.out.print(" Please enter the color of your color. ");
color = scan.next();
System.out.print( " Is there any thorns? (Yes/No) ");
thorns = scan.next();
System.out.print( " Is there a smell? (Yes/No) ");
smell = scan.next();
Flower newFlower = new Flower(iD, name,color, thorns, smell );
plantPack.add(newFlower);
System.out.println();
break;
case 2:
System.out.print( " Please enter your ID number for your flower.");
iD = scan.next();
System.out.print( " Please enter the name of your Fungus. ");
name = scan.next();
System.out.print( " Please enter the color of your Fungus. ");
color = scan.next();
System.out.print( " Is it poisonous? (Yes/No) ");
poisonous = scan.next();
Fungus newFungus = new Fungus(iD, name,color, poisonous );
plantPack.add(newFungus);
System.out.println();
break;
case 3:
System.out.print( " Please enter your ID number for your ID.");
iD = scan.next();
System.out.print( " Please enter the name of your weed. ");
name = scan.next();
System.out.print( " Please enter the color of your weed. ");
color = scan.next();
System.out.print( " Is it poisonous? (Yes/No) ");
poisonous = scan.next();
System.out.print( " Is it edible? (Yes/No) ");
edible = scan.next();
System.out.print( " Is it used for medicinal? (Yes/No) ");
medicinal = scan.next();
Weed newWeed = new Weed(iD, name, color, poisonous, edible, medicinal);
plantPack.add(newWeed);
System.out.println();
break;
}
}
private void removePlant(ArrayList<Plantclass> plantPack)
{
Scanner scan = new Scanner(System.in);
String s;
System.out.println( " Please enter the plant name you would like to remove. ");
System.out.print(" Ans:");
s = scan.next();
for(int i = 0; i < plantPack.size(); i++)
{
Plantclass removePlant = plantPack.get(i);
if(removePlant.getName().equalsIgnoreCase(s))
{
plantPack.remove(i);
break;
}
}
}
private void displayPlant(ArrayList<Plantclass> plantPack)
{
System.out.println();
for(int i = 0; i < plantPack.size(); i++)
{
System.out.println(plantPack.get(i));
}
System.out.println();
}
public void searchPlant(ArrayList<Plantclass> plantPack)
{
Scanner scan = new Scanner(System.in);
System.out.println(" Please search for the plant you would to search for. ");
System.out.print(" Ans:");
String stringAns = scan.next();
boolean FlagAns = false;
for(int i = 0; i < plantPack.size(); i++)
{
Plantclass newPlantPack = plantPack.get(i);
if( newPlantPack.getName().equalsIgnoreCase(stringAns) )
{
FlagAns = true;
break;
}
}
if(FlagAns)
{
System.out.println(" Yes! " + stringAns + " is in the pack of plants.");
}
else
{
System.out.println(" Sorry! " + stringAns + " is not in the pack of plants.");
}
}
public void filterPlant(ArrayList<Plantclass> plantPack)
{
Scanner scan = new Scanner(System.in);
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 < plantPack.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("");
}
public void saveOrLoadPlant(ArrayList<Plantclass> plantPack) throws IOException
{
Scanner scan = new Scanner(System.in);
System.out.println(" Option 1: Would like to SAVE your file? ");
System.out.println(" Option 2: Would like to LOAD a file? ");
System.out.print( " answer:");
int i = scan.nextInt();
Plantclass testjr = new Plantclass();
if(i == 1)
{
File file = new File("C:/Users/gshavers/Desktop/plantFile.txt ");
FileOutputStream outFileStream = new FileOutputStream(file);
PrintWriter outStream = new PrintWriter(outFileStream);
for(int x = 0; x < plantPack.size(); x++)
{
outStream.println(plantPack.get(x));
}
System.out.println();
System.out.println(" File created.....PLEASE check your desktop for plantFile.txt.");
System.out.println();
outStream.close();
}
if(i == 2)
{
String iD,name,color,smell,thorns,poisonous,edible,medicinal;
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);
plantPack.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);
plantPack.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);
plantPack.add(newWeed);
}
}
System.out.println(" File has beeen uploaded.");
System.out.println();
input.close();
}
System.out.println();
}
}
}
Plantclass
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;
}
}
Flower class
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;
}
}
Fungus class
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;
}
}
Weed class
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;
}
}