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

Solved the problem according to the principles of oop #1744

Open
wants to merge 2 commits 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/AreaCalculable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

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

public class Circle extends Figure {
private double radius;

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

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

@Override
public void draw() {
System.out.println("Figure: circle, area: " + calculateArea() + " sq. units, radius: "
+ radius + " units, color: " + getColor());
}
}
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 String[] COLORS = {"red", "blue", "green", "yellow", "purple", "black"};

public String getRandomColor() {
Random random = new Random();
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();
}
12 changes: 12 additions & 0 deletions src/main/java/core/basesyntax/Figure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package core.basesyntax;

public abstract class Figure implements AreaCalculable, Drawable {
private String color;

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

import java.util.Random;

public class FigureSupplier {
private final ColorSupplier colorSupplier = new ColorSupplier();
private final Random random = new Random();

public Figure getRandomFigure() {
int figureType = random.nextInt(5);

Choose a reason for hiding this comment

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

Consider defining a constant for the number of figure types (5) to avoid using magic numbers. This will make your code more readable and easier to maintain. For example, you could define private static final int FIGURE_TYPES_COUNT = 5; and use it here.


switch (figureType) {
case 0:
return new Square(colorSupplier.getRandomColor(), random.nextDouble() * 10 + 1);
case 1:
return new Rectangle(colorSupplier.getRandomColor(), random.nextDouble() * 10 + 1,
random.nextDouble() * 10 + 1);
case 2:
return new RightTriangle(colorSupplier.getRandomColor(), random.nextDouble() * 10
+ 1, random.nextDouble() * 10 + 1);
case 3:
return new Circle(colorSupplier.getRandomColor(), random.nextDouble() * 10 + 1);
case 4:
return new IsoscelesTrapezoid(colorSupplier.getRandomColor(), random.nextDouble()
* 10 + 1, random.nextDouble() * 10 + 1, random.nextDouble() * 10 + 1);
default:
return getDefaultFigure();
}
}

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

This file was deleted.

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

public class IsoscelesTrapezoid extends Figure {
private double base1;
private double base2;
private double height;

public IsoscelesTrapezoid(String color, double base1, double base2, double height) {
super(color);
this.base1 = base1;
this.base2 = base2;
this.height = height;
}

@Override
public double calculateArea() {
return 0.5 * (base1 + base2) * height;
}

@Override
public void draw() {
System.out.println("Shape: isosceles trapezoid, area: " + calculateArea()
+ " square units, first base: " + base1 + " units, second base: " + base2
+ " units, height: " + height + " units, color: " + color);

Choose a reason for hiding this comment

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

Consider using getColor() instead of accessing the color field directly for consistency and better encapsulation. This aligns with the practice of using getter methods to access private fields.


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

/**
* Feel free to remove this class and create your own.
*/
public class Main {
public static void main(String[] args) {
FigureSupplier figureSupplier = new FigureSupplier();
Figure[] figures = new Figure[6];

for (int i = 0; i < figures.length / 2; i++) {

Choose a reason for hiding this comment

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

The first loop is missing the logic to populate the first half of the figures array with random figures. You should call figureSupplier.getRandomFigure() within this loop to assign a random figure to each element in the first half of the array.

figures[i] = figureSupplier.getRandomFigure();
}
for (int i = figures.length / 2; i < figures.length; i++) {
figures[i] = figureSupplier.getDefaultFigure();
}
for (Figure figure : figures) {
figure.draw();
}
}
}
23 changes: 23 additions & 0 deletions src/main/java/core/basesyntax/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package core.basesyntax;

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

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

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

@Override
public void draw() {
System.out.println("Shape: rectangle, area: " + calculateArea() + "square units width: "
+ width + "height units: " + height + "color units: " + getColor());
Comment on lines +20 to +21

Choose a reason for hiding this comment

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

There are missing spaces in the output string of the draw method. Consider adding spaces after 'square units', 'width:', and 'height:' for better readability. The corrected string should look like: "Shape: rectangle, area: " + calculateArea() + " square units, width: " + width + " units, height: " + height + " 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(String color,double firstLeg, double secondLeg) {
super(color);
this.firstLeg = firstLeg;
this.secondLeg = secondLeg;
}

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

@Override
public void draw() {
System.out.println("Shape: right triangle, area: " + calculateArea()
+ " square units, first side: " + firstLeg + " units, second side: "
+ secondLeg + " units, color: " + getColor());
}
}
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(String color,double side) {
super(color);
this.side = side;
}

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

@Override
public void draw() {
System.out.println("Figure: square, area: " + calculateArea() + "square units, side: "
+ side + " color, units" + getColor());
Comment on lines +18 to +19

Choose a reason for hiding this comment

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

There are missing spaces in the output string of the draw method. Consider adding spaces after 'square units,' and before 'color' for better readability. The corrected string should look like: "Figure: square, area: " + calculateArea() + " square units, side: " + side + " units, color: " + getColor().

}
}
Loading