Task 1 done
This commit is contained in:
parent
238c1fcc22
commit
da1a8a2166
65
src/main/java/Rectangle.java
Normal file
65
src/main/java/Rectangle.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
84
src/main/java/Rectangles.java
Normal file
84
src/main/java/Rectangles.java
Normal file
@ -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<Rectangle> rectangles; // List of rectangles.
|
||||
private ObjectMapper objectMapper; // Object mapper for JSON serialization.
|
||||
|
||||
/**
|
||||
* Constructor. Initializes rectangles list and object mapper.
|
||||
*/
|
||||
public Rectangles() {
|
||||
rectangles = new ArrayList<Rectangle>();
|
||||
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<Rectangle> 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);
|
||||
}
|
||||
}
|
||||
}
|
20
src/main/java/Task1.java
Normal file
20
src/main/java/Task1.java
Normal file
@ -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");
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user