Skip to content
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
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

import core.basesyntax.model.Figure;
import core.basesyntax.service.FigureSupplier;

public class Main {
private static final int COUNT_FIGURES = 6;
private static final int HALF_OF_LIST = COUNT_FIGURES / 2;

public static void main(String[] args) {
Figure[] figures = new Figure[COUNT_FIGURES];
FigureSupplier figureSupplier = new FigureSupplier();

for (int i = 0; i < figures.length; i++) {
if (i < HALF_OF_LIST) {
figures[i] = figureSupplier.getRandomFigure();
} else {
figures[i] = figureSupplier.getDefaultFigure();
}
figures[i].draw();
}
}
}
27 changes: 27 additions & 0 deletions src/main/java/core/basesyntax/model/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package core.basesyntax.model;

public class Circle extends Figure {
private int radius;

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

public int getRadius() {
return radius;
}

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

@Override
public void draw() {
System.out.println("Circle: "
+ "color: " + getColor()
+ ", radius: " + getRadius() + " units"
+ ", area: " + getArea() + " square units");

Choose a reason for hiding this comment

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

This violates the requirement for exact output format: area must be printed as "area: ... sq. units" (not "square units").

See task description and previous review: output must be exactly "sq. units" to pass tests.

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

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

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

public abstract class Figure implements Drawable {
private String color;

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

abstract double getArea();

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

public class IsoscelesTrapezoid extends Figure {
private int base;
private int height;
private int side;

public IsoscelesTrapezoid(int base, int height, int side, String color) {
super(color);
this.base = base;
this.height = height;
this.side = side;
}

public int getBase() {
return base;
}

public int getHeight() {
return height;
}

public int getSide() {
return side;
}

@Override
public double getArea() {
return 0.5 * (base + side) * height;
}

@Override
public void draw() {
System.out.println("IsoscelesTrapezoid: "
+ "color: " + getColor()
+ ", base: " + getBase() + " units"
+ ", height: " + getHeight() + " units"
+ ", side: " + getSide() + " units"
+ ", area: " + getArea() + " sq. units");
}
}
34 changes: 34 additions & 0 deletions src/main/java/core/basesyntax/model/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package core.basesyntax.model;

public class Rectangle extends Figure {
private int width;
private int height;

public Rectangle(int width, int height, String color) {
super(color);
this.width = width;
this.height = height;
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}

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

@Override
public void draw() {
System.out.println("Rectangle: "
+ "color: " + getColor()
+ ", width: " + getWidth() + " units"
+ ", height: " + getHeight() + " units"
+ ", area: " + getArea() + " square units");

Choose a reason for hiding this comment

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

The area output in draw() should use "sq. units" instead of "square units" to match the required format: 'area: sq. units'.

Choose a reason for hiding this comment

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

This violates the requirement for exact output format: area must be printed as "area: ... sq. units" (not "square units").

See task description and previous review: output must be exactly "sq. units" to pass tests.

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

public class RightTriangle extends Figure {
private int firstLeg;
private int secondLeg;

public RightTriangle(int firstLeg, int secondLeg, String color) {
super(color);
this.firstLeg = firstLeg;
this.secondLeg = secondLeg;
}

public int getFirstLeg() {
return firstLeg;
}

public int getSecondLeg() {
return secondLeg;
}

@Override
public double getArea() {
return 0.5 * firstLeg * secondLeg;
}

@Override
public void draw() {
System.out.println("RightTriangle: "
+ "color: " + getColor()
+ ", base: " + getFirstLeg() + " units"
+ ", height: " + getSecondLeg() + " units"
+ ", area: " + getArea() + " sq. units");
}
}
27 changes: 27 additions & 0 deletions src/main/java/core/basesyntax/model/Square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package core.basesyntax.model;

public class Square extends Figure {
private int side;

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

public int getSide() {
return side;
}

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

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

import core.basesyntax.model.Color;
import java.util.Random;

public class ColorSupplier {
private Random random = new Random();
private Color[] colors = Color.values();

public String getRandomColor() {
int randomIndex = random.nextInt(colors.length);

return colors[randomIndex].name();
}
}
58 changes: 58 additions & 0 deletions src/main/java/core/basesyntax/service/FigureSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package core.basesyntax.service;

import core.basesyntax.model.Circle;
import core.basesyntax.model.Figure;
import core.basesyntax.model.IsoscelesTrapezoid;
import core.basesyntax.model.Rectangle;
import core.basesyntax.model.RightTriangle;
import core.basesyntax.model.Square;
import java.util.Random;

public class FigureSupplier {
private static final int FIGURE_TYPES_COUNT = 5;
private static final int MAX_SIZE = 10;

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

public Figure getRandomFigure() {
int randomIndex = random.nextInt(FIGURE_TYPES_COUNT) + 1;
String color = colorSupplier.getRandomColor();

int side;
int radius;
int width;
int height;
int base;
int firstLeg;
int secondLeg;

switch (randomIndex) {
case 1:
side = random.nextInt(MAX_SIZE) + 1;
return new Square(side, color);
case 2:
radius = random.nextInt(MAX_SIZE) + 1;
return new Circle(radius, color);
case 3:
width = random.nextInt(MAX_SIZE) + 1;
height = random.nextInt(MAX_SIZE) + 1;
return new Rectangle(width, height, color);
case 4:
firstLeg = random.nextInt(MAX_SIZE) + 1;
secondLeg = random.nextInt(MAX_SIZE) + 1;
return new RightTriangle(firstLeg, secondLeg, color);
case 5:
base = random.nextInt(MAX_SIZE) + 1;
height = random.nextInt(MAX_SIZE) + 1;
side = random.nextInt(MAX_SIZE) + 1;
return new IsoscelesTrapezoid(base, height, side, color);
default:
return getDefaultFigure();
}
}

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