Finish current lab
This commit is contained in:
parent
3074e5759c
commit
0e08cb5b7a
19
plant-information/Flower.java
Normal file
19
plant-information/Flower.java
Normal file
@ -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");
|
||||
}
|
||||
}
|
91
plant-information/MyGarden.java
Normal file
91
plant-information/MyGarden.java
Normal file
@ -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<Plant> myGarden;
|
||||
myGarden = inputPlants(scnr);
|
||||
|
||||
printArrayList(myGarden);
|
||||
}
|
||||
|
||||
public static void printArrayList(ArrayList<Plant> garden){
|
||||
for (Plant plant:garden){
|
||||
plant.printInfo();
|
||||
}
|
||||
|
||||
}
|
||||
public static ArrayList<Plant> inputPlants(Scanner scnr){
|
||||
String pName, pCost, pAnnual, pColor;
|
||||
String s;
|
||||
String[] splitString;
|
||||
boolean isInt;
|
||||
Plant currPlant;
|
||||
|
||||
ArrayList<Plant> garden = new ArrayList<Plant>();
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
15
plant-information/Plant.java
Normal file
15
plant-information/Plant.java
Normal file
@ -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");
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user