Laba6 KozyrevSS PIbd-21 GasolineTanker HARD #6
@ -7,6 +7,8 @@ public class CarsGenericCollection <T extends DrawTanker, U extends IMoveableObj
|
||||
private final int _placeSizeWidth = 110;
|
||||
private final int _placeSizeHeight = 80;
|
||||
private final SetGeneric<T> _collection;
|
||||
|
||||
public Iterable<T> getTanker(final Integer maxTank) {return _collection.GetTankers(maxTank);}
|
||||
public CarsGenericCollection(int picWidth, int picHeight)
|
||||
{
|
||||
int width = picWidth / _placeSizeWidth;
|
||||
@ -63,5 +65,5 @@ public class CarsGenericCollection <T extends DrawTanker, U extends IMoveableObj
|
||||
tank.DrawTransport(g);
|
||||
}
|
||||
}
|
||||
|
||||
public void clear(){ _collection.clear(); }
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -9,12 +11,165 @@ public class CarsGenericStorage {
|
||||
}
|
||||
private final int _pictureWidth;
|
||||
private final int _pictureHeight;
|
||||
private static String _separatorForKeyValueWR = "|";
|
||||
private static String _separatorForKeyValue = "\\|";
|
||||
private static String _separatorForObjectWR = ":";
|
||||
private static String _separatorForObject = "\\:";
|
||||
private String _separatorRecordsWR = ";";
|
||||
private String _separatorRecords = "\\;";
|
||||
|
||||
public CarsGenericStorage(int pictureWidth, int pictureHeight)
|
||||
{
|
||||
_pictureHeight = pictureHeight;
|
||||
_pictureWidth = pictureWidth;
|
||||
_carStorages = new HashMap<>();
|
||||
}
|
||||
|
||||
public boolean SaveDataSingle(String filename, String key) {
|
||||
if (new File(filename).exists()) {
|
||||
new File(filename).delete();
|
||||
}
|
||||
|
||||
StringBuilder data = new StringBuilder();
|
||||
data.append(key).append("\n");
|
||||
if (_carStorages.get(key) == null)
|
||||
return false;
|
||||
if (_carStorages.get(key).getTanker(100) == null)
|
||||
return false;
|
||||
for (DrawTanker elem : _carStorages.get(key).getTanker(100))
|
||||
{
|
||||
data.append(elem != null ? ExtentionDrawingTanker.GetDataForSave(elem, _separatorForObjectWR) + "\n" : "");
|
||||
}
|
||||
if (data.length() == 0)
|
||||
return false;
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename)))
|
||||
{
|
||||
writer.write("TankerStorage" + System.lineSeparator() + data.toString());
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean LoadDataSingle(String filename){
|
||||
if (!new File(filename).exists()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
|
||||
String s = reader.readLine();
|
||||
if (s == null || s.length() == 0)
|
||||
return false;
|
||||
|
||||
if (!s.startsWith("TankerStorage"))
|
||||
return false;
|
||||
|
||||
String key = reader.readLine();
|
||||
if (key == null || key.length() == 0)
|
||||
return false;
|
||||
|
||||
CarsGenericCollection<DrawTanker, DrawingObjectTanker> collection;
|
||||
if (_carStorages.containsKey(key)){
|
||||
collection = _carStorages.get(key);
|
||||
collection.clear();
|
||||
}
|
||||
else
|
||||
collection = new CarsGenericCollection<>(_pictureWidth, _pictureHeight);
|
||||
|
||||
List<String> tankerString = new ArrayList<String>();
|
||||
|
||||
s = reader.readLine();
|
||||
while (s != null && s.length() != 0){
|
||||
tankerString.add(s);
|
||||
s = reader.readLine();
|
||||
}
|
||||
|
||||
Collections.reverse(tankerString);
|
||||
for (String elem : tankerString) {
|
||||
DrawTanker tanker = ExtentionDrawingTanker.CreateDrawingTanker(elem, _separatorForObject, _pictureWidth, _pictureHeight);
|
||||
if (tanker == null || collection.plus(tanker) == -1)
|
||||
return false;
|
||||
}
|
||||
|
||||
_carStorages.put(key, collection);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean SaveData(String filename)
|
||||
{
|
||||
if (new File(filename).exists()) {
|
||||
new File(filename).delete();
|
||||
}
|
||||
|
||||
StringBuilder data = new StringBuilder();
|
||||
|
||||
for (Map.Entry<String, CarsGenericCollection<DrawTanker, DrawingObjectTanker>> record : _carStorages.entrySet()) {
|
||||
StringBuilder records = new StringBuilder();
|
||||
for (DrawTanker elem : record.getValue().getTanker(100)) {
|
||||
records.append(elem != null ? ExtentionDrawingTanker.GetDataForSave(elem, _separatorForObjectWR) + _separatorRecordsWR : "");
|
||||
}
|
||||
data.append(record.getKey()).append(_separatorForKeyValueWR).append(records).append("\n");
|
||||
}
|
||||
|
||||
if (data.length() == 0)
|
||||
return false;
|
||||
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
|
||||
writer.write("TrainStorage" + System.lineSeparator() + data.toString());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean LoadData(String filename) {
|
||||
if (!new File(filename).exists()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
|
||||
String s = reader.readLine();
|
||||
if (s == null || s.length() == 0)
|
||||
return false;
|
||||
|
||||
if (!s.startsWith("TrainStorage"))
|
||||
return false;
|
||||
|
||||
_carStorages.clear();
|
||||
s = reader.readLine();
|
||||
while (s != null && s.length() != 0) {
|
||||
String[] record = s.split(_separatorForKeyValue);
|
||||
s = reader.readLine();
|
||||
if (record.length != 2) {
|
||||
continue;
|
||||
}
|
||||
CarsGenericCollection<DrawTanker, DrawingObjectTanker> collection = new CarsGenericCollection<>(_pictureWidth, _pictureHeight);
|
||||
String[] set = record[1].split(_separatorRecords);
|
||||
List<String> reversedSet = Arrays.asList(set);
|
||||
Collections.reverse(reversedSet);
|
||||
for (String elem : reversedSet) {
|
||||
DrawTanker train = ExtentionDrawingTanker.CreateDrawingTanker(elem, _separatorForObject, _pictureWidth, _pictureHeight);
|
||||
if (train == null || collection.plus(train) == -1)
|
||||
return false;
|
||||
}
|
||||
_carStorages.put(record[0], collection);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public void AddSet(String name)
|
||||
{
|
||||
if (Keys().contains(name))
|
||||
|
@ -1,10 +1,12 @@
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
import javax.swing.event.ListSelectionListener;
|
||||
import javax.swing.filechooser.FileNameExtensionFilter;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.image.CropImageFilter;
|
||||
import java.io.File;
|
||||
import java.util.LinkedList;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Queue;
|
||||
@ -149,6 +151,84 @@ class CollectionFrame extends JComponent {
|
||||
BaseFrame f = new BaseFrame(tank);
|
||||
f.setVisible(true);
|
||||
}
|
||||
|
||||
protected void OpenFile_Click()
|
||||
{
|
||||
JFileChooser fileChooser = new JFileChooser();
|
||||
|
||||
fileChooser.setDialogTitle("Выберите файл для загрузки данных");
|
||||
|
||||
// Установка фильтра для файлов с определенным расширением (например, .txt)
|
||||
fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовые файлы (*.txt)", "txt"));
|
||||
|
||||
fileChooser.setDialogTitle("Выберите файл для загрузки данных");
|
||||
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
|
||||
File selectedFile = fileChooser.getSelectedFile();
|
||||
if (_tanksStorage.LoadData(selectedFile.getAbsolutePath())) {
|
||||
JOptionPane.showMessageDialog(null, "Загрузка прошла успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(null, "Не загрузилось", "Результат", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
ReloadObjects();
|
||||
}
|
||||
protected void SaveFile_Click()
|
||||
{
|
||||
JFileChooser fileChooser = new JFileChooser();
|
||||
fileChooser.setDialogTitle("Выберите файл для сохранения данных");
|
||||
|
||||
// Установка фильтра для файлов с определенным расширением (например, .txt)
|
||||
fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовые файлы (*.txt)", "txt"));
|
||||
|
||||
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
|
||||
File selectedFile = fileChooser.getSelectedFile();
|
||||
if (_tanksStorage.SaveData(selectedFile.getAbsolutePath()))
|
||||
JOptionPane.showMessageDialog(null, "Сохранение прошло успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
|
||||
else
|
||||
JOptionPane.showMessageDialog(null, "Не сохранилось", "Результат", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
protected void LoadFileSingle_Click()
|
||||
{
|
||||
JFileChooser fileChooser = new JFileChooser();
|
||||
|
||||
fileChooser.setDialogTitle("Выберите файл для загрузки данных");
|
||||
|
||||
// Установка фильтра для файлов с определенным расширением (например, .txt)
|
||||
fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовые файлы (*.txt)", "txt"));
|
||||
|
||||
fileChooser.setDialogTitle("Выберите файл для загрузки данных");
|
||||
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
|
||||
File selectedFile = fileChooser.getSelectedFile();
|
||||
if (_tanksStorage.LoadDataSingle(selectedFile.getAbsolutePath())) {
|
||||
JOptionPane.showMessageDialog(null, "Загрузка прошла успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(null, "Не загрузилось", "Результат", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
ReloadObjects();
|
||||
}
|
||||
protected void SaveFileSingle_Click(JList<String> jListStorage)
|
||||
{
|
||||
if (jListStorage.getSelectedValue() == null){
|
||||
JOptionPane.showMessageDialog(null, "Не выбран гараж", "Ошибка", JOptionPane.ERROR_MESSAGE);
|
||||
return;
|
||||
}
|
||||
|
||||
JFileChooser fileChooser = new JFileChooser();
|
||||
fileChooser.setDialogTitle("Выберите файл для сохранения данных");
|
||||
|
||||
// Установка фильтра для файлов с определенным расширением (например, .txt)
|
||||
fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовые файлы (*.txt)", "txt"));
|
||||
|
||||
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
|
||||
File selectedFile = fileChooser.getSelectedFile();
|
||||
if (_tanksStorage.SaveDataSingle(selectedFile.getAbsolutePath(), jListStorage.getSelectedValue()))
|
||||
JOptionPane.showMessageDialog(null, "Сохранение прошло успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
|
||||
else
|
||||
JOptionPane.showMessageDialog(null, "Не сохранилось", "Результат", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class GarageFrame extends JFrame {
|
||||
@ -277,6 +357,51 @@ class GarageFrame extends JFrame {
|
||||
Collection.setBounds(0, 0, Width-200, Height);
|
||||
add(Collection);
|
||||
|
||||
JMenuBar menuBar = new JMenuBar();
|
||||
JMenu fileMenu = new JMenu("File");
|
||||
JMenuItem openItem = new JMenuItem("Open");
|
||||
openItem.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Collection.OpenFile_Click();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
JMenuItem saveItem = new JMenuItem("Save");
|
||||
saveItem.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Collection.SaveFile_Click();
|
||||
}}
|
||||
);
|
||||
|
||||
JMenuItem openItemSingle = new JMenuItem("Open single");
|
||||
openItemSingle.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Collection.LoadFileSingle_Click();
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
JMenuItem saveItemSingle = new JMenuItem("Save single");
|
||||
saveItemSingle.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Collection.SaveFileSingle_Click(listStorage);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
fileMenu.add(openItem);
|
||||
fileMenu.add(saveItem);
|
||||
fileMenu.add(openItemSingle);
|
||||
fileMenu.add(saveItemSingle);
|
||||
|
||||
menuBar.add(fileMenu);
|
||||
menuBar.setBounds(Width - 50, Height - 70, 50, 20);
|
||||
add(menuBar);
|
||||
}
|
||||
|
||||
}
|
76
ExtentionDrawingTanker.java
Normal file
76
ExtentionDrawingTanker.java
Normal file
@ -0,0 +1,76 @@
|
||||
import java.awt.*;
|
||||
|
||||
public class ExtentionDrawingTanker {
|
||||
public static DrawTanker CreateDrawingTanker(String info, String separatorForObject, int width, int height)
|
||||
{
|
||||
String[] strs = info.split(separatorForObject);
|
||||
if (strs.length == 5)
|
||||
{
|
||||
String[] colorValues = strs[2].split(",");
|
||||
|
||||
DrawTanker drawTanker = new DrawTanker(Integer.parseInt(strs[0]), (double)Integer.parseInt(strs[1]),
|
||||
new Color(
|
||||
Integer.parseInt(colorValues[0].replaceAll("\\D", "")),
|
||||
Integer.parseInt(colorValues[1].replaceAll("\\D", "")),
|
||||
Integer.parseInt(colorValues[2].replaceAll("\\D", ""))
|
||||
),
|
||||
width, height,
|
||||
Integer.parseInt(strs[4]));
|
||||
try{
|
||||
drawTanker.wheelsDrawing = (IWheelDraw)Class.forName(strs[3]).getDeclaredConstructor().newInstance();
|
||||
drawTanker.wheelsDrawing.setWheelCount(Integer.parseInt(strs[4]));
|
||||
}
|
||||
catch(Exception e){
|
||||
return null;
|
||||
}
|
||||
return drawTanker;
|
||||
}
|
||||
if (strs.length == 9)
|
||||
{
|
||||
String[] colorValues = strs[2].split(",");
|
||||
String[] colorValues2 = strs[5].split(",");
|
||||
DrawGasolineTanker drawGasoline = new DrawGasolineTanker(
|
||||
Integer.parseInt(strs[0]),
|
||||
(double)Integer.parseInt(strs[1]),
|
||||
new Color(
|
||||
Integer.parseInt(colorValues[0].replaceAll("\\D", "")),
|
||||
Integer.parseInt(colorValues[1].replaceAll("\\D", "")),
|
||||
Integer.parseInt(colorValues[2].replaceAll("\\D", ""))
|
||||
),
|
||||
new Color(
|
||||
Integer.parseInt(colorValues2[0].replaceAll("\\D", "")),
|
||||
Integer.parseInt(colorValues2[1].replaceAll("\\D", "")),
|
||||
Integer.parseInt(colorValues2[2].replaceAll("\\D", ""))
|
||||
),
|
||||
strs[6].equals("true"),
|
||||
strs[7].equals("true"),
|
||||
strs[8].equals("true"),
|
||||
width, height,
|
||||
Integer.parseInt(strs[4]));
|
||||
try{
|
||||
drawGasoline.wheelsDrawing = (IWheelDraw)Class.forName(strs[3]).getDeclaredConstructor().newInstance();
|
||||
drawGasoline.wheelsDrawing.setWheelCount(Integer.parseInt(strs[4]));
|
||||
}
|
||||
catch(Exception e){
|
||||
return null;
|
||||
}
|
||||
return drawGasoline;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public static String GetDataForSave(DrawTanker drawningTanker, String separatorForObject)
|
||||
{
|
||||
BaseTanker tanker = drawningTanker.GasolineTanker;
|
||||
if (tanker == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
String str = "" + tanker.getSpeed() + separatorForObject + (int)tanker.getWeight() + separatorForObject + tanker.getBodyColor() + separatorForObject + drawningTanker.wheelsDrawing.getClass().getName() + separatorForObject + drawningTanker.wheelsDrawing.getWheelCount();
|
||||
if (!(tanker instanceof GasolineTanker))
|
||||
{
|
||||
return str;
|
||||
}
|
||||
return str+separatorForObject+((GasolineTanker)tanker).GetAdditionalColor().toString()+separatorForObject+((GasolineTanker)tanker).GetBodyKit()+separatorForObject+((GasolineTanker)tanker).GetWing()+separatorForObject+((GasolineTanker)tanker).GetSportLine();
|
||||
}
|
||||
|
||||
}
|
@ -71,4 +71,7 @@ public class SetGeneric <T extends Object>{
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void clear() {_places.clear();}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user