Лабораторная работа №6 (начало)

This commit is contained in:
DjonniStorm 2024-06-10 03:41:03 +04:00
parent f6d2369ac6
commit f24429521f
8 changed files with 240 additions and 5 deletions

View File

@ -21,7 +21,7 @@ public abstract class AbstractCompany {
_pictureWidth = picWidth;
_pictureHeight = picHeight;
_collection = collection;
_collection.SetMaxCount(GetMaxCount(), (Class) DrawningTruck.class);
_collection.MaxCount(GetMaxCount(), (Class) DrawningTruck.class);
}
public DrawningTruck GetRandomObject() {
Random rnd = new Random();

View File

@ -2,9 +2,12 @@ package CollectionGenericObjects;
public interface ICollectionGenericObjects<T> {
int Count();
void SetMaxCount(int value, Class<T> type);
void MaxCount(int value);
int Insert(T obj);
int Insert(T obj, int position);
T Remove(int position);
T Get(int position);
CollectionType GetCollectionType();
Iterable<T> GetItems();
void ClearCollection();
}

View File

@ -12,7 +12,7 @@ public class ListGenericObjects<T> implements ICollectionGenericObjects<T> {
}
@Override
public void SetMaxCount(int value, Class<T> type) {
public void MaxCount(int value) {
if (value > 0) {
_maxCount = value;
}
@ -53,4 +53,19 @@ public class ListGenericObjects<T> implements ICollectionGenericObjects<T> {
}
return _collection.get(position);
}
@Override
public CollectionType GetCollectionType() {
return null;
}
@Override
public Iterable<T> GetItems() {
return null;
}
@Override
public void ClearCollection() {
}
}

View File

@ -1,11 +1,13 @@
package CollectionGenericObjects;
import java.lang.reflect.Array;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T>{
private T[] _collection;
@Override
public void SetMaxCount(int value, Class<T> type) {
public void MaxCount(int value) {
if (value > 0) {
if (_collection == null)
_collection = (T[]) Array.newInstance(type, value);
@ -69,4 +71,34 @@ public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T>{
if (position < 0 || position >= Count()) return null;
return (T) _collection[position];
}
@Override
public CollectionType GetCollectionType() {
return CollectionType.Massive;
}
@Override
public Iterable<T> GetItems() {
return () -> new Iterator<T>() {
private int currentIndex = 0;
@Override
public boolean hasNext() {
return currentIndex < Count();
}
@Override
public T next() {
if (hasNext()) {
return _collection[currentIndex++];
}
throw new NoSuchElementException();
}
};
}
@Override
public void ClearCollection() {
for (T ship : _collection) {
ship = null;
}
}
}

View File

@ -1,8 +1,15 @@
package CollectionGenericObjects;
import Drawnings.DrawningTruck;
import Drawnings.ExtentionDrawningTruck;
import java.io.*;
import java.util.*;
public class StorageCollection<T> {
public class StorageCollection<T extends DrawningTruck> {
private String _collectionKey = "CollectionsStorage";
private String _separatorForKeyValue = "|";
private String _separatorItems = ";";
private Map<String, ICollectionGenericObjects<T>> _storages;
public Set<String> Keys() {
return _storages.keySet();
@ -42,4 +49,84 @@ public class StorageCollection<T> {
return _storages.get(name).Remove(position);
return null;
}
public boolean SaveData(String filename) {
if (_storages.isEmpty()) return false;
File file = new File(filename);
if (file.exists()) file.delete();
try {
file.createNewFile();
FileWriter writer = new FileWriter(file);
writer.write(_collectionKey);
writer.write("\n");
for (Map.Entry<String, ICollectionGenericObjects<T>> value : _storages.entrySet()) {
StringBuilder sb = new StringBuilder();
sb.append(value.getKey());
sb.append(_separatorForKeyValue);
sb.append(value.getValue().GetCollectionType());
sb.append(_separatorForKeyValue);
sb.append(value.getValue().Count());
sb.append(_separatorForKeyValue);
for (T truck : value.getValue().GetItems()) {
String data = ExtentionDrawningTruck.GetDataForSave((DrawningTruck) truck);
if (data.isEmpty()) continue;
sb.append(data);
sb.append(_separatorItems);
}
sb.append("\n");
writer.write(String.valueOf(sb));
}
writer.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return true;
}
public boolean LoadData(String filename) {
File file = new File(filename);
if (!file.exists()) return false;
try (BufferedReader fs = new BufferedReader(new FileReader(filename))) {
String s = fs.readLine();
if (s == null || s.isEmpty() || !s.startsWith(_collectionKey))
return false;
_storages.clear();
s = "";
while ((s = fs.readLine()) != null) {
String[] record = s.split(_separatorForKeyValue);
if (record.length != 4) {
continue;
}
ICollectionGenericObjects<T> collection = CreateCollection(record[1]);
if (collection == null)
{
return false;
}
collection.MaxCount(Integer.parseInt(record[2]));
String[] set = record[3].split(_separatorItems);
for (String elem : set) {
DrawningTruck truck = ExtentionDrawningTruck.CreateDrawningTruck(elem);
if (collection.Insert((T) truck) == -1)
{
return false;
}
}
_storages.put(record[0], collection);
}
return true;
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
public ICollectionGenericObjects<T> CreateCollection(String s) {
switch (s) {
case "Massive":
return new MassiveGenericObjects<T>();
case "List":
return new ListGenericObjects<T>();
}
return null;
}
}

View File

@ -154,4 +154,17 @@ public class DrawningTruck {
g2d.fillRect(_startPosX + 112, _startPosY + 15, 10, 12);
}
public String[] GetStringRepresentationDecks() {
if (drawningWheels instanceof DrawningRhombWheels) {
return new String[]{String.valueOf(drawningWheels.getNumberOfWheels()), "DrawningRhombWheels"};
}
else if (drawningWheels instanceof DrawningCircleWheels) {
return new String[]{String.valueOf(drawningWheels.getNumberOfWheels()), "DrawningCircleWheels"};
}
else if (drawningWheels instanceof DrawningTriangleWheels) {
return new String[]{String.valueOf(drawningWheels.getNumberOfWheels()), "DrawningTriangleWheels"};
}
return null;
}
}

View File

@ -0,0 +1,66 @@
package Drawnings;
import Entities.EntityCleaningCar;
import Entities.EntityTruck;
import java.util.ArrayList;
import java.util.Collections;
public class ExtentionDrawningTruck {
private static String _separatorForObjectS = ":";
private static String _separatorForObject = "/";
public static DrawningTruck CreateDrawningTruck(String info) {
String[] strs = info.split(_separatorForObject);
EntityTruck truck;
IDrawningWheels wheels = null;
if (strs.length == 8)
{
String s = strs[8];
wheels = switch (s) {
case "DrawningCircleWheels" -> new DrawningCircleWheels();
case "DrawningTriangleWheels" -> new DrawningTriangleWheels();
case "DrawningRhombWheels" -> new DrawningRhombWheels();
default -> null;
};
if (wheels != null) wheels.setCountWheels(Integer.parseInt(strs[7]));
}
else if (strs.length == 6) {
String s = strs[5];
wheels = switch (s) {
case "DrawningCircleWheels" -> wheels = new DrawningCircleWheels();
case "DrawningTriangleWheels" -> wheels = new DrawningTriangleWheels();
case "DrawningRhombWheels" -> wheels = new DrawningRhombWheels();
default -> null;
};
if (wheels != null) wheels.setCountWheels(Integer.parseInt(strs[4]));
}
truck = EntityCleaningCar.CreateEntityShip(strs);
if (truck != null)
{
return new DrawningCleaningCar((EntityCleaningCar) truck, wheels);
}
truck = EntityTruck.CreateEntityShip(strs);
if (truck != null)
{
return new DrawningTruck(truck, wheels);
}
return null;
}
public static String GetDataForSave(DrawningTruck truck)
{
if (truck == null) return "";
String[] array1 = truck.EntityTruck.GetStringRepresentation();
String[] array2 = truck.GetStringRepresentationDecks();
if (array1 == null)
{
return "";
}
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list, array1);
if (array2 == null) {
Collections.addAll(list, "0", " ");
}
else Collections.addAll(list, array2);
return String.join(_separatorForObjectS, list);
}
}

View File

@ -1,6 +1,7 @@
package Entities;
import java.awt.*;
import java.util.Objects;
public class EntityTruck {
private int Speed;
@ -26,4 +27,22 @@ public class EntityTruck {
Weight = weight;
BodyColor = bodyColor;
}
public String[] GetStringRepresentation()
{
return new String[]{"EntityTruck", String.valueOf(Speed), String.valueOf(Weight), colorToHexString(BodyColor)};
}
public static EntityTruck CreateEntityShip(String[] strs)
{
if (strs.length != 6 || !Objects.equals(strs[0], "EntityTruck"))
{
return null;
}
return new EntityTruck(Integer.parseInt(strs[1]), Double.parseDouble(strs[2]), hexIntoColor(strs[3]));
}
public static String colorToHexString(Color color) {
return String.format("#%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue());
}
public static Color hexIntoColor(String hexString) {
return Color.decode(hexString);
}
}