SImple Java
java_arraylist_0.java
java_arraylist_0.java
/*
* Aleksandr Kovalchuk
* PRG / 421
* June 20, 2016
* =============================================================================================================
*
* This is a java program to demonstrate the use of ArrayList.
* The program allows a user to do the following;
* 1. Add, edit, delete different types of animals
* 2. Select an animal, and the corresponding characteristics will be displayed (such as color, vertebrate or invertebrate, can swim, etc.)
*
* =============================================================================================================
*/
import
java
.
util
.
*
;
// import statements
import
java
.
util
.
Scanner
;
public
class
Animals
{
public
static
Scanner
scan
=
new
Scanner
(
System
.
in
);
// enable user keyboard input
public
static
void
main
(
String
[]
args
)
{
ArrayList
<
Animal
>
animals
=
new
ArrayList
<
Animal
>
();
// create array list
char
another
=
'Y'
;
// Check if user wants to perform another operation
int
exitCode
=
-
1
;
// Program exits upon entering -1 as choice
String
[]
operations
=
new
String
[]{
"0. Add Animals"
,
"1. Update Animal"
,
"2. Remove Animal"
};
// Declare and initialize array to hold operation choices
System
.
out
.
println
(
"Animals Array List\n=========================================="
);
// Title
while
(
another
==
'Y'
)
{
System
.
out
.
println
(
"Please select an operation: "
);
// Prompt user for operation
for
(
int
i
=
0
;
i
<
operations
.
length
;
i
++
)
{
System
.
out
.
println
(
operations
[
i
]);
// Display operation options
}
System
.
out
.
println
(
"Enter "
+
exitCode
+
" to exit.\n-------------------------------------------"
);
// Inform that -1 exits program
int
userChoice
=
scan
.
nextInt
();
// Get user input
if
(
userChoice
==
0
)
// Check choice and call appropriate method
{
addAnimal
(
animals
);
// Call add animals method
}
else
if
(
userChoice
==
1
)
{
updateAnimal
(
animals
);
// Call update animals method
}
else
if
(
userChoice
==
2
)
{
removeAnimal
(
animals
);
// Call remove animals method
}
else
if
(
userChoice
==
exitCode
)
{
System
.
out
.
print
(
"Thank you for your time. Goodbye."
);
// exit message
System
.
exit
(
0
);
}
System
.
out
.
print
(
"Perform another Operation? (Y/N)"
);
// Ask if user wishes to perform another operation
String
choice
=
scan
.
next
();
choice
=
choice
.
toUpperCase
();
another
=
choice
.
charAt
(
0
);
}
System
.
out
.
println
(
"\n-------------------------------------------\nThe array list has the following animals: \n"
+
animals
.
toString
()
+
"\n-------------------------------------------"
);
// If no other operation, display elements in array list
}
// Function to add animal to array list
public
static
void
addAnimal
(
ArrayList
<
Animal
>
anim
)
{
char
stop
=
'Y'
;
String
name
,
color
,
canSwim
,
hasBackbone
;
// Properties of animal object from Animal class
int
code
;
while
(
stop
==
'Y'
)
{
System
.
out
.
print
(
"Animal Code: "
);
// prompt user for input of property
code
=
scan
.
nextInt
();
// Assign property to a variable
System
.
out
.
print
(
"Name: "
);
name
=
scan
.
next
();
System
.
out
.
print
(
"Color: "
);
color
=
scan
.
next
();
System
.
out
.
print
(
"Can Swim?(Y/N): "
);
canSwim
=
scan
.
next
();
System
.
out
.
print
(
"Has Backbone?(Y/N): "
);
hasBackbone
=
scan
.
next
();
anim
.
add
(
new
Animal
(
name
,
color
,
canSwim
,
hasBackbone
,
code
));
// Add animal element to array list
System
.
out
.
print
(
"-------------------------------------------\n"
);
System
.
out
.
print
(
"Add Another Animal? (Y/N)"
);
// Check if user wishes to add another animal
String
choice
=
scan
.
next
();
choice
=
choice
.
toUpperCase
();
stop
=
choice
.
charAt
(
0
);
}
// Display animals in the array list after quitting
System
.
out
.
println
(
"The following animals have been added: \n"
+
anim
.
toString
()
+
"\n-------------------------------------------"
);
}
// Function to edit animal to array list
public
static
void
updateAnimal
(
ArrayList
<
Animal
>
anim
)
{
char
stop
=
'Y'
;
String
name
,
color
,
canSwim
,
hasBackbone
;
// animal properties to be updated
if
(
!
anim
.
isEmpty
())
// First check is array list is not empty then proceed
{
while
(
stop
==
'Y'
)
// while user wishes to update more animals
{
System
.
out
.
print
(
"Enter Animal Code: "
);
// Prompt user for animal code
int
cd
=
scan
.
nextInt
();
// Capture code from scanner input
for
(
Animal
anm
:
anim
)
// Loop through array list
{
if
(
anm
.
getCode
()
>
0
&&
anm
.
getCode
()
==
cd
)
// Pull element with the given code
{
System
.
out
.
print
(
"Name: "
);
// prompt user for new details of animal
name
=
scan
.
next
();
// capture details
System
.
out
.
print
(
"Color: "
);
color
=
scan
.
next
();
System
.
out
.
print
(
"Can Swim?(Y/N): "
);
canSwim
=
scan
.
next
();
System
.
out
.
print
(
"Has Backbone?(Y/N): "
);
hasBackbone
=
scan
.
next
();
anm
.
setName
(
name
);
// For each property, update with the new information
anm
.
setColor
(
color
);
anm
.
setSwim
(
canSwim
);
anm
.
setBackbone
(
hasBackbone
);
}
else
{
System
.
out
.
print
(
"No matching code found."
);
// Inform user if no record was found
}
}
System
.
out
.
print
(
"Update Another Animal? (Y/N)"
);
// ask user if they wish to update another animal
String
choice
=
scan
.
next
();
choice
=
choice
.
toUpperCase
();
stop
=
choice
.
charAt
(
0
);
}
// Display elements in array list if user is done updating
System
.
out
.
println
(
"The array list has the following animals: \n"
+
anim
.
toString
()
+
"\n-------------------------------------------"
);
}
else
{
System
.
out
.
println
(
"No animals were found in array list"
);
// Inform user if array list has no animal elements
}
}
// Function to remove animal to array list
public
static
void
removeAnimal
(
ArrayList
<
Animal
>
anim
)
{
char
stop
=
'Y'
;
if
(
!
anim
.
isEmpty
())
// First check that there are animals in the array list
{
while
(
stop
==
'Y'
)
{
System
.
out
.
print
(
"Enter Animal Code: "
);
// prompt user for animal code
int
cd
=
scan
.
nextInt
();
Iterator
<
Animal
>
itr
=
anim
.
iterator
();
// Declare iterator to iterate through array list
while
(
itr
.
hasNext
())
// Check that there are elements using iterator
{
if
(
itr
.
next
().
getCode
()
==
cd
)
// search for element with the given animal code
{
itr
.
remove
();
// remove the found element
System
.
out
.
print
(
"Animal with code "
+
cd
+
" successfully removed."
);
// success message
}
else
{
System
.
out
.
println
(
"No matching code found."
);
// no matching animal found
}
}
System
.
out
.
print
(
"\nRemove Another Animal? (Y/N)"
);
// ask if user wants to remove another animal
String
choice
=
scan
.
next
();
choice
=
choice
.
toUpperCase
();
stop
=
choice
.
charAt
(
0
);
}
// Display array list
System
.
out
.
println
(
"The array list has the following animals: \n"
+
anim
.
toString
()
+
"\n-------------------------------------------"
);
}
else
{
System
.
out
.
println
(
"No animals were found in array list"
);
// No elements in array
}
}
}
/**
*
@author
* Name:
* Course Title:
* Date:
* =============================================================================================================
*/
public
class
Animal
// Animal class to hold properties of animal
{
public
static
String
name
,
color
,
canSwim
,
hasBackbone
;
// properties declaration
int
code
;
public
Animal
(
String
Name
,
String
Color
,
String
CanSwim
,
String
HasBackbone
,
int
Code
)
// constructor
{
name
=
Name
;
color
=
Color
;
canSwim
=
CanSwim
;
hasBackbone
=
HasBackbone
;
code
=
Code
;
}
public
String
toString
()
// override toString method to display elements in array list
{
return
"\n Animal Code: "
+
code
+
"\n Name: "
+
name
+
"\n Color: "
+
color
+
"\n Can Swim: "
+
canSwim
+
"\n Vertebrate: "
+
hasBackbone
;
}
// Getters
public
int
getCode
()
// get animal code
{
return
this
.
code
;
}
public
String
getName
()
// get animal name
{
return
name
;
}
public
String
getColor
()
// get animal color
{
return
color
;
}
public
String
getSwim
()
// get animal swim value
{
if
(
canSwim
.
equalsIgnoreCase
(
"Y"
))
return
"Yes"
;
else
return
"No"
;
}
public
String
getBackbone
()
// get animal backbone value
{
if
(
hasBackbone
.
equalsIgnoreCase
(
"Y"
))
return
"Yes"
;
else
return
"No"
;
}
// Setters
public
int
setCode
(
int
cd
)
// set animal code for update
{
return
cd
;
}
public
void
setName
(
String
nm
)
// set animal name for update
{
name
=
nm
;
}
public
void
setColor
(
String
clr
)
// set animal color for update
{
color
=
clr
;
}
public
void
setSwim
(
String
swim
)
// set animal can swim value for update
{
canSwim
=
swim
;
}
public
void
setBackbone
(
String
bckbn
)
// set animal has backbone value for update
{
hasBackbone
=
bckbn
;
}
}