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

Implement tasks #1760

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
15 changes: 15 additions & 0 deletions src/main/java/core/basesyntax/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package core.basesyntax;

public class Application {
public static void main(String[] args) {
Figure[] figures = new Figure[6];

Choose a reason for hiding this comment

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

Consider replacing the magic number 6 with a named constant to improve code readability and maintainability. This aligns with the checklist requirement to avoid magic numbers in your code.

int halfSize = figures.length / 2;
for (int i = 0; i < halfSize; i++) {
figures[i] = FigureSupplier.getRandomFigure();

Choose a reason for hiding this comment

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

Avoid using static methods for getRandomFigure and getDefaultFigure. Instead, create an instance of FigureSupplier and call these methods on that instance. This aligns with the checklist item: 'Don't use static methods in your solution'.

figures[halfSize + i] = FigureSupplier.getDefaultFigure();

Choose a reason for hiding this comment

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

Similarly, avoid using static methods for getDefaultFigure. Create an instance of FigureSupplier before the loop and use it to call getDefaultFigure. This aligns with the checklist item: 'You should create several random Figures, so you will use a loop. Please don't create a new FigureSupplier() inside the loop.'

}
for (Figure figure : figures) {
figure.draw();
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/core/basesyntax/ColorSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package core.basesyntax;

import java.util.Random;

public class ColorSupplier {
public static String getRandomColor() {

Choose a reason for hiding this comment

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

Avoid using a static method for getRandomColor. Convert this method to an instance method by removing the static keyword. This aligns with the checklist item: 'Don't use static methods in your solution'.

StringBuilder sb = new StringBuilder("rgb(");
Random random = new Random();
int red = random.nextInt(256);
int green = random.nextInt(256);
int blue = random.nextInt(256);
sb.append(red)
.append(", ")
.append(green)
.append(", ")
.append(blue)
.append(")");
return sb.toString();
}
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/Drawable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

public interface Drawable {
void draw();
}
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 Drawable {
private final String color;

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

public String getColor() {
return color;
}

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

import core.basesyntax.models.Circle;
import core.basesyntax.models.IsoscelesTrapezoid;
import core.basesyntax.models.Rectangle;
import core.basesyntax.models.RightTriangle;
import core.basesyntax.models.Square;
import java.util.Random;

public class FigureSupplier {
public static Figure getRandomFigure() {

Choose a reason for hiding this comment

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

Avoid using a static method for getRandomFigure. Convert this method to an instance method by removing the static keyword. This aligns with the checklist item: 'Don't use static methods in your solution'.

Random random = new Random();
double squareSide = random.nextDouble();
double width = random.nextDouble();
double height = random.nextDouble();
double firstLeg = random.nextDouble();
double secondLeg = random.nextDouble();
double radius = random.nextDouble();
double bottom = random.nextDouble();
double top = random.nextDouble();
double trapSide = random.nextDouble();
Comment on lines +23 to +31

Choose a reason for hiding this comment

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

The random values for dimensions should be within a realistic range to ensure meaningful figures are generated. Consider setting a minimum and maximum value for these random numbers.

int randomFigureIndex = random.nextInt(Figures.values().length);
Figures figure = Figures.values()[randomFigureIndex];
Figure randomFigure = switch (figure) {
case SQUARE -> new Square(ColorSupplier.getRandomColor(), squareSide);
case RECTANGLE -> new Rectangle(ColorSupplier.getRandomColor(), width, height);
case RIGHT_TRIANGLE ->
new RightTriangle(ColorSupplier.getRandomColor(), firstLeg, secondLeg);
case CIRCLE -> new Circle(ColorSupplier.getRandomColor(), radius);
case ISOSCELES_TRAPEZOID ->
new IsoscelesTrapezoid(ColorSupplier.getRandomColor(), trapSide, bottom, top);
};
return randomFigure;
}

public static Figure getDefaultFigure() {

Choose a reason for hiding this comment

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

Avoid using a static method for getDefaultFigure. Convert this method to an instance method by removing the static keyword. This aligns with the checklist item: 'Don't use static methods in your solution'.

return new Circle("rgb(255, 255, 255)", 10.0);
}
}
9 changes: 9 additions & 0 deletions src/main/java/core/basesyntax/Figures.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package core.basesyntax;

public enum Figures {
SQUARE,
RECTANGLE,
RIGHT_TRIANGLE,
CIRCLE,
ISOSCELES_TRAPEZOID
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

27 changes: 27 additions & 0 deletions src/main/java/core/basesyntax/models/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package core.basesyntax.models;

import core.basesyntax.Figure;

public class Circle extends Figure {
private final double radius;

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

@Override
public double getArea() {
return Math.PI * radius * radius;
}

@Override
public void draw() {
System.out.printf(
"Figure: circle, area: %.1f sq. units, radius: %.1f units, color: %s\n",
this.getArea(),
this.radius,
this.getColor()
);
}
}
34 changes: 34 additions & 0 deletions src/main/java/core/basesyntax/models/IsoscelesTrapezoid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package core.basesyntax.models;

import core.basesyntax.Figure;

public class IsoscelesTrapezoid extends Figure {
private final double side;
private final double bottom;
private final double top;

public IsoscelesTrapezoid(String color, double side, double bottom, double top) {
super(color);
this.side = side;
this.bottom = bottom;
this.top = top;
}

@Override
public double getArea() {
return ((bottom + top) / 4) * Math.sqrt(4 * side * side - Math.pow((bottom - top), 2));
}

@Override
public void draw() {
System.out.printf(
"Figure: isosceles trapezoid, area: %.1f sq. units, side: %.1f units,"
+ " bottom: %.1f units, top: %.1f units, color: %s\n",
this.getArea(),
this.side,
this.bottom,
this.top,
this.getColor()
);
}
}
31 changes: 31 additions & 0 deletions src/main/java/core/basesyntax/models/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package core.basesyntax.models;

import core.basesyntax.Figure;

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

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

@Override
public double getArea() {
return width * height;
}

@Override
public void draw() {
System.out.printf(
"Figure: rectangle, area: %.1f sq. units, width: %.1f units,"
+ " height: %.1f units, color: %s\n",
this.getArea(),
this.width,
this.height,
this.getColor()
);
}
}
31 changes: 31 additions & 0 deletions src/main/java/core/basesyntax/models/RightTriangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package core.basesyntax.models;

import core.basesyntax.Figure;

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

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

@Override
public double getArea() {
return firstLeg * secondLeg / 2;
}

@Override
public void draw() {
System.out.printf(
"Figure: right triangle, area: %.1f sq. units, first leg: %.1f units,"
+ " second leg: %.1f units, color: %s\n",
this.getArea(),
this.firstLeg,
this.secondLeg,
this.getColor()
);
}
}
27 changes: 27 additions & 0 deletions src/main/java/core/basesyntax/models/Square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package core.basesyntax.models;

import core.basesyntax.Figure;

public class Square extends Figure {
private final double side;

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

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

@Override
public void draw() {
System.out.printf(
"Figure: square, area: %.1f sq. units, side: %.1f units, color: %s\n",
this.getArea(),
this.side,
this.getColor()
);
}
}
Loading