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

public interface AreaCalculator {
double getArea();
}
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 final double radius;

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

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

@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
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 {
BLUE,
RED,
YELLOW,
GREEN,
BLACK
}
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 Color[] colors = Color.values();

public String getRandomColor() {
return colors[random.nextInt(colors.length)].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();
}
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 Drawable, AreaCalculator {
private final String color;

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

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

import java.util.Random;

public class FigureSupplier {
static final int MAX_FIGURE_QUANTITY = 5;
static final double FIGURE_RADIUS = 9.0 + 1.0;
static final String DEFAULT_CIRCLE_COLOUR = Color.BLUE.name();
private final Random random = new Random();
private final ColorSupplier colorSupplier = new ColorSupplier();

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

switch (randomFigure) {
case 0:
return new Circle(figureColor, random.nextDouble() * FIGURE_RADIUS);
case 1:
return new IsoscelesTrapezoid(figureColor, random.nextDouble() * FIGURE_RADIUS,
random.nextDouble() * FIGURE_RADIUS, random.nextDouble() * FIGURE_RADIUS);
case 2:
return new Rectangle(figureColor, random.nextDouble() * FIGURE_RADIUS,
random.nextDouble() * FIGURE_RADIUS);
case 3:
return new RightTriangle(figureColor, random.nextDouble() * FIGURE_RADIUS,
random.nextDouble() * FIGURE_RADIUS);
case 4:
return new Square(figureColor, random.nextDouble() * FIGURE_RADIUS);
default:
return getDefaultFigure();
}
}

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

This file was deleted.

26 changes: 26 additions & 0 deletions src/main/java/core/basesyntax/IsoscelesTrapezoid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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 draw() {
System.out.println("Figure: trapezoid, area: " + getArea()
+ " sq.units, side1: " + side1 + " sq.units, side2: " + side2
+ " sq.units, height: " + height + " units, color: " + getColor());
}

@Override
public double getArea() {
return 0.5 * (side1 + side2) * height;
}
}
18 changes: 18 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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++) {
figures[i] = (i < 3) ? figureSupplier.getRandomFigure()
: figureSupplier.getDefaultFigure();
}

for (Figure figure : figures) {
figure.draw();
}
}
}
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 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 draw() {
System.out.println("Figure: rectangle, area: " + getArea() + " sq.units, length: "
+ length + " sq.units, width: " + width + " units, color: " + getColor());
}

@Override
public double getArea() {
return length * width;
}
}
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 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 draw() {
System.out.println("Figure: triangle, area: " + getArea()
+ " sq.units, firstLeg: " + firstLeg + " sq.units, secondLeg: " + secondLeg
+ " units, color: " + getColor());
}

@Override
public double getArea() {
return 0.5 * firstLeg * secondLeg;
}
}
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 final double side;

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

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

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