From 8cb4a02a0dcd3bcc82b716335d016712336977d1 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Tue, 2 May 2023 11:20:41 +0100 Subject: [PATCH] [POO] aula09 added --- 1ano/2semestre/poo/src/aula09/ex1/ALDemo.java | 42 +++++++ .../poo/src/aula09/ex2/CollectionTester.java | 46 ++++++++ .../poo/src/aula09/ex3/CommercialPlane.java | 50 +++++++++ .../poo/src/aula09/ex3/MilitaryPlane.java | 50 +++++++++ 1ano/2semestre/poo/src/aula09/ex3/Plane.java | 90 +++++++++++++++ .../poo/src/aula09/ex3/PlaneManager.java | 76 +++++++++++++ .../poo/src/aula09/ex3/PlaneTester.java | 104 ++++++++++++++++++ 7 files changed, 458 insertions(+) create mode 100644 1ano/2semestre/poo/src/aula09/ex1/ALDemo.java create mode 100644 1ano/2semestre/poo/src/aula09/ex2/CollectionTester.java create mode 100644 1ano/2semestre/poo/src/aula09/ex3/CommercialPlane.java create mode 100644 1ano/2semestre/poo/src/aula09/ex3/MilitaryPlane.java create mode 100644 1ano/2semestre/poo/src/aula09/ex3/Plane.java create mode 100644 1ano/2semestre/poo/src/aula09/ex3/PlaneManager.java create mode 100644 1ano/2semestre/poo/src/aula09/ex3/PlaneTester.java diff --git a/1ano/2semestre/poo/src/aula09/ex1/ALDemo.java b/1ano/2semestre/poo/src/aula09/ex1/ALDemo.java new file mode 100644 index 0000000..ba1b60c --- /dev/null +++ b/1ano/2semestre/poo/src/aula09/ex1/ALDemo.java @@ -0,0 +1,42 @@ +package aula09.ex1; + +import aula06.ex1.Person; +import utils.DateYMD; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +public class ALDemo { + public static void main(String[] args) { + ArrayList c1= new ArrayList<>(); + for(int i= 10; i<= 100; i+=10) c1.add(i);System.out.println("Size: "+ c1.size()); + + for(int i= 0; i< c1.size(); i++) + System.out.println("Elemento: "+ c1.get(i)); + + ArrayList c2= new ArrayList<>(); + c2.add("Vento"); + c2.add("Calor"); + c2.add("Frio"); + c2.add("Chuva"); + System.out.println(c2); + Collections.sort(c2); + System.out.println(c2); + c2.remove("Frio"); + c2.remove(0); + System.out.println(c2); + + Set c3 = new HashSet<>(); + c3.add(new Person("Ana", 12345678, new DateYMD(1, 1, 2000))); + c3.add(new Person("Joao", 42342289, new DateYMD(1, 1, 2000))); + c3.add(new Person("Maria", 12346789, new DateYMD(1, 1, 2000))); + c3.add(new Person("Marco", 12356789, new DateYMD(1, 1, 2000))); + c3.add(new Person("Ana", 12346789, new DateYMD(1, 1, 2000))); + + for (Person p : c3) { + System.out.println(p); + } + } +} diff --git a/1ano/2semestre/poo/src/aula09/ex2/CollectionTester.java b/1ano/2semestre/poo/src/aula09/ex2/CollectionTester.java new file mode 100644 index 0000000..e860748 --- /dev/null +++ b/1ano/2semestre/poo/src/aula09/ex2/CollectionTester.java @@ -0,0 +1,46 @@ +package aula09.ex2; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; + +public class CollectionTester { + public static void main(String[] args) { + int DIM= 5000; + Collection col= new ArrayList<>(); + checkPerformance(col, DIM); + } + + private static void checkPerformance(Collection col, int DIM) { + double start, stop, delta; + // Add + start = System.nanoTime(); // clock snapshot before + for(int i=0; i iterator = col.iterator(); + while(iterator.hasNext()) { + iterator.next(); + iterator.remove(); + } + stop = System.nanoTime(); // clock snapshot after + delta = (stop-start)/1e6; // convert nanoseconds to milliseconds + System.out.println(col.size() + ": Remove from "+ col.getClass().getSimpleName() +" took "+ delta+ "ms"); + } +} diff --git a/1ano/2semestre/poo/src/aula09/ex3/CommercialPlane.java b/1ano/2semestre/poo/src/aula09/ex3/CommercialPlane.java new file mode 100644 index 0000000..9877124 --- /dev/null +++ b/1ano/2semestre/poo/src/aula09/ex3/CommercialPlane.java @@ -0,0 +1,50 @@ +package aula09.ex3; + +import java.util.Objects; + +public class CommercialPlane extends Plane { + private int numOfCrewMembers; + + public CommercialPlane(String id, String manufacturer, String model, int year, int maxNumOfPassengers, double maxSpeed, int numOfCrewMembers) { + super(id, manufacturer, model, year, maxNumOfPassengers, maxSpeed); + this.numOfCrewMembers = numOfCrewMembers; + } + + public int getNumOfCrewMembers() { + return this.numOfCrewMembers; + } + + public void setNumOfCrewMembers(int numOfCrewMembers) { + this.numOfCrewMembers = numOfCrewMembers; + } + + public String getPlaneType() { + return "Commercial"; + } + + @Override + public String toString() { + return "CommercialPlane {" + + "\n\tid='" + super.getId() + '\'' + + ",\n\tmanufacturer='" + super.getManufacturer() + '\'' + + ",\n\tmodel='" + super.getModel() + '\'' + + ",\n\tproductionYear=" + super.getProductionYear() + + ",\n\tmaxPassengers=" + super.getMaxPassengers() + + ",\n\tmaxSpeed=" + super.getMaxSpeed() + + ",\n\tnumOfCrewMembers=" + this.getNumOfCrewMembers() + + ",\n}"; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof CommercialPlane plane)) return false; + if (!super.equals(o)) return false; + return this.getNumOfCrewMembers() == plane.getNumOfCrewMembers(); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), this.getNumOfCrewMembers()); + } +} diff --git a/1ano/2semestre/poo/src/aula09/ex3/MilitaryPlane.java b/1ano/2semestre/poo/src/aula09/ex3/MilitaryPlane.java new file mode 100644 index 0000000..38c6eb7 --- /dev/null +++ b/1ano/2semestre/poo/src/aula09/ex3/MilitaryPlane.java @@ -0,0 +1,50 @@ +package aula09.ex3; + +import java.util.Objects; + +public class MilitaryPlane extends Plane { + private int numMissiles; + + public MilitaryPlane(String id, String manufacturer, String model, int year, int maxPassengers, double maxSpeed, int numMissiles) { + super(id, manufacturer, model, year, maxPassengers, maxSpeed); + this.numMissiles = numMissiles; + } + + public int getNumMissiles() { + return this.numMissiles; + } + + public void setNumMissiles(int numMissiles) { + this.numMissiles = numMissiles; + } + + public String getPlaneType() { + return "Military"; + } + + @Override + public String toString() { + return "MilitaryPlane {" + + "\n\tid='" + super.getId() + '\'' + + ",\n\tmanufacturer='" + super.getManufacturer() + '\'' + + ",\n\tmodel='" + super.getModel() + '\'' + + ",\n\tproductionYear=" + super.getProductionYear() + + ",\n\tmaxPassengers=" + super.getMaxPassengers() + + ",\n\tmaxSpeed=" + super.getMaxSpeed() + + ",\n\tnumMissiles=" + this.getNumMissiles() + + ",\n}"; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof MilitaryPlane plane)) return false; + if (!super.equals(o)) return false; + return this.getNumMissiles() == plane.getNumMissiles(); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), this.getNumMissiles()); + } +} \ No newline at end of file diff --git a/1ano/2semestre/poo/src/aula09/ex3/Plane.java b/1ano/2semestre/poo/src/aula09/ex3/Plane.java new file mode 100644 index 0000000..143348f --- /dev/null +++ b/1ano/2semestre/poo/src/aula09/ex3/Plane.java @@ -0,0 +1,90 @@ +package aula09.ex3; + +import java.util.Objects; + +public class Plane { + private final String id; + private String manufacturer; + private String model; + private int productionYear; + private int maxPassengers; + private double maxSpeed; + + public Plane(String id, String manufacturer, String model, int productionYear, int maxPassengers, double maxSpeed) { + this.id = id; + this.manufacturer = manufacturer; + this.model = model; + this.productionYear = productionYear; + this.maxPassengers = maxPassengers; + this.maxSpeed = maxSpeed; + } + + + public String getId() { + return id; + } + + public String getManufacturer() { + return manufacturer; + } + + public void setManufacturer(String manufacturer) { + this.manufacturer = manufacturer; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public int getProductionYear() { + return productionYear; + } + + public void setProductionYear(int productionYear) { + this.productionYear = productionYear; + } + + public int getMaxPassengers() { + return maxPassengers; + } + + public void setMaxPassengers(int maxPassengers) { + this.maxPassengers = maxPassengers; + } + + public double getMaxSpeed() { + return maxSpeed; + } + + public void setMaxSpeed(double maxSpeed) { + this.maxSpeed = maxSpeed; + } + + @Override + public String toString() { + return "Plane {" + + "\n\tid='" + id + '\'' + + ",\n\tmanufacturer='" + manufacturer + '\'' + + ",\n\tmodel='" + model + '\'' + + ",\n\tproductionYear=" + productionYear + + ",\n\tmaxPassengers=" + maxPassengers + + ",\n\tmaxSpeed=" + maxSpeed + + ",\n}"; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Plane plane)) return false; + return this.getProductionYear() == plane.getProductionYear() && this.getMaxPassengers() == plane.getMaxPassengers() && Double.compare(plane.getMaxSpeed(), this.getMaxSpeed()) == 0 && Objects.equals(this.getId(), plane.getId()) && Objects.equals(this.getManufacturer(), plane.getManufacturer()) && Objects.equals(this.getModel(), plane.getModel()); + } + + @Override + public int hashCode() { + return Objects.hash(this.getId(), this.getManufacturer(), this.getModel(), this.getProductionYear(), this.getMaxPassengers(), this.getMaxSpeed()); + } +} diff --git a/1ano/2semestre/poo/src/aula09/ex3/PlaneManager.java b/1ano/2semestre/poo/src/aula09/ex3/PlaneManager.java new file mode 100644 index 0000000..10a6d1c --- /dev/null +++ b/1ano/2semestre/poo/src/aula09/ex3/PlaneManager.java @@ -0,0 +1,76 @@ +package aula09.ex3; + +import java.util.*; + +public class PlaneManager { + private final LinkedList planes = new LinkedList<>(); + + public void addPlane(Plane plane) { + planes.add(plane); + } + + public void removePlane(String id) { + for (Plane plane : planes) { + if (plane.getId().equals(id)) { + planes.remove(plane); + return; + } + } + } + + public Plane searchPlane(String id) { + for (Plane plane : planes) { + if (plane.getId().equals(id)) { + return plane; + } + } + return null; + } + + public LinkedList getCommercialPlanes() { + LinkedList commercialPlanes = new LinkedList<>(); + for (Plane plane : planes) { + if (plane instanceof CommercialPlane) { + commercialPlanes.add(plane); + } + } + return commercialPlanes; + } + + public LinkedList getMilitaryPlanes() { + LinkedList militaryPlanes = new LinkedList<>(); + for (Plane plane : planes) { + if (plane instanceof MilitaryPlane) { + militaryPlanes.add(plane); + } + } + return militaryPlanes; + } + + public Plane getFastestPlane() { + Plane fastestPlane = null; + for (Plane plane : planes) { + if (fastestPlane == null || plane.getMaxSpeed() > fastestPlane.getMaxSpeed()) { + fastestPlane = plane; + } + } + return fastestPlane; + } + + public void printAllPlanes() { + for (Plane plane : planes) { + System.out.println(plane); + } + } + + public void printAllPlanes(String type) { + for (Plane plane : planes) { + if (plane instanceof CommercialPlane && type.equals("commercial")) { + System.out.println(plane); + } + else if (plane instanceof MilitaryPlane && type.equals("military")) { + System.out.println(plane); + } + } + } +} diff --git a/1ano/2semestre/poo/src/aula09/ex3/PlaneTester.java b/1ano/2semestre/poo/src/aula09/ex3/PlaneTester.java new file mode 100644 index 0000000..31e1f59 --- /dev/null +++ b/1ano/2semestre/poo/src/aula09/ex3/PlaneTester.java @@ -0,0 +1,104 @@ +package aula09.ex3; + +import java.util.Scanner; + +public class PlaneTester { + public static void main(String[] args) { + PlaneManager planeManager = new PlaneManager(); + Scanner scanner = new Scanner(System.in); + int choice; + + do { + System.out.println("\nPlane Fleet Menu:"); + System.out.println("1. Add a plane to the fleet"); + System.out.println("2. Remove a plane from the fleet"); + System.out.println("3. Search for a plane"); + System.out.println("4. Print summary of all planes in the fleet"); + System.out.println("5. Print list of all commercial planes in the fleet"); + System.out.println("6. Print list of all military planes in the fleet"); + System.out.println("7. Print the fastest plane in the fleet"); + System.out.println("0. Exit"); + + System.out.print("Enter your choice: "); + choice = Integer.parseInt(scanner.nextLine()); + + switch (choice) { + case 1 -> addPlane(planeManager, scanner); + case 2 -> removePlane(planeManager, scanner); + case 3 -> searchPlane(planeManager, scanner); + case 4 -> printAllPlanes(planeManager); + case 5 -> printCommercialPlanes(planeManager); + case 6 -> printMilitaryPlanes(planeManager); + case 7 -> printFastestPlane(planeManager); + case 0 -> System.out.println("Exiting program..."); + default -> System.out.println("Invalid choice. Please try again."); + } + } while (choice != 0); + + scanner.close(); + } + + private static void addPlane(PlaneManager planeManager, Scanner scanner) { + System.out.print("Enter the plane's ID: "); + String id = scanner.nextLine(); + System.out.print("Enter the plane's manufacturer: "); + String manufacturer = scanner.nextLine(); + System.out.print("Enter the plane's model: "); + String model = scanner.nextLine(); + System.out.print("Enter the plane's year of manufacture: "); + int year = Integer.parseInt(scanner.nextLine()); + System.out.print("Enter the plane's passenger count: "); + int passengerCount = Integer.parseInt(scanner.nextLine()); + System.out.print("Enter the plane's maximum speed: "); + int maxSpeed = Integer.parseInt(scanner.nextLine()); + System.out.print("Enter the plane's type (commercial/military): "); + String type = scanner.nextLine(); + if (type.equals("commercial")) { + System.out.print("Enter the plane's crew members count: "); + int crewMembersCount = Integer.parseInt(scanner.nextLine()); + planeManager.addPlane(new CommercialPlane(id, manufacturer, model, year, passengerCount, maxSpeed, crewMembersCount)); + } + else if (type.equals("military")) { + System.out.print("Enter the plane's missile count: "); + int missileCount = Integer.parseInt(scanner.nextLine()); + planeManager.addPlane(new MilitaryPlane(id, manufacturer, model, year, passengerCount, maxSpeed, missileCount)); + } + } + + private static void removePlane(PlaneManager planeManager, Scanner scanner) { + System.out.print("Enter the plane's ID: "); + String id = scanner.nextLine(); + if (planeManager.searchPlane(id) == null) { + System.out.println("Plane not found."); + return; + } + planeManager.removePlane(id); + } + + private static void searchPlane(PlaneManager planeManager, Scanner scanner) { + System.out.print("Enter the plane's ID: "); + String id = scanner.nextLine(); + Plane plane = planeManager.searchPlane(id); + if (plane == null) { + System.out.println("Plane not found."); + return; + } + System.out.println(plane); + } + + private static void printAllPlanes(PlaneManager planeManager) { + planeManager.printAllPlanes(); + } + + private static void printCommercialPlanes(PlaneManager planeManager) { + planeManager.printAllPlanes("commercial"); + } + + private static void printMilitaryPlanes(PlaneManager planeManager) { + planeManager.printAllPlanes("military"); + } + + private static void printFastestPlane(PlaneManager planeManager) { + System.out.print(planeManager.getFastestPlane()); + } +}