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
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(String color, double radius) {
super(color);
this.radius = radius;
}

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

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

Better to create Color enum, and use it in all figures as field. It would be easier and more clear to extend code. And also you would avoid issues when someone made a mistake writing some color

private Random random = new Random();

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

import core.basesyntax.Drawable;
import core.basesyntax.Measurable;

public abstract class Figure implements Drawable, Measurable {
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);
}
}
43 changes: 43 additions & 0 deletions src/main/java/core/basesyntax/FigureSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package core.basesyntax;

import java.util.Random;

class FigureSupplier {
private static final int FIGURE_TYPES_COUNT = 5;
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(10) + 1;
return new Square(color, side);
}
case 1: {
int width = random.nextInt(10) + 1;
int height = random.nextInt(10) + 1;
return new Rectangle(color, width, height);
}
case 2: {
int base = random.nextInt(10) + 1;
int height = random.nextInt(10) + 1;
return new RightTriangle(color, base, height);
}
case 3: {
int radius = random.nextInt(10) + 1;
return new Circle(color, radius);
}
default: {
int base1 = random.nextInt(10) + 1;
int base2 = random.nextInt(10) + 1;
int height = random.nextInt(10) + 1;

Choose a reason for hiding this comment

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

magic numbers

return new IsoscelesTrapezoid(color, base1, base2, height);
}
}
}

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.

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

public class IsoscelesTrapezoid extends Figure {
private double base1;
private double base2;
Copy link

Choose a reason for hiding this comment

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

numbers in naming are usually bad, let's name them topBase bottomBase

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 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("base1: " + base1 + " units, base2: " + base2 + " units, height: " + height + " units");
}

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

public class Main {
public static void main(String[] args) {
FigureSupplier figureSupplier = new FigureSupplier();
Figure[] figures = new Figure[6];

Choose a reason for hiding this comment

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

Magic number


// Generate random figures for the first half
for (int i = 0; i < figures.length / 2; i++) {
figures[i] = figureSupplier.getRandomFigure();
}

// Generate default figures for the second half
for (int i = figures.length / 2; i < figures.length; i++) {
figures[i] = figureSupplier.getDefaultFigure();
}

// Display all figures
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();
System.out.println();
}

Choose a reason for hiding this comment

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

It is possible to make everything in one loop

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

public interface Measurable {

Choose a reason for hiding this comment

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

Suggested change
public interface Measurable {
public interface AreaCalcualtor{

double getArea();
}
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(String color, double width, double height) {
super(color);
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(String color, double firstLeg, double secondLeg) {
super(color);
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(String color, double side) {
super(color);
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