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

Done 02-05/10/23 #1310

Open
wants to merge 1 commit into
base: master
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
21 changes: 21 additions & 0 deletions src/main/java/core/basesyntax/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.basesyntax;

public class Circle extends Figure {
private double radius;

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

@Override
public void calculateSquare() {
System.out.println("Square circle = " + Math.PI * radius * radius);
}

@Override
String getInfo() {
return "Figure: Circle, area: " + Math.PI * radius * radius
+ " sq.units, radius: " + radius + " units, color: " + getColor();
}
}
10 changes: 10 additions & 0 deletions src/main/java/core/basesyntax/Color.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core.basesyntax;

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

import java.util.Random;

public class ColorSupplier {
private Random random = new Random();

public String getRandomColor() {
int index = random.nextInt(Color.values().length);
return Color.values()[index].name();
}
}
15 changes: 15 additions & 0 deletions src/main/java/core/basesyntax/Figure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package core.basesyntax;

public abstract class Figure implements SquareOfFigures {
private String color;

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

public String getColor() {
return color;
}

abstract String getInfo();
}
35 changes: 35 additions & 0 deletions src/main/java/core/basesyntax/FigureSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package core.basesyntax;

import java.util.Random;

public class FigureSupplier {
private static final int MAX_FIGURE = 6;
private Random random = new Random();
private ColorSupplier colorSupplier = new ColorSupplier();

public Figure getRandomFigure() {
int randomNumberOfFigure = random.nextInt(MAX_FIGURE);
switch (randomNumberOfFigure) {
case 1:
return new Circle(colorSupplier.getRandomColor(), random.nextInt(10));
case 2:
return new IsoscelesTrapezoid(random.nextInt(10),
random.nextInt(10), random.nextInt(10),
Comment on lines +16 to +17
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please extract magic numbers to constants with proper naming

colorSupplier.getRandomColor());
case 3:
return new Rectangle(colorSupplier.getRandomColor(),
random.nextInt(10), random.nextInt(10));
case 4:
return new RightTriangle(colorSupplier.getRandomColor(),
random.nextInt(10), random.nextInt(10));
case 5:
return new Square(colorSupplier.getRandomColor(), random.nextInt(10));
default:
return getDefaultFigure();
}
}

public Figure getDefaultFigure() {
return new Circle(Color.WHITE.name(), 10);
}
}
27 changes: 27 additions & 0 deletions src/main/java/core/basesyntax/IsoscelesTrapezoid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package core.basesyntax;

public class IsoscelesTrapezoid extends Figure {
private double sideOne;
private double sideTwo;
private double height;

public IsoscelesTrapezoid(double sideOne, double sideTwo, double height, String color) {
super(color);
this.sideOne = sideOne;
this.sideTwo = sideTwo;
this.height = height;
}

@Override
public void calculateSquare() {
System.out.println("Square Of IsoscelesTrapezoid = " + (sideOne + sideTwo) / 2 * height);

}

@Override
String getInfo() {
return "Figure: IsoscelesTrapezoid, area: " + (sideOne + sideTwo) / 2 * height
+ " sq.units, " + "sideOne " + sideOne + " units, " + "sideTwo "
+ sideTwo + " units, " + "height " + height + ", color: " + getColor();
}
}
16 changes: 16 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package core.basesyntax;

public class Main {
public static void main(String[] args) {
FigureSupplier figureSupplier = new FigureSupplier();
Figure[] figures = new Figure[6];
for (int i = 0; i < figures.length; i++) {
if (i < 3) {
figures[i] = figureSupplier.getRandomFigure();
} else {
figures[i] = figureSupplier.getDefaultFigure();
}
System.out.println(figures[i].getInfo());
}
}
}
24 changes: 24 additions & 0 deletions src/main/java/core/basesyntax/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax;

public class Rectangle extends Figure {
private double sideOne;
private double sideTwo;

public Rectangle(String color, double sideOne, double sideTwo) {
super(color);
this.sideOne = sideOne;
this.sideTwo = sideTwo;
}

@Override
public void calculateSquare() {
System.out.println("Square of Rectangle = " + sideOne * sideTwo);
}

@Override
String getInfo() {
return "Figure: Rectangle, area: " + sideOne * sideTwo
+ " sq.units, " + "sideOne " + sideOne + " units, " + "sideTwo "
+ sideTwo + " units, " + "color: " + getColor();
}
}
24 changes: 24 additions & 0 deletions src/main/java/core/basesyntax/RightTriangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax;

public class RightTriangle extends Figure {
private double sideOne;
private double sideTwo;

public RightTriangle(String color, double sideOne, double sideTwo) {
super(color);
this.sideOne = sideOne;
this.sideTwo = sideTwo;
}

@Override
public void calculateSquare() {
System.out.println("Square of RightTriangle = " + (sideOne * sideTwo) / 2);
}

@Override
String getInfo() {
return "Figure: RightTriangle, area: " + (sideOne * sideTwo) / 2
+ " sq.units, " + "sideOne " + sideOne + " units, " + "sideTwo "
+ sideTwo + " units, " + "color: " + getColor();
}
}
21 changes: 21 additions & 0 deletions src/main/java/core/basesyntax/Square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.basesyntax;

public class Square extends Figure {
private double side;

public Square(String color, double side) {
super(color);
this.side = side;
}

@Override
public void calculateSquare() {
System.out.println("Square of square = " + side * side);
}

@Override
String getInfo() {
return "Figure: Square, area: " + side
+ " sq.units, " + "side " + side + " units, " + "color: " + getColor();
}
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/SquareOfFigures.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

public interface SquareOfFigures {
void calculateSquare();
}
Loading