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

added solution according to README.md #1296

Open
wants to merge 1 commit 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
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();
}
22 changes: 22 additions & 0 deletions src/main/java/core/basesyntax/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package core.basesyntax;

public class Circle extends Figure {
private double radius;

public Circle(Color color, double radius) {
super(color);
this.radius = radius;
}

@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: " + 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 {
WHITE,
BLACK,
PURPLE,
ORANGE,
GREEN,
BROWN,
BLUE,
YELLOW
}
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();

public Color getRandomColor() {
int index = random.nextInt(Color.values().length);
return Color.values()[index];
}
}
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 {
private Color color;

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

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

import java.util.Random;

public class FigureSupplier {
private static final int MAX_FIGURE_NUMBER = 5;
private static final int MAX_VALUE = 100;
private static final int MIN_VALUE = 1;
private static final double DEFAULT_RADIUS = 10;

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

public Figure getRandomFigure() {
int randomFigureNumber = random.nextInt(MAX_FIGURE_NUMBER);
Color randomColor = supplier.getRandomColor();

switch (randomFigureNumber) {
case 0:
return new Circle(randomColor,
getRandomNumber());
case 1:
return new IsoscelesTrapezoid(randomColor,
getRandomNumber(), getRandomNumber(), getRandomNumber());
case 2:
return new Rectangle(randomColor,
getRandomNumber(), getRandomNumber());
case 3:
return new RightTriangle(randomColor,
getRandomNumber(), getRandomNumber());
case 4:
return new Square(randomColor,
getRandomNumber());
default:
return getDefaultFigure();
}
}

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

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

This file was deleted.

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

public class IsoscelesTrapezoid extends Figure {
private double topParallelSide;
private double bottomParallelSide;
private double side;

public IsoscelesTrapezoid(
Color color, double topParallelSide,
double bottomParallelSide, double side) {
super(color);
if (topParallelSide < bottomParallelSide) {
this.topParallelSide = topParallelSide;
this.bottomParallelSide = bottomParallelSide;
} else {
this.topParallelSide = bottomParallelSide;
this.bottomParallelSide = topParallelSide;
}
Comment on lines +12 to +18
Copy link

Choose a reason for hiding this comment

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

If you are confident that your users will always pass the correct values (i.e., topParallelSide is always less than or equal to bottomParallelSide), then you could disable this check. However, it is usually better to be more secure and add this check to avoid incorrect use of the class.

In summary, good catch 👍

this.side = side;
}

@Override
public double getArea() {
return ((topParallelSide + bottomParallelSide) / 2)
* (Math.sqrt(side * side
- Math.pow((bottomParallelSide - topParallelSide) / 2, 2)));
}

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

public class Main {
private static final int SIZE = 6;

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

for (int i = 0; i < SIZE; i++) {
if (SIZE / 2 > i) {
figures[i] = supplier.getRandomFigure();
} else {
figures[i] = supplier.getDefaultFigure();
}
figures[i].draw();
}
}
}
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 height;
private double width;

public Rectangle(Color color, double width, double height) {
super(color);
this.width = width;
this.height = height;
}

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

@Override
public void draw() {
System.out.println("Figure: rectangle, area: " + getArea() + " sq.units, "
+ "height: " + height + " units, width: " + width + " 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 firstLeg;
private double secondLeg;

public RightTriangle(Color color, double firstLeg, double secondLeg) {
super(color);
this.firstLeg = firstLeg;
this.secondLeg = secondLeg;
}

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

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

public class Square extends Figure {
private double side;

public Square(Color color, double side) {
super(color);
this.side = side;
}

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

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