diff --git a/src/containers/Backpack.java b/src/containers/Backpack.java new file mode 100644 index 0000000..2f8bdf3 --- /dev/null +++ b/src/containers/Backpack.java @@ -0,0 +1,44 @@ +package containers; + +import data.Shape; +import exception.BackpackOverflow; + +import java.util.ArrayList; + +public class Backpack { + private ArrayList elements; + private final int n; + + public Backpack(int n) { + this.n = n; + try { + elements = new ArrayList<>(); + } catch (NegativeArraySizeException e) { + System.out.println("NegativeArraySize"); + } + } + + public int add(T element) { + if (elements.size() == n) { + throw new BackpackOverflow(); + } else { + elements.add(element); + sort(); + return elements.indexOf(element); + } + } + + public void remove(int i) { + if (elements.size() != 0) { + elements.remove(i); + } + } + + public int size() { + return elements.size(); + } + + public void sort() { + elements.sort((a, b)-> (int)(b.getVolume() - a.getVolume())); + } +} diff --git a/src/controller/Controller.java b/src/controller/Controller.java new file mode 100644 index 0000000..ec9e382 --- /dev/null +++ b/src/controller/Controller.java @@ -0,0 +1,27 @@ +package controller; + +import containers.Backpack; +import data.Shape; +import gui.GUI; + +public class Controller { + private final int BACKPACK_CAPACITY = 100; + private final Backpack backpack = new Backpack<>(BACKPACK_CAPACITY); + + public Controller() { + GUI app = new GUI(this); + app.setVisible(true); + } + + public String backpackStat() { + return "Backpack Size: " + backpack.size() + ", Backpack Capacity: " + BACKPACK_CAPACITY; + } + + public int addElementToBackpack(Shape new_element) { + return backpack.add(new_element); + } + + public void remove(int i) { + backpack.remove(i); + } +} diff --git a/src/data/Ball.java b/src/data/Ball.java new file mode 100644 index 0000000..51799ed --- /dev/null +++ b/src/data/Ball.java @@ -0,0 +1,19 @@ +package data; + +public class Ball extends Shape { + double radius; + + public Ball(double radius) { + this.radius = radius; + } + + @Override + public double getVolume() { + return 4.0 / 3 * Math.PI * Math.pow(radius, 3); + } + + @Override + public String toString() { + return "Ball with radius " + radius + " and Volume " + RoundAvoid(getVolume(), 4); + } +} diff --git a/src/data/Cube.java b/src/data/Cube.java new file mode 100644 index 0000000..307e7cf --- /dev/null +++ b/src/data/Cube.java @@ -0,0 +1,19 @@ +package data; + +public class Cube extends Shape { + double edge; + + public Cube(double edge) { + this.edge = edge; + } + + @Override + public double getVolume() { + return Math.pow(edge, 3); + } + + @Override + public String toString() { + return "Cube with edge " + edge + " and Volume " + RoundAvoid(getVolume(), 4); + } +} diff --git a/src/data/Shape.java b/src/data/Shape.java new file mode 100644 index 0000000..c152a62 --- /dev/null +++ b/src/data/Shape.java @@ -0,0 +1,11 @@ +package data; + +public abstract class Shape { + public abstract double getVolume(); + + protected double RoundAvoid(double value, int places) { + double scale = Math.pow(10, places); + return Math.round(value * scale) / scale; + } +} + diff --git a/src/data/Tetrahedron.java b/src/data/Tetrahedron.java new file mode 100644 index 0000000..4021ff0 --- /dev/null +++ b/src/data/Tetrahedron.java @@ -0,0 +1,19 @@ +package data; + +public class Tetrahedron extends Shape { + double edge; + + public Tetrahedron(double edge) { + this.edge = edge; + } + + @Override + public double getVolume() { + return Math.pow(edge, 3) / (6 * Math.sqrt(2)); + } + + @Override + public String toString() { + return "Tetrahedron with edge " + edge + " and Volume " + RoundAvoid(getVolume(), 4); + } +} diff --git a/src/exception/BackpackOverflow.java b/src/exception/BackpackOverflow.java new file mode 100644 index 0000000..0c5af45 --- /dev/null +++ b/src/exception/BackpackOverflow.java @@ -0,0 +1,7 @@ +package exception; + +public class BackpackOverflow extends RuntimeException { + public BackpackOverflow() { + super("Can`t add anymore to backpack"); + } +} diff --git a/src/gui/GUI.java b/src/gui/GUI.java new file mode 100644 index 0000000..a39934e --- /dev/null +++ b/src/gui/GUI.java @@ -0,0 +1,127 @@ +package gui; + +import controller.Controller; +import data.Ball; +import data.Cube; +import data.Shape; +import data.Tetrahedron; + +import javax.swing.*; +import java.awt.*; +import java.util.regex.Pattern; + +public class GUI extends JFrame { + private final DefaultListModel listModel = new DefaultListModel<>(); + private final JList list = new JList<>(listModel); + + private final JButton addCube = new JButton(); + private final JButton addBall = new JButton(); + private final JButton addTetrahedron = new JButton(); + private final JButton deleteComponent = new JButton(); + + private final JLabel backpackStat = new JLabel(); + + private final Controller controller; + private final String[] statuses = new String[]{"edge of cube", "ball radius", "edge of tetrahedron"}; + + public GUI(Controller controller) { + this.controller = controller; + JScrollPane scrollPane = new JScrollPane(); + scrollPane.add(list); + + setMinimumSize(new Dimension(800, 800)); + setTitle("Backpack v0.1"); + setLocationRelativeTo(null); + setDefaultCloseOperation(EXIT_ON_CLOSE); + + createLayout(getContentPane()); + setElementsInfo(); + } + + private void createLayout(Container pane) { + BorderLayout layout = new BorderLayout(); + pane.setLayout(layout); + pane.add(list, BorderLayout.CENTER); + pane.add(backpackStat, BorderLayout.SOUTH); + JPanel panel = new JPanel(); + panel.setLayout(new GridLayout(0, 1)); + panel.add(addCube); + panel.add(addBall); + panel.add(addTetrahedron); + panel.add(deleteComponent); + pane.add(panel, BorderLayout.EAST); + } + + private final Pattern NUM_EX = Pattern.compile("-?\\d+(\\.\\d+)?"); + + private boolean isNumeric(String strNum) { + return NUM_EX.matcher(strNum).matches(); + } + + private void setElementsInfo() { + addCube.setText("Add cube"); + addCube.addActionListener(actionEvent -> displayInputDialog(0)); + addBall.setText("Add ball"); + addBall.addActionListener(actionEvent -> displayInputDialog(1)); + addTetrahedron.setText("Add tetrahedron"); + addTetrahedron.addActionListener(actionEvent -> displayInputDialog(2)); + + deleteComponent.setText("Remove selected shape from Backpack"); + deleteComponent.addActionListener(actionEvent -> { + if (!list.isSelectionEmpty()) { + int index = list.getSelectedIndex(); + listModel.remove(index); + controller.remove(index); + backpackStat.setText(controller.backpackStat()); + } else { + JOptionPane.showMessageDialog(GUI.this, + "Select shape to delete first", + "Error", + JOptionPane.ERROR_MESSAGE); + } + }); + + list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + list.setLayoutOrientation(JList.HORIZONTAL_WRAP); + list.setVisibleRowCount(-1); + + backpackStat.setText(controller.backpackStat()); + } + + private void displayInputDialog(int type) { + String message = JOptionPane.showInputDialog( + GUI.this, + "

Enter " + statuses[type] + " (non-negative number)"); + if (message != null && isNumeric(message) && Integer.parseInt(message) >= 0) { + switch (type) { + case 0: { + addToBackpack(new Cube(Integer.parseInt(message))); + break; + } + case 1: { + addToBackpack(new Ball(Integer.parseInt(message))); + break; + } + case 2: { + addToBackpack(new Tetrahedron(Integer.parseInt(message))); + break; + } + } + } else { + inputError(); + } + } + + private void addToBackpack(Shape shape) { + int index = controller.addElementToBackpack(shape); + listModel.add(index, shape); + backpackStat.setText(controller.backpackStat()); + } + + private void inputError() { + JOptionPane.showMessageDialog(GUI.this, + "Seems, you enter something that is not a non-negative number :)", + "Error", + JOptionPane.ERROR_MESSAGE); + } +} diff --git a/src/main/Main.java b/src/main/Main.java new file mode 100644 index 0000000..459103d --- /dev/null +++ b/src/main/Main.java @@ -0,0 +1,9 @@ +package main; + +import controller.Controller; + +public class Main { + public static void main(String[] args) { + Controller controller = new Controller(); + } +}