chore: Lab05
This commit is contained in:
37
Lab05/src/main/java/lab05/FileUtility.java
Normal file
37
Lab05/src/main/java/lab05/FileUtility.java
Normal 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;
|
||||
}
|
||||
}
|
11
Lab05/src/main/java/lab05/Main.java
Normal file
11
Lab05/src/main/java/lab05/Main.java
Normal 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());
|
||||
}
|
||||
}
|
36
Lab05/src/main/java/lab05/SubscriptionPlan.java
Normal file
36
Lab05/src/main/java/lab05/SubscriptionPlan.java
Normal 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 + '\'' + '}';
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user