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

public class Circle extends Figure {
private int radius;

public Circle(int radius, String color) {
super(color);
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();
}
}
24 changes: 24 additions & 0 deletions src/main/java/core/basesyntax/Ellipse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax;

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

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

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

@Override
public String printInformation() {
return "Figure: Ellipse, area: " + getArea()
+ "sq. units, lengthA: " + lengthA + " units, lengthB: "
+ lengthB + " units, color: " + getColor();
}
}
21 changes: 21 additions & 0 deletions src/main/java/core/basesyntax/Figure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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 Figure(String color) {
this.color = color;
}

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();
}
34 changes: 34 additions & 0 deletions src/main/java/core/basesyntax/FigureSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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;
String randomColor = colorSupplier.getRandomColor();
Figure figure = switch (random.nextInt(FIRST_MAX_NUMBER) + 1) {
case 1 -> new Rectangle(max1, max2, randomColor);
case 2 -> new RightTriangle(max1, max2, randomColor);
case 3 -> new Ellipse(max1, max2, randomColor);
case 4 -> new Square(max1, randomColor);
default -> new IsoscelesTrapezoid(max1, max2, max2, randomColor);
};
return figure;
}

public Figure getDefaultFigure() {
return new Circle(RADIUS, "WHITE");
}
}
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 double sideA;
private double sideB;
private double height;

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

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

@Override
public String printInformation() {
return "Figure: IsoscelesTrapezoid, area: " + getArea()
+ "sq. units, sideA: " + sideA + " units, sideB: " + sideB + " units, height: "
+ height + " units, color: " + getColor();
}
}
22 changes: 22 additions & 0 deletions src/main/java/core/basesyntax/MainApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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();
} else {
figures[i] = figureSupplier.getDefaultFigure();
}
System.out.println(figures[i].printInformation());
}
}
}
24 changes: 24 additions & 0 deletions src/main/java/core/basesyntax/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax;

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

public Rectangle(double sideA, double sideB, String color) {
super(color);
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();
}
}
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 double base;
private double height;

public RightTriangle(double base, double height, String color) {
super(color);
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();
}
}
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 int side;

public Square(int side, String color) {
super(color);
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