Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laba8 Sokolova #8

Open
wants to merge 2 commits into
base: laba7
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 75 additions & 16 deletions src/laba/Dock.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import java.awt.Color;
import java.awt.Graphics;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Iterator;

public class Dock<T extends IShip> {
public class Dock<T extends IShip> implements Serializable, Comparable<Dock<T>>, Iterable<T>, Iterator<T> {
private HashMap<Integer, T> _places;

// ������ ������ ����� �������� �� ������.
Expand All @@ -23,6 +25,7 @@ public class Dock<T extends IShip> {
private int PictureHeight;
// ������������ ���������� ����
private int _maxCount;
private int currentIndex;

// ����������� ����.
public Dock(int size, int pictureWidth, int pictureHeight) {
Expand All @@ -33,21 +36,22 @@ public Dock(int size, int pictureWidth, int pictureHeight) {
}

// �������� "+": ��������� �������
public int Add(T ship) throws DockOverFlowException {
public int Add(T ship) throws DockOverFlowException, DockAlreadyHaveException {
if (_places.size() == _maxCount) {
throw new DockOverFlowException();
}

for (int i = 0; i < _maxCount; i++) {
if (CheckFreePlace(i)) {
_places.put(i, ship);
_places.get(i).SetPosition(5 + i / 5 * _placeSizeWidth + 5,
i % 5 * _placeSizeHeight + _placeSizeHeight - 4, PictureWidth, PictureHeight);

return i;
}
int index = _places.size();
for (int i = 0; i < _places.size(); i++) {
if (CheckFreePlace(i))
index = i;
if (_places.containsValue(ship))
throw new DockAlreadyHaveException();
}
return -1;
_places.put(index, ship);
_places.get(index).SetPosition(5 + index / 5 * _placeSizeWidth + 5,
index % 5 * _placeSizeHeight + _placeSizeHeight - 4, PictureWidth, PictureHeight);
return index;

}

// �������� "-": ������� �������
Expand Down Expand Up @@ -98,12 +102,67 @@ public T getAt(int index) throws DockNotFoundException {

public void setAt(int index, T ship) throws DockOccupiedPlaceException {
if (CheckFreePlace(index)) {
ship.SetPosition(5 + index / 5 * _placeSizeWidth + 5, index % 5 * _placeSizeHeight + 80,
PictureWidth, PictureHeight);
ship.SetPosition(5 + index / 5 * _placeSizeWidth + 5, index % 5 * _placeSizeHeight + 80, PictureWidth,
PictureHeight);
_places.put(index, ship);
}
else {
} else {
throw new DockOccupiedPlaceException(index);
}
}

@Override
public boolean hasNext() {
if (currentIndex + 1 >= _places.size()) {
currentIndex = -1;
return false;
}
currentIndex++;
return true;
}

@Override
public T next() {
return (T) _places.get(currentIndex);
}

@Override
public Iterator<T> iterator() {
return this;
}

@Override
public int compareTo(Dock<T> other) {
if (this._places.size() > other._places.size()) {
return -1;
} else if (this._places.size() < other._places.size()) {
return 1;
} else {
Integer[] thisKeys = this._places.keySet().toArray(new Integer[this._places.size()]);
Integer[] otherKeys = other._places.keySet().toArray(new Integer[other._places.size()]);
for (int i = 0; i < this._places.size(); i++) {
if (this._places.get(thisKeys[i]).getClass().equals( Ship.class)
&& other._places.get(otherKeys[i]).getClass().equals( Ship_Liner.class)) {
return 1;
}
if (this._places.get(thisKeys[i]).getClass().equals( Ship_Liner.class)
&& other._places.get(otherKeys[i]).getClass().equals( Ship.class)) {
return -1;
}
if (this._places.get(thisKeys[i]).getClass().equals( Ship.class)
&& other._places.get(otherKeys[i]).getClass().equals( Ship.class)) {
return (( Ship) this._places.get(thisKeys[i])).compareTo(( Ship) other._places.get(otherKeys[i]));
}
if (this._places.get(thisKeys[i]).getClass().equals( Ship_Liner.class)
&& other._places.get(otherKeys[i]).getClass().equals( Ship_Liner.class)) {
return (( Ship_Liner) this._places.get(thisKeys[i]))
.compareTo((Ship_Liner) other._places.get(otherKeys[i]));
}
}
}
return 0;
}

private void reset() {
currentIndex = -1;
}
}
7 changes: 7 additions & 0 deletions src/laba/DockAlreadyHaveException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package laba;

public class DockAlreadyHaveException extends Exception{
public DockAlreadyHaveException() {
super("� ���� ��� ���� ����� �������");
}
}
17 changes: 16 additions & 1 deletion src/laba/FormDock.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class FormDock {
private JMenuItem menuItem_Load;
private static Logger logger = Logger.getLogger(FormDock.class.getName());
private static int place;

/**
* Launch the application.
*/
Expand Down Expand Up @@ -82,7 +83,7 @@ public FormDock() {
initialize();
}

public void getShip() throws DockOverFlowException {
public void getShip() throws DockOverFlowException, DockAlreadyHaveException {
select = new FormShipConfig(frame);
if (select.res()) {
IShip ship = select.ship;
Expand Down Expand Up @@ -132,6 +133,8 @@ public void actionPerformed(ActionEvent arg0) {
panel_Dock.repaint();
} catch (DockOverFlowException e) {
JOptionPane.showMessageDialog(null, "������������", "���������", JOptionPane.INFORMATION_MESSAGE);
} catch (DockAlreadyHaveException e) {
JOptionPane.showMessageDialog(null, "����� ������� ��� ���� � ����", "���������", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "����������� ������", "���������",
JOptionPane.INFORMATION_MESSAGE);
Expand Down Expand Up @@ -191,6 +194,18 @@ public void actionPerformed(ActionEvent e) {
button_Take.setBounds(865, 311, 89, 23);
frame.getContentPane().add(button_Take);

JButton buttonSort = new JButton("\u0421\u043E\u0440\u0442\u0438\u0440\u043E\u0432\u0430\u0442\u044C");
buttonSort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
dock.Sort();
panel_Dock.repaint();
panel_Dock.Docker = dock.getAt(0);
logger.info("���������� �������");
}
});
buttonSort.setBounds(834, 174, 135, 37);
frame.getContentPane().add(buttonSort);

menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);

Expand Down
35 changes: 18 additions & 17 deletions src/laba/MultiLevelDock.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,20 @@ public void SaveData(String filename) throws DockNotFoundException, FileNotFound
for (Dock<IShip> level : dockStages) {
// �������� �������
WriteToFile("Level" + "\r\n", fs);
for (int i = 0; i < countPlaces; i++) {
try {
IShip ship = level.getAt(i);
if (ship != null) {
// ���� ����� �� ������
// ���������� ��� �������
if (ship instanceof Ship_Liner) {
WriteToFile(i + ":Ship_Liner:", fs);
} else if (ship instanceof Ship) {
WriteToFile(i + ":Ship:", fs);
}

// ������������ ���������
System.out.println(ship.toString() + "\r\n");
WriteToFile(ship.toString() + "\r\n", fs);

for (IShip ship : level) {
if (ship != null) {
// ���� ����� �� ������
// ���������� ��� �������
if (ship instanceof Ship_Liner) {
WriteToFile(":Ship_Liner:", fs);
} else if (ship instanceof Ship) {
WriteToFile(":Ship:", fs);
}
} finally {

// ������������ ���������
System.out.println(ship.toString() + "\r\n");
WriteToFile(ship.toString() + "\r\n", fs);

}
}
}
Expand Down Expand Up @@ -143,4 +139,9 @@ public void LoadData(String filename) throws Exception {
dockStages.get(counter).setAt(Integer.parseInt(strs[k].split(":", 0)[0]), ship);
}
}

// ���������� �������
public void Sort() {
dockStages.sort(null);
}
}
38 changes: 37 additions & 1 deletion src/laba/Ship.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import java.awt.Color;
import java.awt.Graphics;
import java.io.Serializable;
import java.lang.reflect.Field;

import javax.swing.JPanel;

public class Ship extends Boat {
public class Ship extends Boat implements Serializable, Comparable<Ship> {
// ������ ��������� �������
private int shipWidth = 100;
// ������ ��������� �������
Expand Down Expand Up @@ -106,4 +107,39 @@ protected Color getColorByName(String name) {
return null;
}
}

@Override
public int compareTo(Ship another) {
if (another == null) {
return 1;
}

if (MaxSpeed != another.MaxSpeed) {
return Integer.compare(MaxSpeed,another.MaxSpeed);
}
if (Weight != another.Weight) {
return Float.compare(Weight,another.Weight);
}
if (MainColor != another.MainColor) {
return Integer.compare(MainColor.getRGB(),another.MainColor.getRGB());
}
return 0;
}

@Override
public boolean equals(Object another) {
if (another == null) return false;
if (!(another instanceof Ship)) return false;
Ship airCraftObj = (Ship) another;
return equals(airCraftObj);
}

public boolean equals(Ship another) {
if (another == null) return false;
if (!(another instanceof Ship)) return false;
if (MaxSpeed != another.MaxSpeed) return false;
if (Weight != another.Weight) return false;
if (MainColor != another.MainColor) return false;
return true;
}
}
70 changes: 62 additions & 8 deletions src/laba/Ship_Liner.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import java.awt.Color;
import java.awt.Graphics;
import java.io.Serializable;

public class Ship_Liner extends Ship {
public class Ship_Liner extends Ship implements Serializable {
// ������ ��������� �������
private int shipWidth = 100;
// ������ ��������� �������
Expand Down Expand Up @@ -89,11 +90,64 @@ public void DrawShip(Graphics g) {
public void SetDopColor(Color color) {
DopColor = color;
}

//�������������� ���������� � ������
public String toString()
{
return super.toString() + ";" + getColorName(DopColor) + ";" + Pipe + ";" +
SmokeFromPipe + ";" + Window;
}

// �������������� ���������� � ������
public String toString() {
return super.toString() + ";" + getColorName(DopColor) + ";" + Pipe + ";" + SmokeFromPipe + ";" + Window;
}

public int compareTo(Ship_Liner another) {
if (another == null)
return 1;
if (MaxSpeed != another.MaxSpeed)
return Integer.compare(MaxSpeed, another.MaxSpeed);
if (Weight != another.Weight)
return Float.compare(Weight, another.Weight);
if (MainColor != another.MainColor)
return Integer.compare(MainColor.getRGB(), another.MainColor.getRGB());
if (DopColor != another.DopColor)
return Integer.compare(DopColor.getRGB(), another.DopColor.getRGB());
if (Pipe != another.Pipe)
return Boolean.compare(Pipe, another.Pipe);
if (SmokeFromPipe != another.SmokeFromPipe)
return Boolean.compare(SmokeFromPipe, another.SmokeFromPipe);
if (Window != another.Window)
return Boolean.compare(Window, another.Window);
return 0;
}

@Override
public boolean equals(Object another) {
if (another == null) {
return false;
}
if (!(another instanceof Ship_Liner)) {
return false;
}
Ship_Liner ship_LinerObf = (Ship_Liner) another;
return equals(ship_LinerObf);
}

public boolean equals(Ship_Liner another) {
if (another == null)
return false;
if (!(another instanceof Ship_Liner)) {
return false;
}
if (MaxSpeed != another.MaxSpeed)
return false;
if (Weight != another.Weight)
return false;
if (MainColor != another.MainColor)
return false;
if (DopColor != another.DopColor)
return false;
if (Pipe != another.Pipe)
return false;
if (SmokeFromPipe != another.SmokeFromPipe)
return false;
if (Window != another.Window)
return false;
return true;
}
}