Task 2 done

This commit is contained in:
Rémi Heredero 2023-10-18 10:50:21 +02:00
parent da1a8a2166
commit 67e0ffad03
6 changed files with 324 additions and 0 deletions

56
ApplianceModels.json Normal file
View File

@ -0,0 +1,56 @@
{
"version": "1.0",
"models": [
{
"model": "Sony AF9",
"type": "TV",
"functions": [
{
"function": "DOWN",
"code": 1002
}, {
"function": "LEFT",
"code": 1003
}, {
"function": "RIGHT",
"code": 1004
}, {
"function": "DOWN",
"code": 1005
}, {
"function": "SELECT",
"code": 1006
}
]
}
, {
"model": "UBP-X800",
"type": "Blu-ray & DVD player",
"functions": [
{
"function": "MAIN MENU",
"code": 2002
}, {
"function": "SPEED +",
"code": 2003
}, {
"function": "SPEED -",
"code": 2004
}, {
"function": "NEXT",
"code": 2005
}, {
"function": "PLAY",
"code": 2006
}, {
"function": "STOP",
"code": 2007
}, {
"function": "SELECT",
"code": 2008
}
]
}
]
}

View File

@ -0,0 +1,122 @@
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
public class ApplianceModelsDefinition {
private String version;
private List<Model> models;
private ObjectMapper objectMapper;
ApplianceModelsDefinition(){
this.objectMapper = new ObjectMapper();
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public List<Model> getModels() {
return models;
}
public void setModels(List<Model> models) {
this.models = models;
}
public boolean importFromJSON(){
try {
ApplianceModelsDefinition applianceModelsDefinition;
applianceModelsDefinition= objectMapper.readValue(new FileInputStream("ApplianceModels.json"), ApplianceModelsDefinition.class);
this.version = applianceModelsDefinition.getVersion();
this.models = applianceModelsDefinition.getModels();
return true;
} catch (JsonMappingException e) {
throw new RuntimeException(e);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (JsonParseException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
class Model {
private String model;
private String type;
private List<Function> functions;
Model(String name, String type){
this.model = name;
this.type = type;
}
Model(String name){
this.model = name;
}
Model(){
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<Function> getFunctions() {
return functions;
}
public void setFunctions(List<Function> functions) {
this.functions = functions;
}
}
class Function {
private String function;
private int code;
Function(String function, int code){
this.function = function;
this.code = code;
}
Function(){
}
public String getFunction() {
return function;
}
public void setFunction(String function) {
this.function = function;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}

View File

@ -0,0 +1,32 @@
import java.util.ArrayList;
import java.util.List;
public class Appliances {
private String name; // Name of appliance.
private List<Button> buttons; // List of buttons.
Appliances(String name) {
this.name = name;
this.buttons = new ArrayList<Button>();
}
public void addButton(Button button) {
buttons.add(button);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Button> getButtons() {
return buttons;
}
public void setButtons(List<Button> buttons) {
this.buttons = buttons;
}
}

31
src/main/java/Button.java Normal file
View File

@ -0,0 +1,31 @@
public class Button {
private String text; // Text of button.
private int irCode; // IR code of button.
/**
* Constructor. Initializes text and IR code.
* @param text Text of button.
* @param irCode IR code of button.
*/
public Button(String text, int irCode) {
this.text = text;
this.irCode = irCode;
System.out.println("New button: " + text + " " + irCode);
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public int getIrCode() {
return irCode;
}
public void setIrCode(int irCode) {
this.irCode = irCode;
}
}

58
src/main/java/RCU.java Normal file
View File

@ -0,0 +1,58 @@
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.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class RCU {
private String name;
private List<Appliances> appliances; // List of appliances.
private ObjectMapper objectMapper;
public RCU(String name) {
this.name = name;
this.appliances = new ArrayList<Appliances>();
this.objectMapper = new ObjectMapper();
}
public void addAppliance(Appliances appliance) {
appliances.add(appliance);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Appliances> getAppliances() {
return appliances;
}
public void setAppliances(List<Appliances> appliances) {
this.appliances = appliances;
}
public boolean exportToJSON(){
objectMapper.enable(SerializationFeature.INDENT_OUTPUT); // Enable JSON pretty printing.
try {
objectMapper.writeValue(new FileOutputStream("rcu.json"), this);
return true;
} catch (JsonMappingException e) {
throw new RuntimeException(e);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (JsonGenerationException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

25
src/main/java/Task2.java Normal file
View File

@ -0,0 +1,25 @@
public class Task2 {
public static void main(String[] args) {
/* Test export
RCU rcu = new RCU("My remote control");
Appliances tv = new Appliances("TV");
tv.addButton(new Button("DOWN", 1002));
rcu.addAppliance(tv);
rcu.exportToJSON();
*/
RCU rcu = new RCU("My remote control");
ApplianceModelsDefinition amd = new ApplianceModelsDefinition();
amd.importFromJSON();
for(Model model : amd.getModels()){
Appliances appliance = new Appliances(model.getType());
for(Function function : model.getFunctions()){
appliance.addButton(new Button(function.getFunction(), function.getCode()));
}
rcu.addAppliance(appliance);
}
rcu.exportToJSON();
System.out.println("Finish");
}
}