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

Develop #1711

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open

Develop #1711

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

public class Circle extends Figure {
private double radius;

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

public double getRadius() {
return radius;
}

public void setRadius(double radius) {
this.radius = radius;
}

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

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

public enum Color {
RED,
BLUE,
GREEN,
YELLOW,
ORANGE,
BROWN,
GREY,
BLACK,
WHITE,
PURPLE,
PINK,
}
13 changes: 13 additions & 0 deletions src/main/java/core/basesyntax/ColorSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package core.basesyntax;

import java.util.Random;

public class ColorSupplier {
private static Random random = new Random();

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

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

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

public String getColor() {
return color;
}

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

import java.util.Random;

public class FigureSupplier {
private static final int FIGURES_NUMBER = 5;
private static final double DEFAULT_RADIUS = 10;
private static Random random = new Random();

private static ColorSupplier colorSupplier = new ColorSupplier();

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

public Figure getRandomFigure() {
int randomFigure = random.nextInt(FIGURES_NUMBER);
return switch (randomFigure) {
case 0 -> new Square(colorSupplier.getRandomColor(), random.nextDouble());
case 1 -> new Rectangle(colorSupplier.getRandomColor(), random.nextDouble(),
random.nextDouble());
case 2 -> new Circle(colorSupplier.getRandomColor(), random.nextDouble());
case 3 -> new RightTriangle(colorSupplier.getRandomColor(), random.nextDouble(),
random.nextDouble());
default -> new IsoscelesTrapezoid(colorSupplier.getRandomColor(), random.nextDouble(),
random.nextDouble(), random.nextDouble());
};
}
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

public class IsoscelesTrapezoid extends Figure {
private double height;
private double firstBase;
private double secondBase;

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

public double getHeight() {
return height;
}

public void setHeight(double height) {
this.height = height;
}

public double getFirstBase() {
return firstBase;
}

public double getSecondBase() {
return secondBase;
}

public void setFirstBase(double firstBase) {
this.firstBase = firstBase;
}

public void setSecondBase(double secondBase) {
this.secondBase = secondBase;
}

@Override
public void draw() {
System.out.println("Figure: " + this.getClass()
+ ", area: " + this.calculateArea() + " sq. units,"
+ " first base: " + this.firstBase + " units, "
+ " second base: " + this.secondBase + " units, "
+ " height: " + this.height + " units, "
+ "color: " + this.getColor());
}

@Override
public double calculateArea() {
return ((firstBase * secondBase) * 0.5 * height);
}

}
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 {
public static void main(String[] args) {
final int size = 6;
Figure[] figureArray = new Figure[size];
FigureSupplier figureSupplier = new FigureSupplier();
for (int i = 0; i < size; i++) {
if (i < size / 2) {
figureArray[i] = figureSupplier.getRandomFigure();
} else {
figureArray[i] = figureSupplier.getDefaultFigure();
}
}
for (int j = 0; j < size; j++) {
figureArray[j].draw();
}
}
}
42 changes: 42 additions & 0 deletions src/main/java/core/basesyntax/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package core.basesyntax;

public class Rectangle extends Figure {
private double firstSide;
private double secondSide;

public Rectangle(String color, double firstSide, double secondSide) {
super(color);
this.firstSide = firstSide;
this.secondSide = secondSide;
}

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

@Override
public void draw() {
System.out.println("Figure: " + this.getClass()
+ ", area: " + this.calculateArea() + " sq. units,"
+ " first side: " + this.firstSide + " units, "
+ " second side: " + this.secondSide + " units, "
+ "color: " + this.getColor());
}

public double getFirstSide() {
return firstSide;
}

public double getSecondSide() {
return secondSide;
}

public void setFirstSide(double firstSide) {
this.firstSide = firstSide;
}

public void setSecondSide(double secondSide) {
this.secondSide = secondSide;
}
}
42 changes: 42 additions & 0 deletions src/main/java/core/basesyntax/RightTriangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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;
}

public double getFirstLeg() {
return firstLeg;
}

public double getSecondLeg() {
return secondLeg;
}

public void setFirstLeg(double firstLeg) {
this.firstLeg = firstLeg;
}

public void setSecondLeg(double seconLeg) {
this.secondLeg = seconLeg;
}

@Override
public void draw() {
System.out.println("Figure: " + this.getClass()
+ ", area: " + this.calculateArea() + " sq. units,"
+ " first leg: " + this.firstLeg + " units, "
+ " second leg: " + this.secondLeg + " units, "
+ "color: " + this.getColor());
}

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

public class Square extends Figure {
private double side;

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

public void setSide(double side) {
this.side = side;
}

public double getSide() {
return side;
}

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

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