-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlantArrayListExample.java
More file actions
73 lines (64 loc) · 2.41 KB
/
PlantArrayListExample.java
File metadata and controls
73 lines (64 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.Scanner;
import java.util.ArrayList;
import java.util.StringTokenizer;
// Lab 5 ArrayList
// Maricel Vicente bvicente@syr.edu
public class PlantArrayListExample {
// TODO: Define a PrintArrayList method that prints an ArrayList of plant (or flower) objects
public static void PrintArrayList(ArrayList<Plant> myGarden) {
for (int i=0; i < myGarden.size(); i++) {
myGarden.get(i).printInfo();
}
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String input;
// TODO: Declare an ArrayList called myGarden that can hold object of type plant
ArrayList<Plant> myGarden = new ArrayList<Plant>();
// TODO: Declare variables - plantName, plantCost, colorOfFlowers, isAnnual
String plantName;
String plantCost;
String colorOfFlowers;
Boolean isAnnual;
input = scnr.next();
while(!input.equals("-1")){
// TODO: Check if input is a plant or flower
// Record the plant name
plantName = scnr.next();
// Record the plant cost
plantCost = scnr.next();
if (input.equals("plant"))
{
// Store as a plant object
Plant plnt = new Plant();
// Set plant name
plnt.setPlantName(plantName);
// Set plant cost
plnt.setPlantCost(plantCost);
// Add to the ArrayList myGarden
myGarden.add(plnt);
}
else if (input.equals("flower"))
{
Flower flwr = new Flower();
// set flower name
flwr.setPlantName(plantName);
// set flower cost
flwr.setPlantCost(plantCost);
// Record annual
isAnnual = scnr.nextBoolean();
// Record color of flower
colorOfFlowers = scnr.next();
// set is annual
flwr.setPlantType(isAnnual);
// set color of flowers
flwr.setColorOfFlowers(colorOfFlowers);
// Add to the ArrayList myGarden
myGarden.add(flwr);
}
input = scnr.next();
}
// TODO: Call the method PrintArrayList to print myGarden
PrintArrayList(myGarden);
}
}