This commit is contained in:
Вячеслав Иванов 2023-12-27 22:32:01 +04:00
parent e078c849ab
commit 288fc4bc73
5 changed files with 399 additions and 4 deletions

View File

@ -45,6 +45,33 @@ public class DrawningBus {
DrawningDoor.ChangeDoorsNumber(random.nextInt(2, 6)); DrawningDoor.ChangeDoorsNumber(random.nextInt(2, 6));
} }
public int GetIDrawType() {
if (DrawningDoor instanceof DrawningDoor) {
return 0;
}
if (DrawningDoor instanceof DrawningDoorOval) {
return 1;
}
if (DrawningDoor instanceof DrawningDoorTriangle) {
return 2;
}
return -1;
}
public void ChangeIDraw(int variant) {
IDraw cur;
int dif = _busWidth / 10;
if (variant == 0) {
cur = new DrawningDoor(_busWidth - dif, _busHeight, _startPosX, _startPosY);
} else if (variant == 1) {
cur = new DrawningDoorOval(_busWidth - dif, _busHeight, _startPosX, _startPosY);
} else {
cur = new DrawningDoorTriangle(_busWidth - dif, _busHeight, _startPosX, _startPosY);
}
ChangeIDraw(cur);
}
protected DrawningBus(int speed, double weight, Color bodyColor, protected DrawningBus(int speed, double weight, Color bodyColor,
int width, int height, int busWidth, int busHeight) { int width, int height, int busWidth, int busHeight) {
if (width <= _busWidth || height <= _busHeight) { if (width <= _busWidth || height <= _busHeight) {

View File

@ -0,0 +1,101 @@
package DoubleDeckerBus.DrawningObjects;
import DoubleDeckerBus.Entities.EntityDoubleDeckerBus;
import java.awt.*;
public class ExtentionDrawningBus {
private static String getName(Color col){
if (col.equals(Color.RED)) {
return new String("RED");
}
if (col.equals(Color.GREEN)) {
return new String("GREEN");
}
if (col.equals(Color.BLUE)) {
return new String("BLUE");
}
if (col.equals(Color.YELLOW)) {
return new String("YELLOW");
}
if (col.equals(Color.WHITE)) {
return new String("WHITE");
}
if (col.equals(Color.GRAY)) {
return new String("GRAY");
}
if (col.equals(Color.BLACK)) {
return new String("BLACK");
}
if (col.equals(Color.PINK)) {
return new String("PINK");
}
return null;
}
private static Color getColor(String col){
if (col.equals("RED")) {
return Color.RED;
}
if (col.equals("GREEN")) {
return Color.GREEN;
}
if (col.equals("BLUE")) {
return Color.BLUE;
}
if (col.equals("YELLOW")) {
return Color.YELLOW;
}
if (col.equals("WHITE")) {
return Color.WHITE;
}
if (col.equals("GRAY")) {
return Color.GRAY;
}
if (col.equals("BLACK")) {
return Color.BLACK;
}
if (col.equals("PINK")) {
return Color.PINK;
}
return null;
}
public static DrawningBus CreateDrawningBus(String info, char separatorForObject, int width, int height) {
String[] strs = info.split(Character.toString(separatorForObject));
if (strs.length == 5) {
DrawningBus toRet = new DrawningBus(Integer.parseInt(strs[0]),
Integer.parseInt(strs[1]), getColor(strs[2]), width, height);
toRet.ChangeDoorsNumb(Integer.parseInt(strs[3]));
toRet.ChangeIDraw(Integer.parseInt(strs[4]));
return toRet;
}
if (strs.length == 9) {
DrawningBus toRet = new DrawningDoubleDeckerBus(Integer.parseInt(strs[0]),
Integer.parseInt(strs[1]), getColor(strs[2]),
getColor(strs[8]), 4, width, height, Boolean.parseBoolean(strs[5]),
Boolean.parseBoolean(strs[6]), Boolean.parseBoolean(strs[7]));
toRet.ChangeDoorsNumb(Integer.parseInt(strs[3]));
toRet.ChangeIDraw(Integer.parseInt(strs[4]));
return toRet;
}
return null;
}
public static String GetDataForSave(DrawningBus drawningBus, char separatorForObject) {
var bus = drawningBus.EntityBus();
if (bus == null) {
return null;
}
var str = String.format("%d%c%d%c%s%c%d%c%d", bus.Speed(), separatorForObject, (int)bus.Weight(),
separatorForObject, getName(bus.BodyColor), separatorForObject,
drawningBus._doorNumb, separatorForObject, drawningBus.GetIDrawType());
if (!(bus instanceof EntityDoubleDeckerBus)) {
return str;
}
var nstr = String.format("%s%c%b%c%b%c%b%c%s", str, separatorForObject,
((EntityDoubleDeckerBus) bus).Ladder(), separatorForObject,
((EntityDoubleDeckerBus) bus).LineBetweenFloor(), separatorForObject,
((EntityDoubleDeckerBus) bus).SecondFloor(), separatorForObject,
getName(((EntityDoubleDeckerBus) bus).AdditionalColor), separatorForObject);
return nstr;
}
}

View File

@ -5,6 +5,9 @@ import DoubleDeckerBus.Generics.BusGenericStorage;
import DoubleDeckerBus.Generics.BusTrashCollection; import DoubleDeckerBus.Generics.BusTrashCollection;
import DoubleDeckerBus.Generics.TheBusesGenericCollection; import DoubleDeckerBus.Generics.TheBusesGenericCollection;
import DoubleDeckerBus.MovementStrategy.DrawningObjectBus; import DoubleDeckerBus.MovementStrategy.DrawningObjectBus;
import java.io.File;
import java.io.IOException;
import java.util.List; import java.util.List;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -43,6 +46,17 @@ public class FormBusCollection {
} }
public FormBusCollection() { public FormBusCollection() {
JMenuBar menuFile = new JMenuBar();
JMenu file = new JMenu("Файл");
menuFile.add(file);
JMenuItem saveFile = new JMenuItem("Сохранить");
JMenuItem loadFile = new JMenuItem("Загрузить");
JMenuItem saveCollection = new JMenuItem("Сохранить коллекцию");
JMenuItem loadCollection = new JMenuItem("Загрузить коллекцию");
file.add(saveCollection);
file.add(loadCollection);
file.add(saveFile);
file.add(loadFile);
BusTrashCollection<DrawningBus> _trashCollection = new BusTrashCollection<>(); BusTrashCollection<DrawningBus> _trashCollection = new BusTrashCollection<>();
JButton callTrashButton = new JButton("мусор"); JButton callTrashButton = new JButton("мусор");
_storage = new BusGenericStorage(pictureBoxWidth, pictureBoxHeight); _storage = new BusGenericStorage(pictureBoxWidth, pictureBoxHeight);
@ -77,12 +91,94 @@ public class FormBusCollection {
toolBox.add(refreshButton); toolBox.add(refreshButton);
toolBox.add(callTrashButton); toolBox.add(callTrashButton);
collectionFrame.add(toolBox); collectionFrame.add(toolBox);
collectionFrame.setJMenuBar(menuFile);
collectionFrame.add(canvas); collectionFrame.add(canvas);
collectionFrame.setVisible(true); collectionFrame.setVisible(true);
canvas._storage = _storage; canvas._storage = _storage;
canvas.listBoxStorages = listBoxStorages; canvas.listBoxStorages = listBoxStorages;
saveFile.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser("C:\\Users\\Vyach\\Documents\\lab6save");
fc.addChoosableFileFilter(new TxtSaveFilter());
int retrieval = fc.showSaveDialog(null);
if (retrieval == JFileChooser.APPROVE_OPTION) {
File file = new File(fc.getSelectedFile() + "." + "txt");
try {
_storage.SaveData(file);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
});
saveCollection.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser("C:\\Users\\Vyach\\Documents\\lab6save");
fc.addChoosableFileFilter(new TxtSaveFilter());
int retrieval = fc.showSaveDialog(null);
if (retrieval == JFileChooser.APPROVE_OPTION) {
File file = new File(fc.getSelectedFile() + "." + "txt");
try {
if (listBoxStorages.getSelectedIndex() == -1) {
return;
}
_storage._busStorages.get(listBoxStorages.getSelectedValue()).SaveData(file, listBoxStorages.getSelectedValue());
ReloadObjects();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
});
loadFile.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser("C:\\Users\\Vyach\\Documents\\lab6save");
int ret = fc.showDialog(null, "Открыть файл");
if (ret == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
try {
_storage.LoadData(file);
canvas._storage =_storage;
ReloadObjects();
canvas.repaint();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
});
loadCollection.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser("C:\\Users\\Vyach\\Documents\\lab6save");
int ret = fc.showDialog(null, "Открыть файл");
if (ret == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
try {
_storage.LoadCollection(file);
ReloadObjects();
canvas.repaint();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
});
addStorageButton.addActionListener(new ActionListener() { addStorageButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
@ -100,7 +196,7 @@ public class FormBusCollection {
if (listBoxStorages.getSelectedIndex() == -1) { if (listBoxStorages.getSelectedIndex() == -1) {
return; return;
} }
_storage.DelSet(listBoxStorages.getSelectedValue()); _storage.DelSet(listBoxStorages.getSelectedValue(), _trashCollection);
ReloadObjects(); ReloadObjects();
} }
}); });

View File

@ -1,14 +1,129 @@
package DoubleDeckerBus.Generics; package DoubleDeckerBus.Generics;
import DoubleDeckerBus.DrawningObjects.DrawningBus; import DoubleDeckerBus.DrawningObjects.DrawningBus;
import DoubleDeckerBus.DrawningObjects.ExtentionDrawningBus;
import DoubleDeckerBus.MovementStrategy.DrawningObjectBus; import DoubleDeckerBus.MovementStrategy.DrawningObjectBus;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class BusGenericStorage { public class BusGenericStorage {
final HashMap<String, TheBusesGenericCollection<DrawningBus, DrawningObjectBus>> _busStorages; public final HashMap<String, TheBusesGenericCollection<DrawningBus, DrawningObjectBus>> _busStorages;
private static final char _separatorForKeyValue = '|';
private final char _separatorRecords = ';';
private static final char _separatorForObject = ':';
public boolean SaveData(File f) throws IOException {
if (f.exists()) {
f.delete();
}
f.createNewFile();
StringBuilder data = new StringBuilder();
data.append("BusStorage\n");
for (Map.Entry<String, TheBusesGenericCollection<DrawningBus, DrawningObjectBus>> record : _busStorages.entrySet()) {
StringBuilder records = new StringBuilder();
for (DrawningBus elem : record.getValue().GetTheBuses()) {
records.append(String.format("%s%c", ExtentionDrawningBus.GetDataForSave(elem, _separatorForObject),
_separatorRecords));
}
data.append(String.format("%s%c%s\n", record.getKey(), _separatorForKeyValue, records.toString()));
}
if (data.length() == 0)
return false;
FileWriter writer = new FileWriter(f);
writer.write(data.toString());
writer.flush();
writer.close();
return true;
}
public boolean LoadData(File f) throws FileNotFoundException {
if (!f.exists()) {
return false;
}
StringBuilder bufferTextFromFile = new StringBuilder();
Scanner s = new Scanner(f);
while (s.hasNext()) {
bufferTextFromFile.append(s.next() + "\n");
}
s.close();
var strs = bufferTextFromFile.toString().split("\n");
if(strs == null || strs.length == 0) {
return false;
}
if (!strs[0].startsWith("BusStorage"))
return false;
_busStorages.clear();
for (String data : strs){
String st = new String("\\" + Character.toString( _separatorForKeyValue));
String[]record = data.split(st);
if (record.length != 2)
continue;
TheBusesGenericCollection<DrawningBus, DrawningObjectBus> collection =
new TheBusesGenericCollection<>(_pictureWidth, _pictureHeight);
String[] set = record[1].split(Character.toString(_separatorRecords));
for (int i = set.length -1; i >=0; i--) {
String elem = set[i];
DrawningBus bus = ExtentionDrawningBus.CreateDrawningBus(elem,
_separatorForObject, _pictureWidth, _pictureHeight);
if (bus != null)
{
if (!(collection.Insert(bus)))
{
return false;
}
}
}
_busStorages.put(record[0], collection);
}
return true;
}
public boolean LoadCollection(File f) throws FileNotFoundException {
if (!f.exists()) {
return false;
}
StringBuilder bufferTextFromFile =new StringBuilder();
Scanner s = new Scanner(f);
while (s.hasNext()) {
bufferTextFromFile.append(s.next() + "\n");
}
s.close();
var strs = bufferTextFromFile.toString().split("\n");
if (strs == null || strs.length == 0) {
return false;
}
if (!strs[0].startsWith("BusCollection")) {
return false;
}
String collectionName = strs[1];
TheBusesGenericCollection<DrawningBus, DrawningObjectBus> collection = GetCollection(collectionName);
if (collection == null) {
collection = new TheBusesGenericCollection<>(_pictureWidth, _pictureHeight);
} else {
collection.Clear();
}
String[] bussInfo = strs[2].split(Character.toString(TheBusesGenericCollection._separatorRecords));
for (int i = bussInfo.length-1; i >= 0; i--) {
String data = bussInfo[i];
DrawningBus bus = ExtentionDrawningBus.CreateDrawningBus(data,
TheBusesGenericCollection._separatorForObject, _pictureWidth, _pictureHeight);
if (bus != null) {
if (!(collection.Insert(bus))) {
return false;
}
}
}
AddSetFromFile(collectionName, collection);
return true;
}
public List<String> Keys() { public List<String> Keys() {
if (_busStorages == null) { if (_busStorages == null) {
return null; return null;
@ -31,10 +146,21 @@ public class BusGenericStorage {
_busStorages.put(name, new TheBusesGenericCollection<>(_pictureWidth, _pictureHeight)); _busStorages.put(name, new TheBusesGenericCollection<>(_pictureWidth, _pictureHeight));
} }
public void DelSet(String name) { public void AddSetFromFile(String name, TheBusesGenericCollection<DrawningBus, DrawningObjectBus> toAdd){
if (_busStorages.containsKey(name)){
_busStorages.remove(name);
}
_busStorages.put(name, toAdd);
}
public void DelSet(String name, BusTrashCollection<DrawningBus> trashBox){
if (!_busStorages.containsKey(name)) { if (!_busStorages.containsKey(name)) {
return; return;
} }
TheBusesGenericCollection<DrawningBus, DrawningObjectBus> cur = _busStorages.get(name);
for(int i = 0; i < cur.Size(); i++)
trashBox.Push(cur.Get(i));
_busStorages.remove(name); _busStorages.remove(name);
} }
@ -48,4 +174,8 @@ public class BusGenericStorage {
public DrawningBus Get(String collectionName, int position) { public DrawningBus Get(String collectionName, int position) {
return _busStorages.get(collectionName).Get(position); return _busStorages.get(collectionName).Get(position);
} }
public TheBusesGenericCollection<DrawningBus, DrawningObjectBus> GetCollection(String collectionName){
return _busStorages.get(collectionName);
}
} }

View File

@ -1,12 +1,20 @@
package DoubleDeckerBus.Generics; package DoubleDeckerBus.Generics;
import DoubleDeckerBus.DrawningObjects.DrawningBus; import DoubleDeckerBus.DrawningObjects.DrawningBus;
import DoubleDeckerBus.DrawningObjects.ExtentionDrawningBus;
import DoubleDeckerBus.MovementStrategy.IMoveableObject; import DoubleDeckerBus.MovementStrategy.IMoveableObject;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class TheBusesGenericCollection<T extends DrawningBus, U extends IMoveableObject> { public class TheBusesGenericCollection<T extends DrawningBus, U extends IMoveableObject> {
public static char _separatorRecords = ';';
public static char _separatorForObject = ':';
private final int _pictureWidth; private final int _pictureWidth;
private final int _pictureHeight; private final int _pictureHeight;
@ -15,7 +23,35 @@ public class TheBusesGenericCollection<T extends DrawningBus, U extends IMoveabl
private final int _placeSizeHeight = 50; private final int _placeSizeHeight = 50;
private final SetGeneric<T> _collection; private SetGeneric<T> _collection;
public ArrayList<T> GetTheBuses(){
return _collection.GetBus(_collection.Count);
}
public boolean SaveData(File f, String name) throws IOException {
if (f.exists()) {
f.delete();
}
f.createNewFile();
StringBuilder data = new StringBuilder();
data.append("BusCollection\n");
data.append(String.format("%s\n", name));
StringBuilder records = new StringBuilder();
for (DrawningBus elem : GetTheBuses()) {
records.append(String.format("%s%c", ExtentionDrawningBus.GetDataForSave(elem, _separatorForObject),
_separatorRecords));
}
data.append(records);
if (data.length() == 0) {
return false;
}
FileWriter writer = new FileWriter(f);
writer.write(data.toString());
writer.flush();
writer.close();
return true;
}
public TheBusesGenericCollection(int picWidth, int picHeight){ public TheBusesGenericCollection(int picWidth, int picHeight){
int width = picWidth / _placeSizeWidth; int width = picWidth / _placeSizeWidth;
@ -75,6 +111,7 @@ public class TheBusesGenericCollection<T extends DrawningBus, U extends IMoveabl
} }
} }
private void DrawObjects(Graphics g) { private void DrawObjects(Graphics g) {
for (int i = 0; i < _collection.Count; i++) { for (int i = 0; i < _collection.Count; i++) {
DrawningBus bus = _collection.Get(i); DrawningBus bus = _collection.Get(i);
@ -85,4 +122,8 @@ public class TheBusesGenericCollection<T extends DrawningBus, U extends IMoveabl
} }
} }
} }
public void Clear(){
_collection = new SetGeneric<>(_pictureWidth * _pictureHeight);
}
} }