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 classes, interface and abstract class for them, class ColorSupplier with public String getRandomColor(), class FigureSupplier with public Figure getRandomFigure() and getDefaultFigure(), also created main with random and default figures #1314

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
16 changes: 16 additions & 0 deletions src/main/java/core/basesyntax/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package core.basesyntax;

public class Circle extends Figure {
private final double radius;

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

@Override
public void printfInfoAboutFigure() {
System.out.println("Figure: circle, area: " + Math.PI * radius * radius
+ " sq.units, radius: " + radius + " units, color: " + getColor());
}
}
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();
private final String[] colors = new String[]{"Blue", "Red", "Yellow", "Green", "Black"};

Choose a reason for hiding this comment

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

create Enum Color instead of String

Suggested change
private final String[] colors = new String[]{"Blue", "Red", "Yellow", "Green", "Black"};


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

public abstract class Figure implements FigureBehaviour {

Choose a reason for hiding this comment

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

You missed one behavior:
image

Suggested change
public abstract class Figure implements FigureBehaviour {
public abstract class Figure implements AreaCalculator, Drawable {
public interface AreaCalculator {
    double getArea();
}

private final String color;

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

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

public interface FigureBehaviour {
void printfInfoAboutFigure();

Choose a reason for hiding this comment

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

Suggested change
public interface FigureBehaviour {
void printfInfoAboutFigure();
public interface Drawable {
void draw();

}
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 final Random random = new Random();
private final ColorSupplier colorSupplier = new ColorSupplier();

public Figure getRandomFigure() {
String figureColor = colorSupplier.getRandomColor();
int randomFigure = random.nextInt(5);

Choose a reason for hiding this comment

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

use constants instead of numbers


switch (randomFigure) {
case 0:
return new Circle(figureColor, random.nextDouble() * 9.0 + 1.0);

Choose a reason for hiding this comment

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

All magic numbers, strings, and symbols in your code should be constants.
this article

case 1:
return new IsoscelesTrapezoid(figureColor, random.nextDouble() * 9.0 + 1.0,
random.nextDouble() * 9.0 + 1.0, random.nextDouble() * 9.0 + 1.0);
case 2:
return new Rectangle(figureColor, random.nextDouble() * 9.0 + 1.0,
random.nextDouble() * 9.0 + 1.0);
case 3:
return new RightTriangle(figureColor, random.nextDouble() * 9.0 + 1.0,
random.nextDouble() * 9.0 + 1.0);
case 4:
return new Square(figureColor, random.nextDouble() * 9.0 + 1.0);
default:
return null;

Choose a reason for hiding this comment

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

Suggested change
return null;
return getDefaultFigure();

}
}

public Figure getDefaultFigure() {
return new Circle("White", 10);

Choose a reason for hiding this comment

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

All magic numbers, strings, and symbols in your code should be constants.
this article

}
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

public class IsoscelesTrapezoid extends Figure {
private final double side1;
private final double side2;
private final double height;

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

@Override
public void printfInfoAboutFigure() {
System.out.println("Figure: trapezoid, area: " + 0.5 * (side1 + side2) * height
+ " sq.units, side1: " + side1 + " sq.units, side2: " + side2
+ " sq.units, height: " + height + " units, color: " + getColor());
}
}
21 changes: 21 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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 < 3; i++) {

Choose a reason for hiding this comment

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

use one loop for

figures[i] = figureSupplier.getRandomFigure();
}

for (int i = 3; i < 6; i++) {
figures[i] = figureSupplier.getDefaultFigure();
}

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

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

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

@Override
public void printfInfoAboutFigure() {
System.out.println("Figure: rectangle, area: " + length * width + " sq.units, length: "
+ length + " sq.units, width: " + width + " units, color: " + getColor());
}
}
19 changes: 19 additions & 0 deletions src/main/java/core/basesyntax/RightTriangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package core.basesyntax;

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 void printfInfoAboutFigure() {
System.out.println("Figure: triangle, area: " + 0.5 * firstLeg * secondLeg
+ " sq.units, firstLeg: " + firstLeg + " sq.units, secondLeg: " + secondLeg
+ " units, color: " + getColor());
}
}
16 changes: 16 additions & 0 deletions src/main/java/core/basesyntax/Square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package core.basesyntax;

public class Square extends Figure {
private final double side;

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

@Override
public void printfInfoAboutFigure() {
System.out.println("Figure: square, area: " + side * side + " sq.units, side: "
+ side + " units, color: " + getColor());
}
}