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

created a program that generate the array with random/default figures #1771

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

public class Circle extends Figure {
private int radius;

public Circle(int radius) {
this.radius = radius;
}

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

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

public enum Color {
BLUE,
YELLOW,
BLACK,
BROWN,
PINK,
GREEN,
WHITE
}
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 final Random random = new Random();

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

public class Ellipse extends Figure {
private double lengthA;
private double lengthB;

public Ellipse(double sideA, double lengthB) {
this.lengthA = sideA;
this.lengthB = lengthB;
}

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

@Override
public String printInformation() {
return "Figure: Rectangle, area: " + getArea()

Choose a reason for hiding this comment

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

The printInformation method incorrectly refers to the figure as a 'Rectangle'. It should be 'Ellipse' to accurately describe the figure type.

+ "sq. units, lengthA: " + lengthA + " units, lengthB: "
+ lengthB + " units, color: " + getColor();
}
}
17 changes: 17 additions & 0 deletions src/main/java/core/basesyntax/Figure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package core.basesyntax;

public abstract class Figure implements FigureSquare, FigureInformation {
private String color;

Copy link

Choose a reason for hiding this comment

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

consider creating a constructor for color and calling super() in subclasses

public String getColor() {
return color;
}

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

public abstract double getArea();

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

public interface FigureInformation {
String printInformation();
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/FigureSquare.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

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

import java.util.Random;

public class FigureSupplier {
private static final int FIRST_MAX_NUMBER = 5;
private static final int SECOND_MAX_NUMBER = 100;
private static final int RADIUS = 10;
private final Random random;
private final ColorSupplier colorSupplier;

public FigureSupplier(Random random, ColorSupplier colorSupplier) {
this.random = random;
this.colorSupplier = colorSupplier;
}

public Figure getRandomFigure() {
int max1 = random.nextInt(SECOND_MAX_NUMBER) + 1;
int max2 = random.nextInt(SECOND_MAX_NUMBER) + 1;
Figure figure = switch (random.nextInt(FIRST_MAX_NUMBER) + 1) {
case 1 -> new Rectangle(max1, max2);
case 2 -> new RightTriangle(max1, max2);
case 3 -> new Ellipse(max1, max2);
case 4 -> new Square(max1);
case 5 -> new IsoscelesTrapezoid(max1, max2, max2);
default -> throw new IllegalArgumentException("Unexpected value");

Choose a reason for hiding this comment

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

The default case in the switch statement is unnecessary because the random number generation is controlled by FIRST_MAX_NUMBER and should not exceed this range. Consider removing the default case or handling it in a way that aligns with the expected behavior.

Copy link

Choose a reason for hiding this comment

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

IllegalState is more applicable here, cuz this method doesn't have arguments, problem can only occur via developer's mistake, misconfiguration of lengths. You can also make case 5 default

};
figure.setColor(colorSupplier.getRandomColor());
return figure;
}

public Figure getDefaultFigure() {
Figure figure = new Circle(RADIUS);
figure.setColor("WHITE");
return figure;
}
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

public class IsoscelesTrapezoid extends Figure {
private double sideA;
private double sideB;
private double height;

public IsoscelesTrapezoid(double sideA, double sideB, double height) {
this.sideA = sideA;
this.sideB = sideB;
this.height = height;
}

@Override
public double getArea() {
return (sideA + sideB) / 2 * height;
}

@Override
public String printInformation() {
return "Figure: RightTriangle, area: " + getArea()

Choose a reason for hiding this comment

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

The printInformation method incorrectly refers to the figure as a 'RightTriangle'. It should be 'IsoscelesTrapezoid' to accurately describe the figure type.

+ "sq. units, sideA: " + sideA + " units, sideB: " + sideB + " units, height: "
+ height + " units, color: " + getColor();
}
}
23 changes: 23 additions & 0 deletions src/main/java/core/basesyntax/MainApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package core.basesyntax;

import java.util.Random;

public class MainApp {
public static void main(String[] args) {
ColorSupplier colorSupplier = new ColorSupplier();
Random random = new Random();

FigureSupplier figureSupplier = new FigureSupplier(random, colorSupplier);
Figure[] figures = new Figure[6];

for (int i = 0; i < figures.length; i++) {
if (i < figures.length / 2) {
figures[i] = figureSupplier.getRandomFigure();
System.out.println(figures[i].printInformation());
} else {
figures[i] = figureSupplier.getDefaultFigure();
System.out.println(figures[i].printInformation());
Copy link

Choose a reason for hiding this comment

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

extract common part from if-else

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

public class Rectangle extends Figure {
private double sideA;
private double sideB;

public Rectangle(double sideA, double sideB) {
this.sideA = sideA;
this.sideB = sideB;
}

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

@Override
public String printInformation() {
return "Figure: Rectangle, area: " + getArea()
+ "sq. units, sideA: " + sideA + " units, sideB: "
+ sideB + " units, color: " + getColor();
}
}
23 changes: 23 additions & 0 deletions src/main/java/core/basesyntax/RightTriangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package core.basesyntax;

public class RightTriangle extends Figure {
private double base;
private double height;

public RightTriangle(double base, double height) {
this.base = base;
this.height = height;
}

@Override
public double getArea() {
return base * height / 2;
}

@Override
public String printInformation() {
return "Figure: RightTriangle, area: " + getArea()
+ "sq. units, base: " + base + " units, height: "
+ height + " units, color: " + getColor();
}
}
20 changes: 20 additions & 0 deletions src/main/java/core/basesyntax/Square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package core.basesyntax;

public class Square extends Figure {
private int side;

public Square(int side) {
this.side = side;
}

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

@Override
public String printInformation() {
return "Figure: Square, area: " + getArea()
+ "sq. units, side: " + side + " units, color: " + getColor();
}
}
Loading