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

implemented all the methods from the task #1284

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
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();
}
28 changes: 28 additions & 0 deletions src/main/java/core/basesyntax/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package core.basesyntax;

public class Circle extends Figure {
private int radius;

public Circle() {

}
Copy link

Choose a reason for hiding this comment

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

I think we should reject creating empty Circle. Let's remove that default constructor.


public Circle(int radius, Color color) {
this.radius = radius;
this.setColor(color);
Copy link

Choose a reason for hiding this comment

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

Replace it with super(color);.

}

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

@Override
public void draw() {
System.out.println("Figure: circle, "
+ "area: " + getArea() + " sq. units, "
+ "radius: " + radius + " units, "
+ "color: " + this.getColor()
);
}
}
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 {
RED,
ORANGE,
YELLOW,
GREEN,
BLUE,
INDIGO,
VIOLET,
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 Random random = new Random();
private Color[] colors = Color.values();

public Color getRandomColor() {
return colors[random.nextInt(colors.length)];
}
}
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();
}
23 changes: 23 additions & 0 deletions src/main/java/core/basesyntax/Figure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package core.basesyntax;

public class Figure implements Drawable, AreaCalculator {
Copy link

Choose a reason for hiding this comment

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

I think we can make this class abstract.

private Color color;
Copy link

Choose a reason for hiding this comment

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

Add a constructor public Figure(String color)


public Color getColor() {
return color;
}

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

@Override
public void draw() {

}

@Override
public double getArea() {
return 0;
}
Copy link

Choose a reason for hiding this comment

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

If we make class abstract we will be able to remove that emty overriding.

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

import java.util.Random;

public class FigureSupplier {
private static final int MAX_FIGURES_TYPES = 5;
private static final int SIZE_LIMIT = 5;
private ColorSupplier colorSupplier = new ColorSupplier();
private Random random = new Random();

public Figure getRandomFigure() {
Color randomColor = colorSupplier.getRandomColor();
int figureIndex = random.nextInt(MAX_FIGURES_TYPES) + 1;
int sideA = random.nextInt(SIZE_LIMIT) + 1;
switch (figureIndex) {
Copy link

Choose a reason for hiding this comment

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

You can simplify it with switch (random.nextInt(MAX_FIGURES_TYPES) + 1).

case 1:
return new Circle(sideA, randomColor);
case 2:
return new Square(sideA, randomColor);
case 3:
int rectangleSideB = random.nextInt(SIZE_LIMIT) + 1;
return new Rectangle(sideA, rectangleSideB, randomColor);
case 4:
int triangleSideB = random.nextInt(SIZE_LIMIT) + 1;
return new RightTriangle(sideA, triangleSideB, randomColor);
case 5:
int trapezoidSideB = random.nextInt(SIZE_LIMIT) + 1;
int trapezoidHeight = random.nextInt(SIZE_LIMIT) + 1;
return new IsoscelesTrapezoid(sideA, trapezoidSideB, trapezoidHeight, randomColor);
Copy link

Choose a reason for hiding this comment

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

You can extract random.nextInt(SIZE_LIMIT) + 1 to a separate method. And then you will be able to

 return new IsoscelesTrapezoid(getRandomValue(),
      getRandomValue(), 
      getRandomValue(), 
      colorSupplier.getRandomColor()
 );

default:
return new Figure();
}

Copy link

Choose a reason for hiding this comment

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

Empty redundant line.

}

public Figure getDefaultFigure() {
return new Circle(10, Color.WHITE);
Copy link

Choose a reason for hiding this comment

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

Looks like a magic number.

}
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

public class IsoscelesTrapezoid extends Figure {
private int sideA;
private int sideB;
private int height;

public IsoscelesTrapezoid(int sideA, int sideB, int height, Color color) {
this.sideA = sideA;
this.sideB = sideB;
this.height = height;
this.setColor(color);
}

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

@Override
public void draw() {
System.out.println("Figure: isosceles trapezoid, "
+ "area: " + getArea() + " sq. units, "
+ "first side: " + sideA + " units, "
+ "second side: " + sideB + " units, "
+ "color: " + this.getColor()
);
}
}
20 changes: 20 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package core.basesyntax;

public class Main {
public static void main(String[] args) {
Drawable[] figures = new Drawable[10];
Copy link

Choose a reason for hiding this comment

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

Looks like a magic number.

FigureSupplier figureSupplier = new FigureSupplier();

for (int i = 0; i < figures.length; i++) {
if (i < figures.length / 2) {
figures[i] = figureSupplier.getDefaultFigure();
} else {
figures[i] = figureSupplier.getRandomFigure();
}
}

for (Drawable figure : figures) {
figure.draw();
}
Comment on lines +18 to +20

Choose a reason for hiding this comment

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

You can draw a figure right after you've created it, eliminating the need to iterate over all the figures

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

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

public Rectangle(int sideA, int sideB, Color color) {
this.sideA = sideA;
this.sideB = sideB;
this.setColor(color);
}

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

@Override
public void draw() {
System.out.println("Figure: rectangle, "
+ "area: " + getArea() + " sq. units, "
+ "first side: " + sideA + " units, "
+ "second side: " + sideB + " units, "
+ "color: " + this.getColor()
);
}
}
27 changes: 27 additions & 0 deletions src/main/java/core/basesyntax/RightTriangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package core.basesyntax;

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

public RightTriangle(int firstLeg, int secondLeg, Color color) {
this.firstLeg = firstLeg;
this.secondLeg = secondLeg;
this.setColor(color);
}

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

@Override
public void draw() {
System.out.println("Figure: right triangle, "
+ "area: " + getArea() + " sq. units, "
+ "first leg: " + firstLeg + " units, "
+ "second leg: " + secondLeg + " units, "
+ "color: " + this.getColor()
);
}
}
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 int sideA;

public Square(int sideA, Color color) {
this.setColor(color);
this.sideA = sideA;
}

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

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