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

add base classes, add base loop #1734

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

/**
* Feel free to remove this class and create your own.
*/
public class App {
public static final int FIGURES_LENGTH = 6;

public static void main(String[] args) {
Figure[] figures = new Figure[FIGURES_LENGTH];
FigureSupplier figureSupplier = new FigureSupplier();
for (int i = 0; i < figures.length; i++) {
boolean isFirstPart = i < (FIGURES_LENGTH / 2);
if (isFirstPart) {
figures[i] = figureSupplier.getRandomFigure();
} else {
figures[i] = figureSupplier.getDefaultFigure();
}

figures[i].drawDetails();
}
}
}
45 changes: 45 additions & 0 deletions src/main/java/core/basesyntax/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package core.basesyntax;

public class Circle extends Figure {
private int radius;

public Circle(int radius, Color color) {
setFigure(FigureType.CIRCLE);
setRadius(radius);
setColor(color);
double area = Math.round(getCalculatedArea());
setArea(area);
}

public Circle() {
setFigure(FigureType.CIRCLE);
setRadius(2);
setColor(Color.BLUE);
double area = Math.round(getCalculatedArea());
setArea(area);
}

@Override
public double getCalculatedArea() {
return Math.PI * radius * 2;

Choose a reason for hiding this comment

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

The formula for calculating the area of a circle is incorrect. It should be Math.PI * radius * radius instead of Math.PI * radius * 2. Please correct this to ensure the area is calculated accurately.

}

@Override
public void drawDetails() {
System.out.println(
String.format("Figure: %s, area: %s sq.units, radius: %s units, color: %s",
getFigure().toLowerCase(),
getArea(),
getRadius(),
getColor().toString().toLowerCase())
);
}

public void setRadius(int radius) {
this.radius = radius;
}

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

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

import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class ColorSupplier {
private final List<Color> availableColors = Arrays.asList(Color.values());
private final Random random = new Random();

public Color getRandomColor() {
int colorsMaxIndex = availableColors.size();
int randomIndex = random.nextInt(colorsMaxIndex);
return availableColors.get(randomIndex);
}
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/Draw.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

public interface Draw {
public void drawDetails();
}
33 changes: 33 additions & 0 deletions src/main/java/core/basesyntax/Figure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package core.basesyntax;

public abstract class Figure implements Draw {
private FigureType figure;
private Color color;
private double area;

public void setFigure(FigureType figure) {
this.figure = figure;
}

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

public void setArea(double area) {
this.area = area;
}

public String getFigure() {
return figure.name().toLowerCase();
}

public Color getColor() {
return color;
}

public double getArea() {
return area;
}

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

import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class FigureSupplier {
private static final int DEFAULT_CIRCLE_RADIUS = 10;
private static final int MAX_RANDOM_INTEGER = 10;
private final List<FigureType> availableFigures = Arrays.asList(FigureType.values());
private final Random random = new Random();
private final ColorSupplier color = new ColorSupplier();

public Figure getDefaultFigure() {
return new Circle(DEFAULT_CIRCLE_RADIUS, Color.WHITE);
}

public Figure getRandomFigure() {
int figuresMaxIndex = availableFigures.size();
int randomIndex = random.nextInt(figuresMaxIndex);

FigureType randomFigure = availableFigures.get(randomIndex);
Color randomColor = color.getRandomColor();

switch (randomFigure) {
case CIRCLE: return new Circle(
random.nextInt(MAX_RANDOM_INTEGER),
randomColor
);
case SQUARE: return new Square(
random.nextInt(MAX_RANDOM_INTEGER),
randomColor
);
case RECTANGLE: return new Rectangle(
random.nextInt(MAX_RANDOM_INTEGER),
random.nextInt(MAX_RANDOM_INTEGER),
randomColor
);
case RIGHT_TRIANGLE: return new RightTriangle(
random.nextInt(MAX_RANDOM_INTEGER),
random.nextInt(MAX_RANDOM_INTEGER),
randomColor
);
case ISOSCELES_TRAPEZOID: return new IsoscelesTrapezoid(
random.nextInt(MAX_RANDOM_INTEGER),
random.nextInt(MAX_RANDOM_INTEGER),
random.nextInt(MAX_RANDOM_INTEGER),
randomColor
);
default: return getDefaultFigure();
}

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

public enum FigureType {
SQUARE,
RECTANGLE,
RIGHT_TRIANGLE,
CIRCLE,
ISOSCELES_TRAPEZOID
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

public class IsoscelesTrapezoid extends Figure {
private static final String DEFAULT_DRAW_MESSAGE =
"Figure: %s, area: %s sq.units, top: %s units, "
+ "bottom: %s units, height: %s units, color: %s";
private int topLength;
private int bottomLength;
private int height;

public IsoscelesTrapezoid(int topLength, int bottomLength, int height, Color color) {
setFigure(FigureType.ISOSCELES_TRAPEZOID);
setTopLength(topLength);
setBottomLength(bottomLength);
setHeight(height);
setColor(color);
}

public IsoscelesTrapezoid() {
setFigure(FigureType.ISOSCELES_TRAPEZOID);
setTopLength(2);
setBottomLength(2);
setHeight(2);
setColor(Color.BLUE);
}

@Override
public double getCalculatedArea() {
return ((bottomLength * topLength) * height) / 2.0;

Choose a reason for hiding this comment

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

The formula for calculating the area of an isosceles trapezoid is incorrect. It should be ((topLength + bottomLength) * height) / 2.0 instead of ((bottomLength * topLength) * height) / 2.0. Please correct this to ensure the area is calculated accurately.

}

@Override
public void drawDetails() {
System.out.println(
String.format(
DEFAULT_DRAW_MESSAGE,
getFigure().toLowerCase(),
getArea(),
getTopLength(),
getBottomLength(),
getHeight(),
getColor().toString().toLowerCase())
);
}

public void setBottomLength(int bottomLength) {
this.bottomLength = bottomLength;
}

public void setTopLength(int topLength) {
this.topLength = topLength;
}

public void setHeight(int height) {
this.height = height;
}

public int getBottomLength() {
return bottomLength;
}

public int getTopLength() {
return topLength;
}

public int getHeight() {
return height;
}
}
60 changes: 60 additions & 0 deletions src/main/java/core/basesyntax/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package core.basesyntax;

public class Rectangle extends Figure {
private static final String DEFAULT_DRAW_MESSAGE =
"Figure: %s, area: %s sq.units, width: %s units, length: %s units, color: %s";
private int width;
private int length;

public Rectangle(int width, int length, Color color) {
setFigure(FigureType.RECTANGLE);
setWidth(width);
setLength(length);
double area = getCalculatedArea();
setArea(area);
setColor(color);
}

public Rectangle() {
setFigure(FigureType.RECTANGLE);
setWidth(2);
setLength(2);
double area = getCalculatedArea();
setArea(area);
setColor(Color.BLUE);
}

@Override
public double getCalculatedArea() {
return width * length;
}

@Override
public void drawDetails() {
System.out.println(
String.format(
DEFAULT_DRAW_MESSAGE,
getFigure().toLowerCase(),
getArea(),
getWidth(),
getLength(),
getColor().toString().toLowerCase())
);
}

public void setWidth(int width) {
this.width = width;
}

public void setLength(int length) {
this.length = length;
}

public int getWidth() {
return width;
}

public int getLength() {
return length;
}
}
Loading
Loading