Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
COM4005_Charlotte_Kerr
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
charlotte.kerr
COM4005_Charlotte_Kerr
Commits
72c12375
Commit
72c12375
authored
May 03, 2021
by
charlotte.kerr
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
bdcbdde9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
150 additions
and
0 deletions
+150
-0
Main.java
Main.java
+150
-0
No files found.
Main.java
0 → 100644
View file @
72c12375
import
java.util.ArrayList
;
// So that ArrayLists can be implemented
import
java.util.Scanner
;
// So that a scanner can be implemented
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
// TODO Auto-generated method stub
Scanner
scan
=
new
Scanner
(
System
.
in
);
// Creates a scanner to take user inputs
String
animal_choice
;
// To store the user input for an animal
int
menu_choice
;
// To store the user input for which task they want to perform
int
yes_no_choice
;
// Stores the user's input for yes/no queries
boolean
selecting_done
=
false
;
// Allows us to control when we are done adding animals to a list
boolean
active
=
true
;
// Allows us to control when the program ends
while
(
active
==
true
)
{
// For as long as we want the program to be active, the following loop will run:
// ArrayLists
ArrayList
<
Animal
>
all_animals
=
new
ArrayList
<
Animal
>();
// Will store all animals in an ArrayList
ArrayList
<
Animal
>
animals_of_type
=
new
ArrayList
<
Animal
>();
// Will store all animals of a certain type
ArrayList
<
Animal
>
animals_custom
=
new
ArrayList
<
Animal
>();
// Will store specific animals according to the user's choice
// Adding animals to the zoo
all_animals
.
add
(
new
Adder
(
"Adam"
));
all_animals
.
add
(
new
Adder
(
"Annie"
));
all_animals
.
add
(
new
Goldfish
(
"Gertrude"
));
all_animals
.
add
(
new
Goldfish
(
"Gibson"
));
all_animals
.
add
(
new
Iguana
(
"Iggy"
));
all_animals
.
add
(
new
Lion
(
"Leo"
));
all_animals
.
add
(
new
Lion
(
"Lisa"
));
all_animals
.
add
(
new
Narwhal
(
"Napoleon"
));
all_animals
.
add
(
new
Narwhal
(
"Nessie"
));
all_animals
.
add
(
new
Octopus
(
"Olly"
));
all_animals
.
add
(
new
Owl
(
"Oscar"
));
all_animals
.
add
(
new
Owl
(
"Olivia"
));
all_animals
.
add
(
new
Roadrunner
(
"Ryan"
));
// Displays a basic menu to the user
System
.
out
.
println
();
System
.
out
.
println
(
"Select a task: "
+
"\n1: Display all animals in the zoo"
+
"\n2: Display a single animal's details"
+
"\n3: Display the details of a list of animals"
+
"\n4: Generate a list of animals by type"
+
"\n5: Display required food for a list of animals"
+
"\n6: Display a list of animals by habitat"
+
"\n7: Exit the system"
);
menu_choice
=
scan
.
nextInt
();
// Takes the user's input from the menu
if
((
menu_choice
==
3
)
||
(
menu_choice
==
5
))
{
// If the user is forming a specific list of animals
while
(
selecting_done
==
false
)
{
System
.
out
.
println
(
"Enter the name of an animal to add to your list:"
);
animal_choice
=
scan
.
next
();
//
for
(
int
i
=
0
;
i
<
all_animals
.
size
();
i
++)
{
// Cycles through the list of animals, checking each one throughout
if
(
animal_choice
.
equals
(
all_animals
.
get
(
i
).
name
))
{
// If the animal is in the zoo
animals_custom
.
add
(
all_animals
.
get
(
i
));
// Adds the animal of that name to the custom ArrayList
}
}
System
.
out
.
println
(
"Are you finished adding to the list?"
+
"\n1: Yes"
+
"\n2: No"
);
yes_no_choice
=
scan
.
nextInt
();
if
(
yes_no_choice
==
1
)
{
selecting_done
=
true
;
// User selection is finished; this will end the loop and stop them from adding to the list
}
}
}
// End of custom list generation
switch
(
menu_choice
)
{
case
1
:
// The user wants to display all animals
for
(
int
i
=
0
;
i
<
all_animals
.
size
();
i
++)
{
// Cycles through each element in the ArrayList containing all animals.
all_animals
.
get
(
i
).
displayInfo
();
// Calls the function to display all info for the selected animal.
}
System
.
out
.
println
();
break
;
case
2
:
// The user wants the details of a single animal
System
.
out
.
println
(
"Enter the name of the animal you would like to see the details of:"
);
animal_choice
=
scan
.
next
();
for
(
int
i
=
0
;
i
<
all_animals
.
size
();
i
++)
{
if
(
animal_choice
.
equals
(
all_animals
.
get
(
i
).
name
))
{
// Cycles through each element in the ArrayList containing all animals.
all_animals
.
get
(
i
).
displayInfo
();
// Calls the function to display all info for the selected animal.
}
}
break
;
case
3
:
// The user wants to display the details of a list of animals
for
(
int
i
=
0
;
i
<
animals_custom
.
size
();
i
++)
{
// Cycles through each element in the ArrayList containing the selection of animals.
animals_custom
.
get
(
i
).
displayInfo
();
// Calls the function to display all info from the list of animals.
}
break
;
case
4
:
// The user wants a list of animals by their type
System
.
out
.
println
(
"Enter the type of animal you would like to view: (Bird, Fish, Mammal or Reptile)"
);
animal_choice
=
scan
.
next
();
for
(
int
i
=
0
;
i
<
all_animals
.
size
();
i
++)
{
// Cycles through the list of all animals
if
(
animal_choice
.
equals
(
all_animals
.
get
(
i
).
classification
))
{
// Where the animal's classification matches the one selected
animals_of_type
.
add
(
all_animals
.
get
(
i
));
// The animal will be added to the new ArrayList
}
}
System
.
out
.
println
(
"The following animals are of the type you selected:"
);
for
(
int
i
=
0
;
i
<
animals_of_type
.
size
();
i
++)
{
// Cycles through the list of animals organised by type
System
.
out
.
println
(
animals_of_type
.
get
(
i
).
name
);
// Displays the names only of the animals in this ArrayList
}
break
;
case
5
:
// The user wants to display food requirements for a list of animals
for
(
int
i
=
0
;
i
<
animals_custom
.
size
();
i
++)
{
System
.
out
.
println
(
animals_custom
.
get
(
i
).
name
+
" eats "
+
animals_custom
.
get
(
i
).
diet
+
"."
);
}
break
;
case
6
:
// The user wants to display a list of animals by their environment
System
.
out
.
println
(
"Select an environment to view from: (forest, freshwater, desert, savannah, ocean)"
);
animal_choice
=
scan
.
next
();
for
(
int
i
=
0
;
i
<
all_animals
.
size
();
i
++)
{
if
(
animal_choice
.
equals
(
all_animals
.
get
(
i
).
habitat
))
{
// Where the animal's habitat matches the one selected
animals_of_type
.
add
(
all_animals
.
get
(
i
));
// The animal will be added to the new ArrayList
}
}
System
.
out
.
println
(
"The following animals live within the habitat you selected:"
);
for
(
int
i
=
0
;
i
<
animals_of_type
.
size
();
i
++)
{
// Cycles through the list of animals organised by type
System
.
out
.
println
(
animals_of_type
.
get
(
i
).
name
);
// Displays the names only of the animals in this ArrayList
}
break
;
case
7
:
// The user wants to exit the system
active
=
false
;
break
;
}
// End of case switch
animals_custom
.
clear
();
// Wipes the custom list clear to ensure that it can be re-entered if need be
animals_of_type
.
clear
();
// Wipes the type list clear to ensure that it can be re-entered if need be
}
// End of the while loop
}
// End of the Main function
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment