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

add_home_work_advanced_oop #1781

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
29 changes: 29 additions & 0 deletions src/main/java/classes/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package classes;

public class Circle extends Figure {
private double radius;

public Circle(Color color, double radius) {
super(color);
this.radius = radius;
}

@Override
public String toString() {
return "Circle";
}

@Override
public String draw() {
StringBuilder infoMessage = new StringBuilder("Figure: " + Figures.Circle + ", ")
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
.append("area: " + this.calculateArea() + " sq. units" + ", ")
.append("radius: " + this.radius + " sq. units" + ", ")
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
.append("color: " + this.getColor() + ".");
return infoMessage.toString();
}

@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
5 changes: 5 additions & 0 deletions src/main/java/classes/Color.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package classes;

public enum Color {
RED, GREEN, BLUE, YELLOW, VIOLET, BLACK, WHITE;
}
11 changes: 11 additions & 0 deletions src/main/java/classes/ColorSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package classes;

import java.util.Random;

public class ColorSupplier {
public Color getRandomColor() {
Color[] colors = Color.values();
Random random = new Random();
return colors[random.nextInt(colors.length)];
}
}
5 changes: 5 additions & 0 deletions src/main/java/classes/Drowable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package classes;

public interface Drowable {
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
String draw();
}
19 changes: 19 additions & 0 deletions src/main/java/classes/Figure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package classes;

public abstract class Figure implements Drowable {
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
private Color color;

public Figure(Color color) {
this.color = color;
}

public Color getColor() {
return color;
}

public void setColor(Color color) {
this.color = color;
}

public abstract double calculateArea();
}
32 changes: 32 additions & 0 deletions src/main/java/classes/FigureSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package classes;

import java.util.Random;

public class FigureSupplier {
public static final double DEFAULT_CIRCLE_RADIUS = 10.0;
private Random random = new Random();
private ColorSupplier colorSupplier = new ColorSupplier();

private double getPositiveRandomNumber() {
return 1 + random.nextDouble(10);
}

public Figure getDefaultFigure() {
return new Circle(Color.WHITE, DEFAULT_CIRCLE_RADIUS);
}

public Figure getRandomFigure() {
Figures[] figures = Figures.values();
Color randomColor = colorSupplier.getRandomColor();
Figures randomFigureName = figures[random.nextInt(figures.length)];
double randomDouble = getPositiveRandomNumber();
return switch (randomFigureName) {
case Square -> new Square(randomColor, randomDouble);
case Rectangle -> new Rectangle(randomColor, randomDouble, randomDouble);
case Circle -> new Circle(randomColor, randomDouble);
case Right_triangle -> new RightTriangle(randomColor, randomDouble, randomDouble);
case Isosceles_Trapezoid ->
new IsoscelesTrapezoid(randomColor, randomDouble, randomDouble, randomDouble);
};
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
}
}
5 changes: 5 additions & 0 deletions src/main/java/classes/Figures.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package classes;

public enum Figures {
Square, Rectangle, Right_triangle, Circle, Isosceles_Trapezoid;
}
37 changes: 37 additions & 0 deletions src/main/java/classes/IsoscelesTrapezoid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package classes;

public class IsoscelesTrapezoid extends Figure {

private double baseTop;
private double baseBottom;
private double height;

public IsoscelesTrapezoid(Color color, double baseTop, double baseBottom, double height) {
super(color);
this.baseTop = baseTop;
this.baseBottom = baseBottom;
this.height = height;
}

@Override
public String toString() {
return "Isosceles trapezoid";
}

@Override
public String draw() {
StringBuilder infoMessage = new StringBuilder()
.append("Figure: " + Figures.Isosceles_Trapezoid + ", ")
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
.append("area: " + this.calculateArea() + " sq. units" + ", ")
.append("baseTop: " + this.baseTop + " sq. units" + ", ")
.append("baseBottom: " + this.baseBottom + " sq. units" + ", ")
.append("height: " + this.height + " sq. units" + ", ")
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
.append("color: " + this.getColor() + ".");
return infoMessage.toString();
}

@Override
public double calculateArea() {
return (baseTop + baseBottom) * 0.5 * height;
}
}
32 changes: 32 additions & 0 deletions src/main/java/classes/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package classes;

public class Rectangle extends Figure {
private double width;
private double height;

public Rectangle(Color color, double width, double height) {
super(color);
this.height = height;
this.width = width;
}

@Override
public String toString() {
return "Rectangle";
}

@Override
public String draw() {
StringBuilder infoMessage = new StringBuilder("Figure: " + Figures.Rectangle + ", ")
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
.append("area: " + this.calculateArea() + " sq. units" + ", ")
.append("width: " + this.width + " sq. units" + ", ")
.append("height: " + this.height + " sq. units" + ", ")
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
.append("color: " + this.getColor() + ".");
return infoMessage.toString();
}

@Override
public double calculateArea() {
return width * height;
}
}
32 changes: 32 additions & 0 deletions src/main/java/classes/RightTriangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package classes;

public class RightTriangle extends Figure {
private double firstLeg;
private double secondLeg;

public RightTriangle(Color color, double firstLeg, double secondLeg) {
super(color);
this.firstLeg = firstLeg;
this.secondLeg = secondLeg;
}

@Override
public String toString() {
return "right triangle";
}

@Override
public String draw() {
StringBuilder infoMessage = new StringBuilder("Figure: " + Figures.Right_triangle + ", ")
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
.append("area: " + this.calculateArea() + " sq. units" + ", ")
.append("firstLeg: " + this.firstLeg + " sq. units" + ", ")
.append("secondLeg: " + this.secondLeg + " sq. units" + ", ")
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
.append("color: " + this.getColor() + ".");
return infoMessage.toString();
}

@Override
public double calculateArea() {
return firstLeg * secondLeg * 0.5;
}
}
34 changes: 34 additions & 0 deletions src/main/java/classes/Square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package classes;

public class Square extends Figure {
private double side;

public Square(Color color, double side) {
super(color);
this.setColor(color);
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
this.side = side;
}

@Override
public String toString() {
return "Square";
}

@Override
public String draw() {
StringBuilder infoMessage = new StringBuilder("Figure: " + Figures.Square + ", ")
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
.append("area: " + this.calculateArea() + " sq. units" + ", ")
.append("side: " + this.side + " sq. units" + ", ")
DenDy1227 marked this conversation as resolved.
Show resolved Hide resolved
.append("color: " + this.getColor() + ".");
return infoMessage.toString();
}

@Override
public double calculateArea() {
return side * side;
}

public double getSide() {
return this.side;
}
}
25 changes: 25 additions & 0 deletions src/main/java/core/basesyntax/FiguresApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package core.basesyntax;

import classes.Figure;
import classes.FigureSupplier;

public class FiguresApp {
private static final int FIGURE_COUNT = 6;
private static final int HALF_COUNT = FIGURE_COUNT / 2;

public static void main(String[] args) {
FigureSupplier figureSupplier = new FigureSupplier();
Figure[] figuresArray = new Figure[FIGURE_COUNT];
for (int elementIndex = 0; elementIndex < FIGURE_COUNT; elementIndex++) {
if (elementIndex < HALF_COUNT) {
figuresArray[elementIndex] = figureSupplier.getRandomFigure();
} else {
figuresArray[elementIndex] = figureSupplier.getDefaultFigure();
}
}

for (Figure figure : figuresArray) {
System.out.println(figure.draw());
}
}
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

Loading