chore: Lab05

This commit is contained in:
Fándly Gergő
2021-01-12 17:04:20 +02:00
parent d780953136
commit b9b0716eb8
14 changed files with 489 additions and 0 deletions

View File

@ -0,0 +1,37 @@
package lab05;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class FileUtility {
public static void writeToFile(SubscriptionPlan plan) {
XmlMapper mapper = new XmlMapper();
File file = new File("src/main/resources/plan.xml");
try {
mapper.writeValue(file, plan);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SubscriptionPlan readFromFile() {
XmlMapper mapper = new XmlMapper();
try {
String xml = new String(Files.readAllBytes(Paths.get("src/main/resources/plan.xml")));
SubscriptionPlan subscriptionPlan = mapper.readValue(xml, SubscriptionPlan.class);
return subscriptionPlan;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

View File

@ -0,0 +1,11 @@
package lab05;
public class Main {
public static void main(String[] args) {
SubscriptionPlan plan = new SubscriptionPlan(71, "Plan C");
FileUtility.writeToFile(plan);
SubscriptionPlan subscriptionPlan = FileUtility.readFromFile();
System.out.println(subscriptionPlan.toString());
}
}

View File

@ -0,0 +1,36 @@
package lab05;
public class SubscriptionPlan {
private Integer price;
private String description;
public SubscriptionPlan() {
}
SubscriptionPlan(Integer price, String description) {
this.price = price;
this.description = description;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "SubscriptionPlan{" + "price=" + price + ", description='" + description + '\'' + '}';
}
}