Commit 695b95a6 authored by elliot.copeland's avatar elliot.copeland

Assignment proof checked

parents
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}
################################################################################################################################################################################################
Just incase you aren't already, please make sure you run the program (App.java) through the terminal with the 'Code Runner for Java' plugin, for some reason you can't input values with the Scanner class if it's not through the terminal, cheers :)
################################################################################################################################################################################################
\ No newline at end of file
File added
File added
File added
abstract class Animal {
protected String species;
protected String habitat;
protected String food;
protected String name;
protected String aClass;
protected String description;
protected String[] attributes = {name, species, habitat, food, aClass, description};
abstract String pet();
public String getProfile() {
return (""
+ "\n###################################"
+ "\n"
+ "\nName: " + name
+ "\nSpecies: " + species
+ "\nHabitat: " + habitat
+ "\nFood: " + food
+ "\nAnimal Class: " + aClass
+ "\nDescription: " + description);
}
}
File added
import java.util.Scanner;
import java.util.ArrayList;
public class App {
ArrayList<Animal> animalList = new ArrayList<Animal>();
public static void main(String[] args) throws Exception {
// Please run this program through the terminal with the 'Code Runner for Java' plugin, user input doesn't seem to work through regular vscode :/ //
App zooApp;
zooApp = new App();
Animal testAnimal = new Fish("Carp", "river", "Plankton", "Harry", "Green");
zooApp.animalList.add(testAnimal);
testAnimal = new Avian("Eagle", "forest", "Rodents", "Joseph", "Brown", "Yellow");
zooApp.animalList.add(testAnimal);
testAnimal = new Mammal("Dingo", "desert", "Rodents", "kevin", "Light Brown");
zooApp.animalList.add(testAnimal);
zooApp.menu();
}
public void menu() {
// Main Menu //
System.out.println("");
System.out.println("##################### ZOO MENU #####################");
System.out.println("\n"
+ "\n [1] View full list of animals "
+ "\n [2] Add an animal"
+ "\n [3] remove an animal"
+ "\n [4] Filter search list of animals"
+ "\n [5] Calculate food needs"
+ "\n [6] Exit program"
+ "\n"
+ "\n####################################################"
+ "\n");
System.out.print(" Please enter an option (1 - 5): ");
Scanner inputScan = new Scanner(System.in);
String usrInput = inputScan.nextLine();
if (usrInput.equals("1")) {
viewList();
}
else if (usrInput.equals("2")) {
addAnimal();
}
else if (usrInput.equals("3")) {
removeAnimal();
}
else if (usrInput.equals("4")) {
filterList();
}
else if (usrInput.equals("5")) {
filterFood();
}
else if (usrInput.equals("6")) {
inputScan.close();
System.exit(0);
}
else {
System.out.println(" Illegal input, returning to menu... ");
menu();
}
}
public void viewList() {
// Displays the full animal list //
System.out.println("");
System.out.println("####################################################");
System.out.println("");
int listLength = animalList.size();
for (Animal i : animalList) {
System.out.println(" - " + i.species + ", " + i.habitat + ", (" + i.name +")");
}
System.out.println("");
System.out.println("\n"
+ "\n [1] View an individual animal's details"
+ "\n [2] View all animals details"
+ "\n [3] Return to menu");
System.out.print("\n Please enter an option (1-3): ");
Scanner inputScan = new Scanner(System.in);
String usrInput = inputScan.nextLine();
if (usrInput.equals("1")) {
System.out.println("");
System.out.print(" Enter the animal's list number: ");
usrInput = inputScan.nextLine();
int usrInt = Integer.parseInt(usrInput);
if (( usrInt >= 0) && (usrInt <= listLength - 1)) {
String listString = animalList.get(usrInt).getProfile();
System.out.println(listString);
menu();
}
else {
System.out.println(" Illegal input, returning to menu... ");
menu();
}
}
else if (usrInput.equals("2")) {
for (Animal i : animalList) {
System.out.println(i.getProfile());
}
menu();
}
else if (usrInput.equals("3")) {
menu();
}
else {
System.out.println(" Illegal input, returning to menu... ");
menu();
}
}
public void addAnimal() {
//Allows the user to add an animal to the list //
System.out.println("");
Scanner inputScan = new Scanner(System.in);
System.out.println("");
System.out.print(" Enter the species: ");
String inputSpecies = inputScan.nextLine();
System.out.print(" Enter the habitat: ");
String inputHabitat = inputScan.nextLine().toLowerCase();
System.out.print(" Enter the Food: ");
String inputFood = inputScan.nextLine();
System.out.print(" Name the animal: ");
String inputName = inputScan.nextLine();
System.out.println("\n"
+ "\n [1] Add an Avian"
+ "\n [2] Add a Fish"
+ "\n [3] Add a Mammal"
+ "\n [4] Add a Reptile");
System.out.print("\n Please enter an option (1-4): ");
String usrInput = inputScan.nextLine();
if (usrInput.equals("1")) {
System.out.print(" Enter the Feather Colour: ");
String inputFColour = inputScan.nextLine();
System.out.print(" Enter the Keratin Colour: ");
String inputKColour = inputScan.nextLine();
Animal anml = new Avian(inputSpecies, inputHabitat, inputFood, inputName, inputFColour, inputKColour);
animalList.add(anml);
}
else if (usrInput.equals("2")) {
System.out.print(" Enter the Scale Colour: ");
String inputSCol = inputScan.nextLine();
Animal anml = new Fish(inputSpecies, inputHabitat, inputFood, inputName, inputSCol);
animalList.add(anml);
}
else if (usrInput.equals("3")) {
System.out.print(" Enter the Fur Colour: ");
String inputFCol = inputScan.nextLine();
Animal anml = new Mammal(inputSpecies, inputHabitat, inputFood, inputName, inputFCol);
animalList.add(anml);
}
else if (usrInput.equals("4")) {
System.out.print(" Venomous Capability: ");
String inputVeno = inputScan.nextLine();
Animal anml = new Reptile(inputSpecies, inputHabitat, inputFood, inputName, inputVeno);
animalList.add(anml);
}
else {
System.out.println(" Illegal input, returning to menu... ");
menu();
}
System.out.println(" Animal added, returning to menu...");
menu();
}
public void removeAnimal() {
// Allows the user to remove an animal from the list //
System.out.println("");
System.out.println("####################################################");
System.out.println("");
int listLength = animalList.size();
for (Animal i : animalList) {
System.out.println(" - " + i.species + ", " + i.habitat + ", (" + i.name +")");
}
System.out.println("");
System.out.print(" Enter the animal's list number to remove: ");
Scanner inputScan = new Scanner(System.in);
String usrInput = inputScan.nextLine();
int usrInt = Integer.parseInt(usrInput);
if (( usrInt >= 0) && (usrInt <= listLength - 1)) {
animalList.remove(usrInt);
System.out.println(" Animal removed, returning to the menu... ");
}
else {
System.out.println(" Illegal input, returning to menu... ");
menu();
}
menu();
}
public void filterList() {
// Allows the user to search the list wiht specific filters //
System.out.println("\n"
+ "\n [1] Filter by Class"
+ "\n [2] Filter by Habitat"
+ "\n [3] Return to Menu"
+ "\n Please enter an option (1-3): ");
Scanner inputScan = new Scanner(System.in);
String usrInput = inputScan.nextLine();
if (usrInput.equals("1")) {
System.out.println("");
System.out.print(" Enter the class to sort by (avian, fish, mammal or reptile): ");
usrInput = inputScan.nextLine().toLowerCase();
for (Animal i : animalList) {
if (i.aClass.equals(usrInput)) {
System.out.println(" - " + i.species + ", " + i.habitat + ", (" + i.name +")");
}
}
}
else if (usrInput.equals("2")) {
System.out.println("");
System.out.print(" Enter the habitat to sort by: ");
usrInput = inputScan.nextLine().toLowerCase();
for (Animal i : animalList) {
if (i.habitat.equals(usrInput)) {
System.out.println(" - " + i.species + ", " + i.habitat + ", (" + i.name +")");
}
}
}
else if (usrInput.equals("3")) {
menu();
}
else {
System.out.println(" Illegal input, returning to menu... ");
menu();
}
menu();
}
public void filterFood() {
// Shows the user the food they need to buy //
for (Animal i : animalList) {
System.out.println(i.food + " -is required for " + i.species + ", (" + i.name + ")");
}
menu();
}
}
\ No newline at end of file
File added
public class Avian extends Animal{
protected String featherColour;
protected String keratinColour;
public Avian(String tSpecies, String tHabitat, String tFood, String tName, String fColour, String kColour) {
species = tSpecies;
habitat = tHabitat;
food = tFood;
aClass = "avian";
name = tName;
description = "";
featherColour = fColour;
keratinColour = kColour;
}
@Override
public String getProfile() {
if (description.equals("")){
description = "A beautiful " + featherColour + " " + species + " with " + keratinColour + " claws. They live in the " + habitat + " and feed on " + food + ".";
}
return (super.getProfile() + "\nFeather Colour: " + featherColour
+ "\nKeratin Colour: " + keratinColour
+ "\n###################################");
}
public String pet() {
String response;
if (name.equals("Name not given")) {
response = "The " + species + " accepts your pats.";
}
else {
response = name + " rumbles affectionally.";
}
return response;
}
}
File added
public class Fish extends Animal{
protected String scaleColour = "Scale Colour not provided";
public Fish(String tSpecies, String tHabitat, String tFood, String tName, String sColour ) {
species = tSpecies;
habitat = tHabitat;
food = tFood;
aClass = "fish";
name = tName;
description = "";
scaleColour = sColour;
}
@Override
public String getProfile() {
if (description.equals("")){
description = "A majestic " + scaleColour + " " + species + ". They live in the " + habitat + " and feed on " + food + ".";
}
return (super.getProfile() + "\nScale Colour: " + scaleColour
+ "\n###################################");
}
public String pet() {
String response;
if (name.equals("Name not given")) {
response = "The " + species + " swims in circles.";
}
else {
response = name + " playfully splashes you with water.";
}
return response;
}
}
public class Mammal extends Animal {
protected String furColour = "Fur colour not given";
public Mammal(String tSpecies, String tHabitat, String tFood, String tName, String fColour ) {
species = tSpecies;
habitat = tHabitat;
food = tFood;
aClass = "mammal";
name = tName;
description = "";
furColour = fColour;
}
@Override
public String getProfile() {
if (description.equals("")){
description = "An imtimidating " + furColour + " " + species + ". They live in the " + habitat + " and feed on " + food + ".";
}
return (super.getProfile() + "\nFur Colour: " + furColour
+ "\n###################################");
}
public String pet() {
String response;
if (name.equals("Name not given")) {
response = "The " + species + " looks happier now.";
}
else {
response = name + " chirps happily.";
}
return response;
}
}
\ No newline at end of file
public class Reptile extends Animal {
protected String venomous = "n/a";
public Reptile(String tSpecies, String tHabitat, String tFood, String tName, String veno ) {
species = tSpecies;
habitat = tHabitat;
food = tFood;
aClass = "reptile";
name = tName;
description = "";
venomous = veno;
}
@Override
public String getProfile() {
if (description.equals("")){
description = "An elusive " + species + ". They live in the " + habitat + " and feed on " + food + ". (Venomous rating: " + venomous + ").";
}
return (super.getProfile() + "\nVenomous?: " + venomous
+ "\n###################################");
}
public String pet() {
String response;
if (name.equals("Name not given")) {
response = "The " + species + " looks happier now.";
}
else {
response = name + " chirps happily.";
}
return response;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment