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 #1775

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open

Done #1775

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

public class Circle extends Figure {
private final float radius;

public Circle(Color color, float radius) {

Choose a reason for hiding this comment

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

Suggested change
public Circle(Color color, float radius) {
public Circle(String color, float radius) {

super(color, "Circle");

Choose a reason for hiding this comment

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

Suggested change
super(color, "Circle");
super(color);

this.radius = radius;
}

@Override
public double area() {
return Math.PI * Math.pow(radius, 2);
}

@Override
public void draw() {
System.out.println("Figure: " + getName() + ", area: "
+ area() + " sq. units, radius: "
+ radius + " units, color: " + getColor());
}
}
9 changes: 9 additions & 0 deletions src/main/java/core/basesyntax/Color.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package core.basesyntax;

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

import java.util.Random;

public final class ColorSupplier {

Choose a reason for hiding this comment

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

Suggested change
public final class ColorSupplier {
public class ColorSupplier {

private final Random picker = new Random();

Choose a reason for hiding this comment

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

Suggested change
private final Random picker = new Random();
private final Random random = new Random();


public Color getRandomColor() {

Choose a reason for hiding this comment

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

Suggested change
public Color getRandomColor() {
public String getRandomColor() {

return Color.values()[picker.nextInt(Color.values().length)];
}
}
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();
}
29 changes: 29 additions & 0 deletions src/main/java/core/basesyntax/Figure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package core.basesyntax;

public abstract class Figure implements Drawable {
private Color color;

Choose a reason for hiding this comment

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

Suggested change
private Color color;
private String color;

private String name;

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

public Color getColor() {
return this.color;
}

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

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public abstract double area();

Choose a reason for hiding this comment

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

create interface AreaCalculator with method getArea()

Suggested change
public abstract double area();

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

import java.util.Random;

public final class FigureSupplier {

Choose a reason for hiding this comment

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

Suggested change
public final class FigureSupplier {
public class FigureSupplier {

private final Random picker = new Random();
private final ColorSupplier colorSupplier = new ColorSupplier();

public Figure getRandomFigure() {
final int figureCount = 5;
final int startNum = 1;
final int limitNum = 50;

Choose a reason for hiding this comment

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

Suggested change
final int limitNum = 50;
final int limitNum = 50;
String randomColor = colorSupplier.getRandomColor();


return switch (picker.nextInt(figureCount)) {
case 0 -> new Circle(colorSupplier.getRandomColor(),

Choose a reason for hiding this comment

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

same for other figures

Suggested change
case 0 -> new Circle(colorSupplier.getRandomColor(),
case 0 -> new Circle(randomColor,

picker.nextInt(startNum, limitNum));

Choose a reason for hiding this comment

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

create method separate method to get picker.nextInt(startNum, limitNum)); and reuse it

case 1 -> new Rectangle(colorSupplier.getRandomColor(),
picker.nextInt(startNum, limitNum),
picker.nextInt(startNum, limitNum));
case 2 -> new RightTriangle(colorSupplier.getRandomColor(),
picker.nextInt(startNum, limitNum),
picker.nextInt(startNum, limitNum));
case 3 -> new Square(colorSupplier.getRandomColor(),
picker.nextInt(startNum, limitNum));
case 4 -> new IsoscelesTrapezoid(colorSupplier.getRandomColor(),
picker.nextInt(startNum, limitNum),
picker.nextInt(startNum, limitNum),
picker.nextInt(startNum, limitNum));
default -> new Circle(Color.WHITE, 10);

Choose a reason for hiding this comment

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

10 should be constant

};
}

public Figure getDefaultFigure() {
return new Circle(Color.WHITE, 10);
}
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

public class IsoscelesTrapezoid extends Figure {
private final float firstBase;
private final float secondBase;
private final float height;

public IsoscelesTrapezoid(Color color, float firstBase, float secondBase, float height) {
super(color, "IsoscelesTrapezoid");
this.firstBase = firstBase;
this.secondBase = secondBase;
this.height = height;
}

@Override
public double area() {
return ((firstBase + secondBase) / 2) * height;
}

@Override
public void draw() {
System.out.println("Figure: " + getName()
+ ", area: " + area() + " sq. units, firstBase: "
+ firstBase + " units, secondBase: "
+ secondBase + " units, height: "
+ height + " units, color: " + getColor());
}
}
31 changes: 31 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package core.basesyntax;

public class Main {
private static final FigureSupplier figureSupplier = new FigureSupplier();

public static void main(String[] args) {
printArray();

Choose a reason for hiding this comment

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

redundant separate method, create Figure[] here, 6 - should be constant, use one for loop for this task

}

private static Figure[] listOfRandomFigures() {
Figure[] array = new Figure[6];

for (int i = 0; i < 3; i++) {
array[i] = figureSupplier.getRandomFigure();
}

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

return array;
}

private static void printArray() {
final Figure[] listOfRandomFigures = listOfRandomFigures();

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

public class Rectangle extends Figure {
private final float firstSide;
private final float secondSide;

public Rectangle(Color color, float firstSide, float secondSide) {
super(color, "Rectangle");
this.firstSide = firstSide;
this.secondSide = secondSide;
}

@Override
public double area() {
return firstSide * secondSide;
}

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

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

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

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

@Override
public void draw() {
System.out.println("Figure: " + getName()
+ ", area: " + area() + " sq. units, firstLeg: "
+ firstLeg + " units, secondLeg: "
+ secondLeg + " units, color: " + getColor());
}
}
22 changes: 22 additions & 0 deletions src/main/java/core/basesyntax/Square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package core.basesyntax;

public class Square extends Figure {
private final float side;

public Square(Color color, float side) {
super(color, "Square");
this.side = side;
}

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

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