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

Realization of full application #1280

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
18 changes: 18 additions & 0 deletions src/main/java/core/basesyntax/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package core.basesyntax;

public class Application {
private static final int FIGURE_COUNT = 6;

public static void main(String[] args) {
FigureSupplier figureSupplier = new FigureSupplier();
Figure[] figures = new Figure[FIGURE_COUNT];
for (int i = 0; i < FIGURE_COUNT; i++) {
if (i < FIGURE_COUNT / 2) {
figures[i] = figureSupplier.getRandomFigure();
} else {
figures[i] = figureSupplier.getDefaultFigure();
}
figures[i].draw();
}
}
}
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();
}
23 changes: 23 additions & 0 deletions src/main/java/core/basesyntax/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package core.basesyntax;

public class Circle extends Figure {
private final int radius;

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

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

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

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

import java.util.Random;

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

public String getRandomColor() {
int index = random.nextInt(colorArray.length);
return colorArray[index].name();
}
}
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 {
protected String color;

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

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

import java.util.Random;

public class FigureSupplier {
private static final int MAX_LENGTH = 100;
private static final int FIGURE_COUNT = 5;
private static final int DEFAULT_RADIUS = 10;
private static final String DEFAULT_COLOR = Color.WHITE.name();

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

public Figure getRandomFigure() {
String color = supplier.getRandomColor();
final int index = random.nextInt(FIGURE_COUNT);

switch (index) {
case 0:
return new Circle(getRandomNumber(), color);
case 1:
return new IsoscelesTrapezoid(getRandomNumber(), getRandomNumber(),
getRandomNumber(), color);
case 2:
return new Rectangle(getRandomNumber(), getRandomNumber(), color);
case 3:
return new RightTriangle(getRandomNumber(), getRandomNumber(), color);
default:
return new Square(getRandomNumber(), color);
}
}

public Figure getDefaultFigure() {
return new Circle(DEFAULT_RADIUS, DEFAULT_COLOR);
}

private int getRandomNumber() {
return random.nextInt(MAX_LENGTH - 1) + 1;
}
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

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

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

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

@Override
public void draw() {
System.out.println("Figure: isosceles trapezoid, area: "
+ getArea()
+ ", height: " + height
+ ", first Base: " + firstBase
+ ", secondBase: "
+ secondBase
+ ", color: " + color);
}
}
26 changes: 26 additions & 0 deletions src/main/java/core/basesyntax/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package core.basesyntax;

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

public Rectangle(int firstSide, int secondSide, String color) {
super(color);
this.firstSide = firstSide;
this.secondSide = secondSide;
}

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

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

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

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

@Override
public void draw() {
System.out.println("Figure: right triangle, area: "
+ getArea()
+ ", first leg: "
+ firstLeg + ", second leg:"
+ secondLeg
+ ", color: "
+ color);
}

@Override
public double getArea() {
return (double) firstLeg * secondLeg / 2;
}
}
24 changes: 24 additions & 0 deletions src/main/java/core/basesyntax/Square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax;

public class Square extends Figure {
private final int side;

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

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

@Override
public double getArea() {
return side * side;
}
}
8 changes: 4 additions & 4 deletions src/test/java/core/test/StructureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public class StructureTest {
private static final List<String> figureClassNames = List
.of("Circle", "Square", "IsoscelesTrapezoid", "Rectangle", "RightTriangle");
.of("Circle", "Square", "IsoscelesTrapezoid", "Rectangle", "RightTriangle");
Copy link

Choose a reason for hiding this comment

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

I think you should only provide a solution and not change tests.

private static List<Class> allClasses = new ArrayList<>();

@BeforeClass
Expand All @@ -25,8 +25,8 @@ public static void init() {
allClasses = getClasses("core.basesyntax");
if (allClasses.size() == 0) {
Assert.fail("You should not rename base core.basesyntax package "
+ "and path to project and project name should not contain spaces "
+ "or words in cyrillic");
+ "and path to project and project name should not contain spaces "
+ "or words in cyrillic");
Comment on lines +28 to +29
Copy link

Choose a reason for hiding this comment

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

I think you should only provide a solution and not change tests.

}
} catch (Exception e) {
throw new RuntimeException("Could not load classes ", e);
Expand Down Expand Up @@ -152,4 +152,4 @@ private static List<Class> findClasses(File directory, String packageName)
}
return classes;
}
}
}
Copy link

Choose a reason for hiding this comment

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

The same as above.