Commit 971cd66d authored by Gabriel Penman's avatar Gabriel Penman

zoo application

parents
File added
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).
File added
File added
File added
File added
File added
File added
File added
panda,apple,2,banana,2,bamboo,96
rodent,peanut,15,humanflesh,15,grain,50,apple,10,banana,10
ant,apple,20,banana,20,grain,20,honey,20,peanut,20
parrot,worms,30,seeds,20,apple,20,grain,20,banana,10
\ No newline at end of file
apple,53
banana,43
peanut,120
bamboo,2
humanflesh,130
grain,30
honey,23
worms,90
seeds,40
\ No newline at end of file
desert,50
ice,0
grassland,30
urban,15
marsh,25
jungle,35
\ No newline at end of file
Diet.csv goes in format food, percentage, food, percentage, food, percentage
\ No newline at end of file
import java.util.Arrays;
public abstract class Animal {
private String givenName;
private String commonName;
private Integer age;
private Boolean sex;
private String[] taxonomy = {"","","","","","","",""};
private Diet diet;
private double baseRCI;
private Habitat habitat;
public String getGivenName() {
return givenName;
}
public String getCommonName() {return this.commonName;}
public Integer getAge() {return this.age;}
public Boolean getSex() {return this.sex;}
public Habitat getHabitat() {return habitat;}
public String getSexString() {
if (this.getSex()) {
return "Male";
} else {
return "Female";
}
}
public String[] getTaxonomy() {return this.taxonomy;}
public Diet getDiet() {return this.diet;}
public String getTaxonomyString() {
return Arrays.toString(this.getTaxonomy());
}
public double getBaseRCI() {
return this.baseRCI;
}
public double getWeeklyBaseRCI() {
return this.baseRCI*7;
}
public double getCalories() {
return this.baseRCI;
}
public double getWeeklyCalories(){
return this.getCalories()*7;
}
public void setGivenName(String givenName) {
this.givenName = givenName;
}
public void setCommonName(String commonName) {this.commonName = commonName;}
public void setAge(Integer age) {this.age = age;}
public void setSex(Boolean sex) {this.sex = sex;}
public void setTaxonomy(String[] taxonomy) {this.taxonomy = taxonomy;}
public void setDiet(Diet diet) {this.diet = diet;}
public void setBaseRCI(double baseRCI) {this.baseRCI = baseRCI;}
public void setHabitat(Habitat habitat) {this.habitat = habitat;}
public Animal(String commonName, Integer age, Boolean sex, String[] taxonomy, Diet diet, double baseRCI, Habitat habitat, String givenName) {
this.setCommonName(commonName);
this.setAge(age);
this.setSex(sex);
this.setTaxonomy(taxonomy);
this.setDiet(diet);
this.setBaseRCI(baseRCI);
this.setHabitat(habitat);
this.setGivenName(givenName);
}
public Animal(String commonName, Integer age, Boolean sex, String[] taxonomy, Diet diet, double baseRCI, Habitat habitat) {
this.setCommonName(commonName);
this.setAge(age);
this.setSex(sex);
this.setTaxonomy(taxonomy);
this.setDiet(diet);
this.setBaseRCI(baseRCI);
this.setHabitat(habitat);
this.setGivenName("Unnamed");
}
public final String basicDetails() {
String string = "Given name: "+this.getGivenName()+"\n"+
"Common name: "+this.getCommonName()+"\n"+
"Age: "+this.getAge()+"\n"+
"Sex: "+this.getSexString()+"\n"+
"Habitat: "+this.getHabitat()+"\n"+
"RCI: "+this.getBaseRCI()+"\n";
return string;
}
public final String taxonomyAndDiet() {
String string = "Taxonomy:\n\n-- Kingdom -- Phylum -- Class -- Order -- Family -- Genus -- Species --\n"+this.getTaxonomyString()+"\n\n"+
"Diet: "+this.getDiet().toString();
return string;
}
public abstract String additionalDetails();
public final String details() {
return this.basicDetails()+this.additionalDetails()+this.taxonomyAndDiet();
}
}
File added
public class App {
public static void main(String[] args) throws Exception {
System.out.println("Initilizing!\n");
Storage storage = new Storage("/Users/Penman/Desktop/zoo/lib/food.csv", "/Users/Penman/Desktop/zoo/lib/habitat.csv", "/Users/Penman/Desktop/zoo/lib/diet.csv");
Zoo zoo = new Zoo();
zoo.allAnimalsAdd( new _Panda("Gabriel",20,true, storage));
zoo.allAnimalsAdd( new _Panda("Sarah",21,false, storage));
_Panda littleJim = new _Panda("Little Jim",2,true, storage);
littleJim.setBaseRCI(400);
zoo.allAnimalsAdd(littleJim);
zoo.allAnimalsAdd( new _Rat("Kieran",2,true, storage));
zoo.allAnimalsAdd( new _Rat("Mary",1,false, storage));
zoo.allAnimalsAdd( new _Capybara("Leah",12,false, storage));
zoo.allAnimalsAdd( new _Capybara("Robert",16,true, storage));
zoo.allAnimalsAdd( new _Capybara("Jack",32,true, storage));
// Polymorphic ants
zoo.allAnimalsAdd( new _Leafcutter(false,"Queen",storage));
for (int i = 0; i < 129; i++) {
zoo.allAnimalsAdd( new _Leafcutter(false,"Worker",storage));
}
for (int i = 0; i < 12; i++) {
zoo.allAnimalsAdd( new _Leafcutter(false,"Soldier",storage));
}
zoo.allAnimalsAdd(new _ScarletMacaw("Jules", 20, false, 1.3, storage));
zoo.allAnimalsAdd(new _ScarletMacaw("Kitty", 21, true, 1.5, storage));
zoo.allAnimalsAdd(new _GreyParrot("Jojo", 9, true, 1.2, storage));
zoo.allAnimalsAdd(new _GreyParrot("Elaine", 43, true, 1.1, storage));
Menu menu = new Menu();
menu.main(zoo);
}
}
\ No newline at end of file
File added
import java.util.ArrayList;
public class Diet {
private final String NAME;
private final ArrayList<DietItem> DIET;
public String getName() {
return this.NAME;
}
public ArrayList<DietItem> getContent() {
return this.DIET;
}
private Boolean checkDiet(ArrayList<DietItem> diet) {
//Checks to see if the percentages match up
double total = 0;
for (DietItem item : diet) {
total = total+item.getDietPercentage();
}
if (total == 100) {
return true;
} else {
return false;
}
}
public Diet(String name, DietItem... args) throws Exception {
this.NAME = name;
ArrayList<DietItem> diet = new ArrayList<DietItem>();
//Adds all DietItem args to an arraylist called diet
for (DietItem arg : args) {
diet.add(arg);
}
//Checks to see if the percentages match up
if (this.checkDiet(diet)) {
//Sets constant diet to the new diet
this.DIET = diet;
} else {
throw new Exception("DIETPERCENTAGE of all dietItems does not total to 100");
}
}
public Diet(String name, ArrayList<DietItem> diet) throws Exception {
this.NAME = name;
if (this.checkDiet(diet)) {
this.DIET = diet;
} else {
throw new Exception("DIETPERCENTAGE of all dietItems does not total to 100");
}
}
public String toString() {
String string = this.getName().substring(0, 1).toUpperCase() + this.getName().substring(1);
for (DietItem dietItem : this.getContent()) {
string += "\n "+dietItem.toString();
}
return string;
}
}
public class DietItem {
private final Food FOOD;
private final double DIETPERCENTAGE;
public Food getFood() {
return this.FOOD;
}
public double getDietPercentage() {
return this.DIETPERCENTAGE;
}
public DietItem(Food food, double dietPercentage) {
this.FOOD = food;
this.DIETPERCENTAGE = dietPercentage; // Calories per kilogram
}
public String toString() {
return this.getFood().toString()+" | Percentage of diet: "+this.getDietPercentage()+"%";
}
}
File added
public class Food {
private final String NAME;
private final double ENERGYDENSITY; // Calories per kilogram
private double amount = 0;
public double getEnergyDensity() {
return this.ENERGYDENSITY;
}
public String getName(){
return this.NAME;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public Food(String name, double energyDensity) {
this.NAME = name;
this.ENERGYDENSITY = energyDensity;
}
public Food(Food food) {
this.NAME = food.getName();
this.ENERGYDENSITY = food.getEnergyDensity();
this.amount = food.getAmount();
}
public String toString() {
String string;
if (this.getAmount() != 0) {
string = "Name: "+this.getName().substring(0, 1).toUpperCase() + this.getName().substring(1)+" | Amount: "+Math.round(this.getAmount())+"kg";
} else {
string = "Name: "+this.getName().substring(0, 1).toUpperCase() + this.getName().substring(1)+" | Energy density: "+this.getEnergyDensity()+"cal/kg";
}
return string;
}
}
public class Habitat {
private final String name;
private double averageTemp;
public String getName(){
return this.name;
}
public double getAverageTemp() {
return averageTemp;
}
public void setAverageTemp(double averageTemp) {
this.averageTemp = averageTemp;
}
public Habitat(String name, double averageTemp) {
this.name = name;
this.averageTemp = averageTemp;
}
public String toString() {
String string;
string = "Name: "+this.getName().substring(0, 1).toUpperCase() + this.getName().substring(1)+" | AverageTemperature: "+this.getAverageTemp();
return string;
}
}
File added
This diff is collapsed.
File added
File added
import java.io. * ;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Storage {
private ArrayList<Food> food;
private ArrayList<Habitat> habitat;
private ArrayList<Diet> diet;
public ArrayList<Food> getFood() {
return this.food;
}
public ArrayList<Diet> getDiet() {
return this.diet;
}
public Diet findDiet(String dietName) {
Diet dietMatch = null;
for (Diet diet : this.getDiet()) {
if (diet.getName().equals(dietName)) {
dietMatch = diet;
}
}
return dietMatch;
}
public ArrayList<Habitat> getHabitat() {
return this.habitat;
}
public Habitat findHabitat(String habitatName) {
Habitat habitatMatch = null;
for (Habitat habitat : this.getHabitat()) {
if (habitat.getName().equals(habitatName)) {
habitatMatch = habitat;
}
}
return habitatMatch;
}
public Storage(String foodPath, String habitatPath, String dietPath) throws Exception {
this.food = importFood(foodPath);
this.habitat = importHabitat(habitatPath);
this.diet = importDiet(dietPath);
for (Food food : this.getFood()) {
System.out.println("Imported Food Object: "+food.toString());
}
for (Habitat habitat : this.getHabitat()) {
System.out.println("Imported Habitat Object: "+habitat.toString());
}
for (Diet diet : this.getDiet()) {
System.out.println("Imported Diet Object: "+diet.toString());
}
System.out.println("\n");
}
private ArrayList<Food> importFood(String foodPath) {
ArrayList<Food> foodObjs = new ArrayList<Food>();
ArrayList<String[]> foods = this.read(foodPath);
for (String[] item : foods) {
foodObjs.add(createFood(item));
}
return foodObjs;
}
private ArrayList<Habitat> importHabitat(String habitatPath) {
ArrayList<Habitat> habitatObjs = new ArrayList<Habitat>();
ArrayList<String[]> habitats = this.read(habitatPath);
for (String[] item : habitats) {
habitatObjs.add(createHabitat(item));
}
return habitatObjs;
}
private ArrayList<Diet> importDiet(String dietPath) throws Exception {
ArrayList<Diet> dietObjs = new ArrayList<Diet>();
ArrayList<String[]> diets = this.read(dietPath);
for (String[] item : diets) {
dietObjs.add(createDiet(item));
}
return dietObjs;
}
private Food createFood(String[] item) {
Food food = new Food(item[0], Double.parseDouble(item[1]));
return food;
}
private Habitat createHabitat(String[] item) {
Habitat habitat = new Habitat(item[0], Double.parseDouble(item[1]));
return habitat;
}
private Diet createDiet(String[] item) throws Exception {
ArrayList<DietItem> dietItems = new ArrayList<DietItem>();
Food matchedFood = null;
//take head off of item
String dietName = item[0];
//remove head
String[] copy = new String[item.length - 1];
for (int i = 0, j = 0; i < item.length; i++) {
if (i != 0) {
copy[j++] = item[i];
}
}
item = copy;
for (int index = 0; index < item.length; index++) {
if (index%2==0) {
// Pull foodname from the diet data
String foodName = item[index];
//Find matching food
for (Food food : this.getFood()) {
String cycledName = food.getName();
if (cycledName.equals(foodName)) {
matchedFood = food;
}
}
// not regestering match?
if (matchedFood == null) {
throw new Exception("Diet specified food not found in storage: "+foodName);
}
} else {
int percentage;
try {
percentage = Integer.parseInt(item[index]);
} catch (Exception e) {
System.out.println(item[index]);
throw new Exception("Problem converting percentage value to int");
}
dietItems.add(new DietItem(matchedFood, percentage));
matchedFood = null;
}
}
// need to instantiate diet class with DietItem objects passed from for loop
Diet diet = new Diet(dietName, dietItems);
return diet;
}
private ArrayList<String[]> read(String path) {
String line = "";
String splitBy = ",";
ArrayList<String[]> readItems = new ArrayList<String[]>();
try {
//parsing a CSV file into BufferedReader class constructor
BufferedReader br = new BufferedReader(new FileReader(path));
while ((line = br.readLine()) != null)
//returns a Boolean value
{
String[] item = line.split(splitBy);
readItems.add(item);
}
}
catch(IOException e) {
e.printStackTrace();
}
return readItems;
}
}
File added
import java.util.ArrayList; // import the ArrayList class
public class Zoo {
private ArrayList<Animal> allAnimals = new ArrayList<Animal>();
public ArrayList<Animal> getAllAnimals() {
return this.allAnimals;
}
public void allAnimalsAdd(Animal animal) {
this.getAllAnimals().add(animal);
}
public ArrayList<Food> foodRequired(Animal animal) {
double calories = animal.getWeeklyCalories();
Diet dietObject = animal.getDiet();
ArrayList<DietItem> diet = dietObject.getContent();
ArrayList<Food> foodRequired = new ArrayList<Food>();
for (DietItem item : diet) {
double proportionalCalories = (item.getDietPercentage()/100)*calories;
Food food = new Food(item.getFood());
double kilogramsRequired = proportionalCalories/food.getEnergyDensity();
food.setAmount(kilogramsRequired);
foodRequired.add(food);
}
return foodRequired;
}
public ArrayList<Food> totalFood(ArrayList<Animal> animalList) {
ArrayList<Food> totalFoodRequired = new ArrayList<Food>();
for (Animal animal : animalList) {
ArrayList<Food> foodRequired = this.foodRequired(animal);
for (Food food : foodRequired) {
totalFoodRequired.add(food);
}
}
// Joins food objects
ArrayList<Food> finalFood = new ArrayList<Food>();
for (Food food : totalFoodRequired) {
Boolean match = false;
double amount = 0;
Food carry = food;
for (Food second : finalFood) {
if (second.getName().equals(food.getName())){
match = true;
amount = food.getAmount();
carry = second;
break;
}
}
if (match) {
carry.setAmount(carry.getAmount()+amount);
} else {
finalFood.add(new Food(food));
}
}
return finalFood;
}
public String foodArrayToString(ArrayList<Food> array) {
String string = "";
for (Food food : array) {
string = string+food.toString()+"\n";
}
return string;
}
public String animalArrayToString(ArrayList<Animal> array) {
String string = "";
array.sort((o1, o2) -> o1.getCommonName().compareTo(o2.getCommonName()));
int amount = 0;
for (int i = 0; i < array.size(); i++) {
if ((i > 0) && (array.get(i).details().equals(array.get(i-1).details()))) {
amount = amount+1;
} else {
if (amount > 0) {
string = string+"\n ... and "+amount+" more identical animals.\n\n";
amount = 0;
}
string = string+array.get(i).details()+"\n\n";
}
}
if (amount > 0) {
string = string+"\n ... and "+amount+" more identical animals.\n\n";
amount = 0;
}
return string;
}
}
File added
public abstract class _Ant extends Animal {
private final String CASTE;
public String getCaste() {
return this.CASTE;
}
public _Ant(String antType, boolean sex, String caste, Habitat habitat, String genus, String species, Storage storage) {
super(antType, null, sex, new String[] {"Animalia","Arthropoda","Insecta","Hymenoptera","Formicidae",genus,species}, storage.findDiet("ant"),1, habitat);
this.CASTE = caste;
}
public String additionalDetails() {
return "Form: "+this.CASTE+"\n";
};
}
public class _Capybara extends Animal {
public _Capybara(String name, int age, boolean sex, Storage storage) throws Exception{
super("Capybara", age, sex, new String[] {"Animalia","Chordata","Mammalia","Rodentia","Caviidae","Hydrochoerus","H. hydrochaeris"}, storage.findDiet("rodent"),1000, storage.findHabitat("marsh"), name);
}
public String additionalDetails() {
return "";
};
}
public class _GreyParrot extends _Parrot {
public _GreyParrot(String name, int age, boolean sex, double span, Storage storage) {
super("GreyParrot", age, sex, span, "Grey", storage.findHabitat("jungle"), "Psittacus", "P. erithacus", storage, name);
}
}
public class _Leafcutter extends _Ant {
public _Leafcutter(boolean sex, String caste, Storage storage) {
super("Leafcutter ant", sex, caste, storage.findHabitat("jungle"), "Atta", "Sexdens", storage);
}
}
\ No newline at end of file
public class _Panda extends Animal {
public _Panda(String name, int age, boolean sex, Storage storage) throws Exception{
super("Panda", age, sex, new String[] {"Animalia","Chordata","Mammalia","Carnivora","Ursidae","Ailuropoda","A. melanoleuca"}, storage.findDiet("panda"),9000, storage.findHabitat("ice"), name);
}
public String additionalDetails() {
return "";
};
}
public abstract class _Parrot extends Animal {
private double span;
private final String COLOUR;
public double getSpan() {
return span;
}
public String getColour() {
return this.COLOUR;
}
public void setSpan(double span) {
this.span = span;
}
public _Parrot(String birdType, int age, boolean sex, double span, String colour, Habitat habitat, String genus, String species, Storage storage, String name) {
super(birdType, age, sex, new String[] {"Animalia","Chordata","Aves","Psittacopasserae","Psittaciformes",genus,species}, storage.findDiet("parrot"),220, habitat, name);
this.setSpan(span);
this.COLOUR = colour;
}
public String additionalDetails() {
return "Wingspan: "+this.span+"\n"+"Colour: "+this.COLOUR+"\n";
};
}
File added
public class _Rat extends Animal {
public _Rat(String name, int age, boolean sex, Storage storage) throws Exception{
super("Rat", age, sex, new String[] {"Animalia","Chordata","Mammalia","Simplicidentata","Muridae","Rattus","R. rattus"}, storage.findDiet("rodent"),60, storage.findHabitat("urban"), name);
}
public String additionalDetails() {
return "";
};
}
public class _ScarletMacaw extends _Parrot {
public _ScarletMacaw(String name, int age, boolean sex, double span, Storage storage) {
super("Scarelet Macaw", age, sex, span, "Scarlet", storage.findHabitat("jungle"), "Ara", "A. macao", storage, name);
}
}
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