-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAnimalsArrayList.java
More file actions
155 lines (143 loc) · 4.54 KB
/
AnimalsArrayList.java
File metadata and controls
155 lines (143 loc) · 4.54 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.util.ArrayList;
import java.util.Scanner;
/**
* Example Class to work with ArrayList<String>
*
* @author Raimon Izard
* @since 2023-02-27
* @version 1.0
*/
public class AnimalsArrayList {
/**
* Main method
*
* @param args
*/
public static void main(String[] args) {
ArrayList<String> animals = new ArrayList<>();
animals = initAnimals(animals);
animals = mainMenu(animals);
}
/**
* Main Menu method
*
* @param animals input Arraylist<String>
* @return
*/
private static ArrayList<String> mainMenu(ArrayList<String> animals) {
final String MAIN_MENU_TEXT = "Welcome, choose an option:\n\t1.Show all the animals" + "\n\t2.Add an animal" + "\n\t3.Remove an animal" + "\n\t4.Exit";
int option = 0;
do {
option = getInt(MAIN_MENU_TEXT, 1, 4);
switch (option) {
case 1:
printAnimals(animals);
break;
case 2:
animals = addAnimal(animals);
break;
case 3:
animals = removeAnimal(animals);
break;
case 4:
System.out.println("bye!");
break;
}
} while (option != 4);
return animals;
}
/**
* Method to remove one animal from the list
*
* @param animals input Arraylist<String>
* @return output Arraylist<String>
*/
private static ArrayList<String> removeAnimal(ArrayList<String> animals) {
System.out.println("There are " + animals.size() + " animals: ");
printAnimals(animals);
int index = getInt("Wich animal do you want to delete 0 - " + (animals.size() - 1), 0, animals.size() - 1);
animals.remove(index);
return animals;
}
/**
* Method to add a new animal to the list
*
* @param animals input Arraylist<String>
* @return output Arraylist<String>
*/
private static ArrayList<String> addAnimal(ArrayList<String> animals) {
String animal = getString("Write an animal");
System.out.println("The actual list has " + animals.size() + " animals. Where do you want to add this new one?");
System.out.println();
int position = getInt("Write an number between 0 - " + animals.size(), 0, animals.size());
animals.add(position, animal);
return animals;
}
/**
* Method which gets a String from the keyboard
*
* @param message
* @return
*/
private static String getString(String message) {
Scanner read = new Scanner(System.in);
String value;
System.out.println(message);
value = read.next();
read.nextLine();
read.close();
return value;
}
/**
* This method prints all the animals in the ArrayList
*
* @param animals input Arraylist<String>
*/
private static void printAnimals(ArrayList<String> animals) {
for (String animal : animals) {
System.out.println(animal);
}
}
/**
* This method gets an int value with error control between min and max value
*
* @param message message to be shown to the user
* @param min min accepted value
* @param max max accepted value
* @return returns the read int
*/
private static int getInt(String message, int min, int max) {
Scanner read = new Scanner(System.in);
int x = 0;
boolean correctDataType = false;
do {
System.out.println(message);
correctDataType = read.hasNextInt();
if (!correctDataType) {
System.out.println("ERROR: Not an int value.");
read.nextLine();
} else { // Tinc un enter
x = read.nextInt();
read.nextLine();
if (x < min || x > max) {
System.out.println("Not a valid option.");
correctDataType = false;
}
}
} while (!correctDataType);
return x;
}
/**
* This method initializes the ArrayList with some values.
*
* @param animals input ArrayList<String>
* @return Returns the ArrayList filled with some values.
*/
private static ArrayList<String> initAnimals(ArrayList<String> animals) {
animals.add("Lion");
animals.add("Tiger");
animals.add("Elephant");
animals.add("Gorilla");
return animals;
}
}