diff --git a/LabMaven2/pom.xml b/LabMaven2/pom.xml new file mode 100644 index 0000000..f1b4402 --- /dev/null +++ b/LabMaven2/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + org.Ironhack + LabMaven2 + 1.0-SNAPSHOT + + + 21 + 21 + UTF-8 + + + + com.google.code.gson + gson + 2.10 + + + \ No newline at end of file diff --git a/LabMaven2/src/main/java/org/Ironhack/Apartment.java b/LabMaven2/src/main/java/org/Ironhack/Apartment.java new file mode 100644 index 0000000..177f5eb --- /dev/null +++ b/LabMaven2/src/main/java/org/Ironhack/Apartment.java @@ -0,0 +1,95 @@ +package org.Ironhack; +import java.util.UUID; + +public class Apartment { + private String id; + private String address; + private double price; + private int rooms; + private String description; + private String owner; + private Boolean available; + + public Apartment(){ + setId(); + } + + public Apartment(String address, double price, int rooms, String description, String owner, Boolean available) { + setId(); + this.address = address; + this.price = price; + this.rooms = rooms; + this.description = description; + this.owner = owner; + this.available = available; + } + + public String getAddresss() { + return address; + } + + public void setAddresss(String address) { + this.address = address; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + + public int getRooms() { + return rooms; + } + + public void setRooms(int rooms) { + this.rooms = rooms; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } + + public Boolean getAvailable() { + return available; + } + + public void setAvailable(Boolean available) { + this.available = available; + } + + public String getId() { + return id; + } + + public void setId() { + this.id = UUID.randomUUID().toString(); + } + + @Override + public String toString() { + return "Apartment{" + + "id=" + id + + ", address='" + address + '\'' + + ", price=" + price + + ", rooms=" + rooms + + ", description='" + description + '\'' + + ", owner='" + owner + '\'' + + ", available=" + available + + '}'; + } +} diff --git a/LabMaven2/src/main/java/org/Ironhack/ApartmentCreator.java b/LabMaven2/src/main/java/org/Ironhack/ApartmentCreator.java new file mode 100644 index 0000000..b1543d8 --- /dev/null +++ b/LabMaven2/src/main/java/org/Ironhack/ApartmentCreator.java @@ -0,0 +1,37 @@ +package org.Ironhack; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class ApartmentCreator { + public static void main(String[] args) throws IOException { + //Creamos una lista de apartamentos + List newApartments = new ArrayList<>(); + + // Creamos nuevos apartamentos + Apartment apartment1 = new Apartment("Calle Falsa 123", 1200.50, 3, "Apartamento acogedor", "Juan Pérez", true); + Apartment apartment2 = new Apartment("Avenida Siempre Viva 742", 1500.00, 4, "Apartamento amplio", "María López", false); + Apartment apartment3 = new Apartment("Plaza Mayor 1", 2000.00, 5, "Apartamento de lujo", "Pedro García", true); + Apartment apartment4 = new Apartment(); + System.out.println(apartment4); + // Añadimos los apartamentos a la lista + newApartments.add(apartment1); + newApartments.add(apartment2); + newApartments.add(apartment3); + + + // Crear un FileWriter para escribir el archivo JSON + FileWriter writer = new FileWriter("LabMaven2/src/main/resources/new_apartments.json"); + + // Convertimos la lista de apartamentos a JSON y la escribimos en el archivo creado + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + gson.toJson(newApartments, writer); + + writer.close(); + } +} diff --git a/LabMaven2/src/main/java/org/Ironhack/ApartmentManager.java b/LabMaven2/src/main/java/org/Ironhack/ApartmentManager.java new file mode 100644 index 0000000..9246fde --- /dev/null +++ b/LabMaven2/src/main/java/org/Ironhack/ApartmentManager.java @@ -0,0 +1,33 @@ +package org.Ironhack; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + + +import java.io.*; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class ApartmentManager { + public static void main(String[] args) throws IOException { + //Creamos el constructor de GSON + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + //Leemos el archivo JSON + Reader reader = new FileReader("LabMaven2/src/main/resources/apartments.json"); + //Convertimos el Json a un array de objetos + Apartment[] apartmentsArray = gson.fromJson(reader, Apartment[].class); + + //Creamos un ArrayList y añadimos los apartamentos + List apartmentsList = new ArrayList<>(); + Collections.addAll(apartmentsList,apartmentsArray); + + for(Apartment apartment : apartmentsList){ + System.out.println(apartment); + } + + + reader.close(); + } +} diff --git a/apartments.json b/LabMaven2/src/main/resources/apartments.json similarity index 100% rename from apartments.json rename to LabMaven2/src/main/resources/apartments.json diff --git a/LabMaven2/src/main/resources/new_apartments.json b/LabMaven2/src/main/resources/new_apartments.json new file mode 100644 index 0000000..8e8bb3e --- /dev/null +++ b/LabMaven2/src/main/resources/new_apartments.json @@ -0,0 +1,29 @@ +[ + { + "id": "3856ab98-2b71-4875-8366-7362b657f2a2", + "address": "Calle Falsa 123", + "price": 1200.5, + "rooms": 3, + "description": "Apartamento acogedor", + "owner": "Juan Pérez", + "available": true + }, + { + "id": "abd84316-c4dd-4971-92d9-2a673c8a2fc2", + "address": "Avenida Siempre Viva 742", + "price": 1500.0, + "rooms": 4, + "description": "Apartamento amplio", + "owner": "María López", + "available": false + }, + { + "id": "ea309816-fe52-4987-80bd-f6331a1807b2", + "address": "Plaza Mayor 1", + "price": 2000.0, + "rooms": 5, + "description": "Apartamento de lujo", + "owner": "Pedro García", + "available": true + } +] \ No newline at end of file