diff --git a/plant-information/Flower.java b/plant-information/Flower.java new file mode 100644 index 0000000..1bdeb41 --- /dev/null +++ b/plant-information/Flower.java @@ -0,0 +1,19 @@ +public class Flower extends Plant { + private String isAnnual; + private String color; + + public Flower(String name, String cost, String isAnnual, String color){ + super(name, cost); + this.isAnnual = isAnnual; + this.color = color; + } + + @Override + public void printInfo(){ + System.out.println("Plant Information:"); + System.out.println("\tPlant name: " + name); + System.out.println("\tCost: " + cost); + System.out.println("\tAnnual: " + isAnnual); + System.out.println("\tColor of flowers: " + color + "\n"); + } +} diff --git a/plant-information/MyGarden.java b/plant-information/MyGarden.java new file mode 100644 index 0000000..eb8acc2 --- /dev/null +++ b/plant-information/MyGarden.java @@ -0,0 +1,91 @@ +import java.util.Scanner; +import java.util.ArrayList; + + +public class MyGarden{ + public static void main(String[] args){ + Scanner scnr = new Scanner(System.in); + ArrayList myGarden; + myGarden = inputPlants(scnr); + + printArrayList(myGarden); + } + + public static void printArrayList(ArrayList garden){ + for (Plant plant:garden){ + plant.printInfo(); + } + + } + public static ArrayList inputPlants(Scanner scnr){ + String pName, pCost, pAnnual, pColor; + String s; + String[] splitString; + boolean isInt; + Plant currPlant; + + ArrayList garden = new ArrayList(); + + while(true){ + + //plant has name and cost 3 inputs + //flower has all 5 inputs + + System.out.println("Please enter your plant: "); + System.out.println("type name cost (annual) (color)"); + s = scnr.nextLine(); + splitString = s.split(" "); + + //Check for exit + if (isInteger(splitString[0])){ + if (Integer.parseInt(splitString[0]) == -1){break;} + } + //Check if too many words or too few words + if (splitString.length > 5) { + System.out.println("Too many words!!!"); + continue; + } + else if ((splitString.length !=3) && + (splitString.length !=5)){ + System.out.println("Expecting 3 or 5 words!; -1 to quit"); + continue; + } + + //Set generic strings to vars for readability + pName = splitString[1]; + pCost = splitString[2]; + + + + // Plants take 3 + if (splitString.length == 3){ + currPlant = new Plant(pName, pCost); + garden.add(currPlant); + } + // Flowers take 5 + else{ + //get remaining strings + pAnnual = splitString[3]; + pColor = splitString[4]; + + currPlant = new Flower(pName, pCost, pAnnual, pColor); + garden.add(currPlant); + } + + System.out.println(); + } + return garden; + } + + public static boolean isInteger(String s){ + boolean isInt = false; + try{ + Integer.parseInt(s); + isInt = true; + } + catch (NumberFormatException ex){ + } + return isInt; + } + +} diff --git a/plant-information/Plant.java b/plant-information/Plant.java new file mode 100644 index 0000000..2275022 --- /dev/null +++ b/plant-information/Plant.java @@ -0,0 +1,15 @@ +public class Plant{ + protected String name, cost; + + public Plant(String name, String cost){ + this.name = name; + this.cost = cost; + } + + public void printInfo(){ + System.out.println("Plant Information:"); + System.out.println("\tPlant name: " + name); + System.out.println("\tCost: " + cost + "\n"); + } + +}