From 3279eff580acbf09e3f99cbd44268af439b840d3 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Sat, 8 Apr 2023 00:04:10 +0100 Subject: [PATCH] [POO] AA1 added --- 1ano/2semestre/poo/src/aval/aa1/Animal.java | 76 +++++++++++++ 1ano/2semestre/poo/src/aval/aa1/Bird.java | 44 ++++++++ 1ano/2semestre/poo/src/aval/aa1/Dog.java | 40 +++++++ .../poo/src/aval/aa1/IPetShelter.java | 9 ++ 1ano/2semestre/poo/src/aval/aa1/Main.java | 104 ++++++++++++++++++ .../poo/src/aval/aa1/PetShelter.java | 67 +++++++++++ 1ano/2semestre/poo/src/aval/aa1/Rabbit.java | 44 ++++++++ 7 files changed, 384 insertions(+) create mode 100644 1ano/2semestre/poo/src/aval/aa1/Animal.java create mode 100644 1ano/2semestre/poo/src/aval/aa1/Bird.java create mode 100644 1ano/2semestre/poo/src/aval/aa1/Dog.java create mode 100644 1ano/2semestre/poo/src/aval/aa1/IPetShelter.java create mode 100644 1ano/2semestre/poo/src/aval/aa1/Main.java create mode 100644 1ano/2semestre/poo/src/aval/aa1/PetShelter.java create mode 100644 1ano/2semestre/poo/src/aval/aa1/Rabbit.java diff --git a/1ano/2semestre/poo/src/aval/aa1/Animal.java b/1ano/2semestre/poo/src/aval/aa1/Animal.java new file mode 100644 index 0000000..7fc1bd6 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/aa1/Animal.java @@ -0,0 +1,76 @@ +package aval.aa1; + +import java.util.Objects; + +public class Animal { + public static int currentId = 1; + private final int id; + private String name; + private int weight; + private int age; + private String sponsor; + + public Animal(String name, int weight, int age, String sponsor) { + this.id = Animal.currentId++; + this.name = name; + this.weight = weight; + this.age = age; + this.sponsor = sponsor; + } + + public Animal(String name, int weight, int age) { + this(name, weight, age, null); + } + + public int getId() { + return id; + } + + public void setName(String name) { + this.name = name; + } + public String getName() { + return name; + } + + public void setWeight(int weight) { + if (weight <= 0) + throw new IllegalArgumentException("Weight must be positive!"); + this.weight = weight; + } + public int getWeight() { + return weight; + } + + public void setAge(int age) { + if (age <= 0) + throw new IllegalArgumentException("Age must be positive!"); + this.age = age; + } + public int getAge() { + return age; + } + + public void setSponsor(String sponsor) { + this.sponsor = sponsor; + } + public String getSponsor() { + return sponsor; + } + + @Override + public String toString() { + return String.format("ID: %d\nName: %s\nAge: %d\nWeight: %d\nSponsor: %s", this.id, this.name, this.age, this.weight, this.sponsor == null ? "None" : sponsor); + } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Animal animal = (Animal) o; + return id == animal.id && weight == animal.weight && age == animal.age && Objects.equals(name, animal.name) && Objects.equals(sponsor, animal.sponsor); + } + @Override + public int hashCode() { + return Objects.hash(id, name, weight, age, sponsor); + } +} diff --git a/1ano/2semestre/poo/src/aval/aa1/Bird.java b/1ano/2semestre/poo/src/aval/aa1/Bird.java new file mode 100644 index 0000000..02abeb9 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/aa1/Bird.java @@ -0,0 +1,44 @@ +package aval.aa1; + +import java.util.Objects; + +public class Bird extends Animal { + private String habitat; + + public Bird(String name, int weight, int age, String habitat, String sponsor) { + super(name, weight, age, sponsor); + this.habitat = habitat; + } + + public Bird(String name, int weight, int age, String habitat) { + super(name, weight, age); + this.habitat = habitat; + } + + public String gethabitat() { + return this.habitat; + } + + public void sethabitat(String habitat) { + this.habitat = habitat; + } + + @Override + public String toString() { + return "Bird of habitat " + habitat + "\n" + super.toString(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + Bird bird = (Bird) o; + return Objects.equals(habitat, bird.habitat); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), habitat); + } +} diff --git a/1ano/2semestre/poo/src/aval/aa1/Dog.java b/1ano/2semestre/poo/src/aval/aa1/Dog.java new file mode 100644 index 0000000..1cd5962 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/aa1/Dog.java @@ -0,0 +1,40 @@ +package aval.aa1; + +import java.util.Objects; + +public class Dog extends Animal { + private final String breed; + + public Dog(String name, int weight, int age, String breed, String sponsor) { + super(name, weight, age, sponsor); + this.breed = breed; + } + + public Dog(String name, int weight, int age, String breed) { + super(name, weight, age); + this.breed = breed; + } + + public String getBreed() { + return this.breed; + } + + @Override + public String toString() { + return "Dog of breed " + this.breed + "\n" + super.toString(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + Dog dog = (Dog) o; + return Objects.equals(breed, dog.breed); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), breed); + } +} diff --git a/1ano/2semestre/poo/src/aval/aa1/IPetShelter.java b/1ano/2semestre/poo/src/aval/aa1/IPetShelter.java new file mode 100644 index 0000000..f9cd0af --- /dev/null +++ b/1ano/2semestre/poo/src/aval/aa1/IPetShelter.java @@ -0,0 +1,9 @@ +package aval.aa1; + +public interface IPetShelter { + public void addAnimal(Animal animal); + public void removeAnimal(Animal animal); + public Animal searchForAnimal(String name); + public boolean sponsorAnimal(int animalId); + public void listAllAnimals(); +} diff --git a/1ano/2semestre/poo/src/aval/aa1/Main.java b/1ano/2semestre/poo/src/aval/aa1/Main.java new file mode 100644 index 0000000..564ff3f --- /dev/null +++ b/1ano/2semestre/poo/src/aval/aa1/Main.java @@ -0,0 +1,104 @@ +package aval.aa1; + +import java.util.Scanner; + +public class Main { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + IPetShelter shelter = new PetShelter("Patudos"); + + int choice = 0; + do { + System.out.println("Choose an option:"); + System.out.println("1. Add animal"); + System.out.println("2. Remove animal"); + System.out.println("3. Search for animal"); + System.out.println("4. Sponsor an animal"); + System.out.println("5. View all animals"); + System.out.println("6. Exit"); + + choice = Integer.parseInt(scanner.nextLine()); + + switch (choice) { + case 1: + // adicionar animal + System.out.println("Adding an animal."); + System.out.print("Animal type (Dog/Rabbit/Bird): "); + String newAnimalType = scanner.nextLine(); + System.out.print("Animal name: "); + String newAnimalName = scanner.nextLine(); + System.out.print("Animal age: "); + int newAnimalAge = Integer.parseInt(scanner.nextLine()); + System.out.print("Animal weight: "); + int newAnimalWeight = Integer.parseInt(scanner.nextLine()); + System.out.print("Animal sponsor: "); + String newAnimalSponsor = scanner.nextLine(); + Animal newAnimal; + switch (newAnimalType) { + case "Dog": + System.out.print("Dog breed: "); + String breed = scanner.nextLine(); + newAnimal = new Dog(newAnimalName, newAnimalWeight, newAnimalAge, breed, newAnimalSponsor); + shelter.addAnimal(newAnimal); + break; + case "Rabbit": + System.out.print("Rabbit fur size (small/large): "); + String fur = scanner.nextLine(); + newAnimal = new Rabbit(newAnimalName, newAnimalWeight, newAnimalAge, fur, newAnimalSponsor); + shelter.addAnimal(newAnimal); + break; + case "Bird": + System.out.print("Bird habitat: "); + String habitat = scanner.nextLine(); + newAnimal = new Bird(newAnimalName, newAnimalWeight, newAnimalAge, habitat, newAnimalSponsor); + shelter.addAnimal(newAnimal); + break; + default: + System.out.println("Invalid animal."); + break; + } + break; + case 2: + // remover animal + System.out.print("Animal name: "); + String removedAnimalName = scanner.nextLine(); + Animal removedAnimal = shelter.searchForAnimal(removedAnimalName); + if (removedAnimal != null) { + shelter.removeAnimal(removedAnimal); + System.out.println("Animal removed."); + } else { + System.out.println("Animal not found."); + } + break; + case 3: + // procurar animal + System.out.print("Animal name:"); + String searchAnimalName = scanner.nextLine(); + System.out.println(shelter.searchForAnimal(searchAnimalName)); + break; + case 4: + // apadrinhar animal + System.out.print("Id of animal you want to sponsor: "); + int sponsorId = Integer.parseInt(scanner.nextLine()); + if (shelter.sponsorAnimal(sponsorId)) + System.out.println("Animal of id '" + sponsorId + "' was sponsored"); + else + System.out.println("Couldn't sponsor animal with id: " + sponsorId); + break; + case 5: + // imprimir a informação de todos os animais + shelter.listAllAnimals(); + break; + case 6: + System.out.println("Goodbye!"); + break; + default: + // imprimir mensagem de erro + System.out.println("Invalid option."); + break; + } + + } while (choice != 6); + } +} diff --git a/1ano/2semestre/poo/src/aval/aa1/PetShelter.java b/1ano/2semestre/poo/src/aval/aa1/PetShelter.java new file mode 100644 index 0000000..594a265 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/aa1/PetShelter.java @@ -0,0 +1,67 @@ +package aval.aa1; + +import java.util.Scanner; + +public class PetShelter implements IPetShelter { + private String shelterName; + private Animal[] animals; + + public PetShelter(String shelterName) { + this.shelterName = shelterName; + } + + @Override + public void addAnimal(Animal animal) { + if (animals == null) { + animals = new Animal[1]; + animals[0] = animal; + } else { + Animal[] newAnimals = new Animal[animals.length + 1]; + for (int i = 0; i < animals.length; i++) { + newAnimals[i] = animals[i]; + } + newAnimals[newAnimals.length - 1] = animal; + animals = newAnimals; + } + } + + @Override + public void removeAnimal(Animal animal) { + Animal[] newAnimals = new Animal[animals.length - 1]; + int index = 0; + for (Animal a : animals) { + if (a.getId() != animal.getId()) { + newAnimals[index] = a; + index++; + } + } + animals = newAnimals; + } + + @Override + public Animal searchForAnimal(String name) { + for (Animal a : animals) + if (a.getName().equals(name)) + return a; + return null; + } + + @Override + public boolean sponsorAnimal(int animalId) { + System.out.println("Sponsoring animal with id " + animalId + "..."); + System.out.print("Sponsor's name: "); + String sponsorId = new Scanner(System.in).nextLine(); + for (Animal a : animals) + if (a.getId() == animalId) { + a.setSponsor(sponsorId); + return true; + } + return false; + } + + @Override + public void listAllAnimals() { + for (Animal a : animals) + System.out.println(a.toString()+"\n"); + } +} diff --git a/1ano/2semestre/poo/src/aval/aa1/Rabbit.java b/1ano/2semestre/poo/src/aval/aa1/Rabbit.java new file mode 100644 index 0000000..cdb7124 --- /dev/null +++ b/1ano/2semestre/poo/src/aval/aa1/Rabbit.java @@ -0,0 +1,44 @@ +package aval.aa1; + +import java.util.Objects; + +public class Rabbit extends Animal { + private String fur; + + public Rabbit(String name, int weight, int age, String fur, String sponsor) { + super(name, weight, age, sponsor); + this.fur = fur; + } + + public Rabbit(String name, int weight, int age, String fur) { + super(name, weight, age); + this.fur = fur; + } + + public String getFur() { + return this.fur; + } + + public void setfur(String fur) { + this.fur = fur; + } + + @Override + public String toString() { + return "Rabbit of fur " + fur + "\n" + super.toString(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + Rabbit rabbit = (Rabbit) o; + return Objects.equals(fur, rabbit.fur); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), fur); + } +}