-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDriver.java
63 lines (50 loc) · 1.2 KB
/
Driver.java
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
public class Driver {
public static Plant[] plants = new Plant[5];
public static int plantCount = -1;
public static void main(String[] args) {
add(new Plant("Almont","Brown"));
add(new Herb("Tulsi","Green",true,"All"));
add(new Flower("Rose","Red",true,true));
display();
remove("Tulsi");
System.out.println("After deleting:");
display();
System.out.println("Search result:");
Plant temp = search("Rose");
System.out.println(temp.toString());
}
public static void add(Plant p) {
plantCount++;
plants[plantCount] = p;
}
public static void remove(String name) {
int index = -1;
for (int i = 0; i <= plantCount; i++) {
if(plants[i].getName().equals(name)) {
index = i;
break;
}
}
for (int i = index; i < plantCount; i++) {
plants[i] = plants[i+1];
}
plantCount--;
}
public static Plant search(String name) {
int index = -1;
for (int i = 0; i <= plantCount; i++) {
if(plants[i].getName().equals(name)) {
index = i;
break;
}
}
if(index == -1)
return new Plant();
return plants[index];
}
public static void display() {
for (int i = 0; i <= plantCount; i++) {
System.out.println(plants[i].toString());
}
}
}