From da1a8a2166fdd5d8649ec771464aea55bcc50571 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Wed, 18 Oct 2023 09:43:34 +0200 Subject: [PATCH] Task 1 done --- src/main/java/Rectangle.java | 65 +++++++++++++++++++++++++++ src/main/java/Rectangles.java | 84 +++++++++++++++++++++++++++++++++++ src/main/java/Task1.java | 20 +++++++++ 3 files changed, 169 insertions(+) create mode 100644 src/main/java/Rectangle.java create mode 100644 src/main/java/Rectangles.java create mode 100644 src/main/java/Task1.java diff --git a/src/main/java/Rectangle.java b/src/main/java/Rectangle.java new file mode 100644 index 0000000..7dbed74 --- /dev/null +++ b/src/main/java/Rectangle.java @@ -0,0 +1,65 @@ +/** + * This class represents a rectangle. + * It has width and height. + * @see Rectangles + * @version 1.0 + * @author Rémi Heredero + */ +public class Rectangle { + private int width; // Width of rectangle. + private int height; // Height of rectangle. + + /** + * Constructor. Initializes width and height. + * @param width Width of rectangle. + * @param height Height of rectangle. + */ + public Rectangle(int width, int height) { + this.width = width; + this.height = height; + System.out.println("New rectangle: " + width + "x" + height); + } + + /** + * Constructor. Initializes width and height to 0. + */ + public Rectangle(){ + this(0, 0); + } + + /** + * Get width of rectangle. + * @return width of rectangle + */ + public int getWidth() { + return width; + } + + /** + * Set width of rectangle. + * @param width width of rectangle + */ + public void setWidth(int width) { + this.width = width; + } + + /** + * Get height of rectangle. + * @return height of rectangle + */ + public int getHeight() { + return height; + } + + /** + * Set height of rectangle. + * @param height height of rectangle + */ + public void setHeight(int height) { + this.height = height; + } + + public String toString() { + return width + "x" + height; + } +} diff --git a/src/main/java/Rectangles.java b/src/main/java/Rectangles.java new file mode 100644 index 0000000..848500d --- /dev/null +++ b/src/main/java/Rectangles.java @@ -0,0 +1,84 @@ +import com.fasterxml.jackson.core.JsonGenerationException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * Rectangles class. Contains list of rectangles and methods for importing and exporting rectangles to JSON file. + * @version 1.0 + * @author Rémi Heredero + */ +public class Rectangles { + private List rectangles; // List of rectangles. + private ObjectMapper objectMapper; // Object mapper for JSON serialization. + + /** + * Constructor. Initializes rectangles list and object mapper. + */ + public Rectangles() { + rectangles = new ArrayList(); + objectMapper = new ObjectMapper(); + } + + /** + * Add rectangle to list. + * @param rectangle rectangle to add + */ + public void addRectangle(Rectangle rectangle) { + rectangles.add(rectangle); + } + + /** + * Get all rectangles. + * @return list of rectangles + */ + public List getRectangles() { + return rectangles; + } + + /** + * Export rectangles to JSON file. + * @return true if export was successful + */ + public boolean exportToJSON() { + objectMapper.enable(SerializationFeature.INDENT_OUTPUT); // Enable JSON pretty printing. + try { + objectMapper.writeValue(new FileOutputStream("rectangles.json"), rectangles); + return true; + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } catch (JsonMappingException e) { + throw new RuntimeException(e); + } catch (JsonGenerationException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + /** + * Import rectangles from JSON file. + * @return true if import was successful + */ + public boolean importFromJSON(){ + try { + rectangles = objectMapper.readValue(new FileInputStream("rectangles.json"), ArrayList.class); + return true; + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } catch (JsonMappingException e) { + throw new RuntimeException(e); + } catch (JsonGenerationException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/Task1.java b/src/main/java/Task1.java new file mode 100644 index 0000000..8646877 --- /dev/null +++ b/src/main/java/Task1.java @@ -0,0 +1,20 @@ +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; + +public class Task1 { + public static void main(String[] args) { + + Rectangles rectangles = new Rectangles(); + rectangles.addRectangle(new Rectangle(10, 20)); + rectangles.addRectangle(new Rectangle(20, 30)); + rectangles.addRectangle(new Rectangle(30, 40)); + rectangles.addRectangle(new Rectangle(40, 50)); + rectangles.addRectangle(new Rectangle(50, 60)); + rectangles.exportToJSON(); + rectangles.addRectangle(new Rectangle(60, 70)); + System.out.println("BREAKPOINT HERE"); + rectangles.importFromJSON(); + System.out.println("BREAKPOINT HERE"); + + } +} \ No newline at end of file