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

jv-oop-advanced homework #1297

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
24 changes: 24 additions & 0 deletions src/main/java/core/basesyntax/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax;

public class Circle extends Figure {
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.

You already have Color color field in Figure class.

Copy link
Author

Choose a reason for hiding this comment

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

It has no sence, cuz it has to be inharited from Figure. Changed that everywhere

private double radius;

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

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

@Override
public void print() {
System.out.println("Figure: Circle "
+ " area " + getArea() + " sq.units,"
+ " side " + radius + " units"
+ " color " + color);
}
}
11 changes: 11 additions & 0 deletions src/main/java/core/basesyntax/Color.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package core.basesyntax;

public enum Color {
RED,
BLUE,
GREEN,
YELLOW,
WHITE,
BLACK,
VELVET;
}
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);

Choose a reason for hiding this comment

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

Minor: you already have the field which holds colors, you can reuse it here

return Color.values()[index];
Copy link

Choose a reason for hiding this comment

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

I think you can make Color.values() as separate class field.

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

public abstract class Figure implements GetArea, Print {
private Color color;
}
41 changes: 41 additions & 0 deletions src/main/java/core/basesyntax/FigureSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package core.basesyntax;

import java.util.Random;

public class FigureSupplier {
static final int FIGURE_AMOUNT = 5;
static final int FIGURE_PARAMETERS_MAX = 20;
static final Color DEFAULT_COLOR = Color.WHITE;
static final int DEFAULT_RADIUS = 10;

Choose a reason for hiding this comment

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

Don't forget to make these fields private

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

public Figure getRandomFigure() {
int randomFigureNumber = random.nextInt(FIGURE_AMOUNT + 1);
int randomFigureParameter = random.nextInt(FIGURE_PARAMETERS_MAX + 1);
switch (randomFigureNumber) {
case 0: {
return new Circle(colorSupplier.getRandomColor(), randomFigureParameter);
}
case 1: {
return new Square(colorSupplier.getRandomColor(), randomFigureParameter);
}
case 2: {
return new Rectangle(colorSupplier.getRandomColor(),
randomFigureParameter, randomFigureParameter);
}
case 3: {
return new RightTriangle(colorSupplier.getRandomColor(),
randomFigureParameter, randomFigureParameter);
}
default: {
return new IsoscelesTrapezoid(colorSupplier.getRandomColor(),
randomFigureParameter, randomFigureParameter);

Choose a reason for hiding this comment

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

since each figure needs a color you can extract colorSupplier.getRandomColor() to a separate variable and reuse

Choose a reason for hiding this comment

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

Your method is not fully random. For example, your Rectangle will always be a square since you pass the same randomFigureParameter there. So sizes of rectangle will always be equal. Please refactor this part

}
}
}

public Figure getDefaultFigure() {
return new Circle(DEFAULT_COLOR, DEFAULT_RADIUS);
}
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/GetArea.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

public interface GetArea {
Copy link

Choose a reason for hiding this comment

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

Think about this.

double getArea();
}
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 Color color;
private int side;
private int height;

public IsoscelesTrapezoid(Color color, int side, int height) {
this.color = color;
this.side = side;
this.height = height;
}

@Override
public double getArea() {
double sideD = side;
double heightD = height;
Copy link

Choose a reason for hiding this comment

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

Remove it.

return ((sideD + sideD) / 2d) * heightD;
}

@Override
public void print() {
System.out.println("Figure: Square "
+ " area " + getArea() + " sq.units,"
+ " side " + side + " units,"
+ " height " + height + " units,"
+ " color " + color);
}
}
21 changes: 21 additions & 0 deletions src/main/java/core/basesyntax/MainApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.basesyntax;

public class MainApp {
public static final int FIGURES_AMOUNT = 6;

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

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

public interface Print {
void print();
Copy link

Choose a reason for hiding this comment

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

I think figure should draw instead of print. In the task: To 'draw' means to print.
Lets rename it to Drawable with void draw().

}
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 Color color;
private int height;
private int width;

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

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

@Override
public void print() {
System.out.println("Figure: Square "
+ " area " + getArea() + " sq.units,"
+ " heigth " + height + " units"
+ " width " + width + " units"
+ " color " + color);
}
}
28 changes: 28 additions & 0 deletions src/main/java/core/basesyntax/RightTriangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package core.basesyntax;

public class RightTriangle extends Figure {
private Color color;
private int side;
private int height;

public RightTriangle(Color color, int side, int height) {
this.color = color;
this.side = side;
}

@Override
public double getArea() {
double sideD = side;
double heightD = height;
Copy link

Choose a reason for hiding this comment

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

Remove it.

return (sideD * heightD) / 2d;
}

@Override
public void print() {
System.out.println("Figure: Circle "
+ " area " + getArea() + " sq.units,"
+ " side " + side + " units"
+ " height " + height + " units"
+ " color " + color);
}
}
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 side;
private Color color;

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

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

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