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

hw-1-jv-oop-advanced #1741

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

public interface AreaCalcualtor {
double getArea();
}
20 changes: 20 additions & 0 deletions src/main/java/core/basesyntax/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package core.basesyntax;

public class Circle extends Figure {
private double radius;

public Circle(Color color, double radius) {

Choose a reason for hiding this comment

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

Suggested change
public Circle(Color color, double radius) {
public Circle(String color, double radius) {

Choose a reason for hiding this comment

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

For each figure

Choose a reason for hiding this comment

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

not fixed

super(color.name());

Choose a reason for hiding this comment

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

Suggested change
super(color.name());
super(color);

Choose a reason for hiding this comment

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

not fixed

this.radius = radius;
}

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

@Override
public void draw() {
System.out.println("radius: " + radius + " units");
}
}
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, GREEN, BLUE, YELLOW, BLACK, WHITE;

Choose a reason for hiding this comment

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

Suggested change
RED, GREEN, BLUE, YELLOW, BLACK, WHITE;
RED,
GREEN,
BLUE,
YELLOW,
BLACK,
WHITE

Choose a reason for hiding this comment

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

not fixed

}
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 static final Random RANDOM = new Random();

public String getRandomColor() {
Color[] colors = Color.values();
return colors[RANDOM.nextInt(colors.length)].toString();

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

not fixed

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

public abstract class Figure implements Drawable, AreaCalcualtor {
protected String color;

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

@Override
public void draw() {
System.out.println("Figure: " + this.getClass().getSimpleName()
+ ", area: " + getArea() + " sq. units, color: " + color);
}
}
47 changes: 47 additions & 0 deletions src/main/java/core/basesyntax/FigureSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package core.basesyntax;

import java.util.Random;

class FigureSupplier {
private static final int FIGURE_TYPES_COUNT = 5;
private static final int MAX_DIMENSION = 10;
private static final int MIN_DIMENSION = 1;
private static final int DEFAULT_RADIUS = 10;

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

public Figure getRandomFigure() {
String color = colorSupplier.getRandomColor();
switch (random.nextInt(FIGURE_TYPES_COUNT)) {
case 0: {
int side = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION;

Choose a reason for hiding this comment

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

let's avoid duplication of code
create separate method to get random side

Choose a reason for hiding this comment

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

not fixed

return new Square(Color.valueOf(color), side);
}
case 1: {
int firstLeg = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION;
int secondLeg = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION;
return new Rectangle(Color.valueOf(color), firstLeg, secondLeg);
}
case 2: {
int firstLeg = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION;
int secondLeg = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION;
return new RightTriangle(Color.valueOf(color), firstLeg, secondLeg);
}
case 3: {
int radius = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION;
return new Circle(Color.valueOf(color), radius);
}
default: {
int topBase = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION;
int bottomBase = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION;
int secondLeg = random.nextInt(MAX_DIMENSION) + MIN_DIMENSION;
return new IsoscelesTrapezoid(Color.valueOf(color), topBase, bottomBase, secondLeg);
}
}
}

public Figure getDefaultFigure() {
return new Circle(Color.WHITE, DEFAULT_RADIUS);
}
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

public class IsoscelesTrapezoid extends Figure {
private double topBase;
private double bottomBase;
private double height;

public IsoscelesTrapezoid(Color color, double topBase, double bottomBase, double height) {
super(color.name());
this.topBase = topBase;
this.bottomBase = bottomBase;
this.height = height;
}

@Override
public void draw() {
super.draw();

Choose a reason for hiding this comment

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

Ensure that the super.draw() call is necessary. This depends on the implementation of the Figure class. If Figure does not have a draw() method or if it does not need to be called, this line can be removed.

System.out.println("topBase: " + topBase + " units, bottomBase: "
+ bottomBase + " units, height: " + height + " units");
}

@Override
public double getArea() {
return 0.5 * (topBase + bottomBase) * height;
}
}
22 changes: 22 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package core.basesyntax;

public class Main {
public static void main(String[] args) {
FigureSupplier figureSupplier = new FigureSupplier();
final int arraySize = (int) (Math.random() * 10) + 1;
Figure[] figures = new Figure[arraySize];
Comment on lines +6 to +7

Choose a reason for hiding this comment

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

create constant SIZE_OF_ARRAY for example 6

Suggested change
final int arraySize = (int) (Math.random() * 10) + 1;
Figure[] figures = new Figure[arraySize];
Figure[] figures = new Figure[SIZE_OF_ARRAY];

Choose a reason for hiding this comment

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

not fixed


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

for (Figure figure : figures) {

Choose a reason for hiding this comment

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

use one for loop to implement main method

Choose a reason for hiding this comment

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

not fixed

figure.draw();
}
}
}

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

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

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

@Override
public void draw() {
System.out.println("width: " + width + " units, height: " + height + " units");
}

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

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

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

@Override
public void draw() {
System.out.println("firstLeg: " + firstLeg + " units, secondLeg: " + secondLeg + " units");
}

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

public class Square extends Figure {
private double side;

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

@Override
public void draw() {
super.draw();

Choose a reason for hiding this comment

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

Ensure that the super.draw() call is necessary. This depends on the implementation of the Figure class. If Figure does not have a draw() method or if it does not need to be called, this line can be removed.

Choose a reason for hiding this comment

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

Agree

System.out.println("side: " + side + " units");
}

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