Skip to content

Actualizacion por equivocación #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions LabMaven2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.Ironhack</groupId>
<artifactId>LabMaven2</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10</version>
</dependency>
</dependencies>
</project>
95 changes: 95 additions & 0 deletions LabMaven2/src/main/java/org/Ironhack/Apartment.java
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}
37 changes: 37 additions & 0 deletions LabMaven2/src/main/java/org/Ironhack/ApartmentCreator.java
Original file line number Diff line number Diff line change
@@ -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<Apartment> 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();
}
}
33 changes: 33 additions & 0 deletions LabMaven2/src/main/java/org/Ironhack/ApartmentManager.java
Original file line number Diff line number Diff line change
@@ -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<Apartment> apartmentsList = new ArrayList<>();
Collections.addAll(apartmentsList,apartmentsArray);

for(Apartment apartment : apartmentsList){
System.out.println(apartment);
}


reader.close();
}
}
File renamed without changes.
29 changes: 29 additions & 0 deletions LabMaven2/src/main/resources/new_apartments.json
Original file line number Diff line number Diff line change
@@ -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
}
]