Created
November 26, 2025 05:53
-
-
Save untainsYD/c2b03cd9d2ba79bf57a7b98bf481a387 to your computer and use it in GitHub Desktop.
Java advanced: Laboratory 3, Task4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ua.inf.iwanoff.java.advanced.lab3; | |
| import ua.inf.iwanoff.java.advanced.lab3.model.AcademicGroup; | |
| import ua.inf.iwanoff.java.advanced.lab3.model.Student; | |
| import ua.inf.iwanoff.java.advanced.lab3.service.GroupFileManager; | |
| import java.nio.file.Files; | |
| import java.nio.file.Paths; | |
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| /** | |
| * Завдання 1.4 та 1.5: Робота з даними студента та групи (TXT, XML, JSON). | |
| */ | |
| public class Task4 { | |
| public static void main(String[] args) throws Exception { | |
| System.out.println("=== Лабораторна робота 3. Завдання 1.4 та 1.5 ==="); | |
| Files.createDirectories(Paths.get("results")); | |
| // 1. Створення даних | |
| // ВИПРАВЛЕНО: Огортаємо Arrays.asList у new ArrayList<>(...) | |
| // Це замінює фіксований список на стандартний змінний, який XStream вміє | |
| // серіалізувати без помилок. | |
| AcademicGroup group = new AcademicGroup("KN-224", new ArrayList<>(Arrays.asList( | |
| new Student("Ivanov", 20, 85.5), | |
| new Student("Petrov", 21, 92.0), | |
| new Student("Sidorov", 19, 74.2)))); | |
| System.out.println("Оригінал: " + group); | |
| // 2. Текстовий файл | |
| String txtPath = "results/group.txt"; | |
| GroupFileManager.writeToText(group, txtPath); | |
| AcademicGroup fromTxt = GroupFileManager.readFromText(txtPath); | |
| System.out.println("TXT Read: " + fromTxt); | |
| System.out.println("Equals? " + group.equals(fromTxt)); | |
| // 3. XML (XStream) | |
| String xmlPath = "results/group.xml"; | |
| GroupFileManager.serializeToXML(group, xmlPath); | |
| AcademicGroup fromXml = GroupFileManager.deserializeFromXML(xmlPath); | |
| System.out.println("XML Read: " + fromXml); | |
| System.out.println("Equals? " + group.equals(fromXml)); | |
| // 4. JSON (XStream) | |
| String jsonXPath = "results/group_xstream.json"; | |
| GroupFileManager.serializeToJSON_XStream(group, jsonXPath); | |
| AcademicGroup fromJsonX = GroupFileManager.deserializeFromJSON_XStream(jsonXPath); | |
| System.out.println("JSON(X) : " + fromJsonX); | |
| System.out.println("Equals? " + group.equals(fromJsonX)); | |
| // 5. JSON (Org.json) - Завдання 1.5 | |
| String jsonOPath = "results/group_org.json"; | |
| GroupFileManager.writeJsonOrg(group, jsonOPath); | |
| AcademicGroup fromJsonO = GroupFileManager.readJsonOrg(jsonOPath); | |
| System.out.println("JSON(O) : " + fromJsonO); | |
| System.out.println("Equals? " + group.equals(fromJsonO)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment