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 #1737

Closed
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
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/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package core.basesyntax;

import core.basesyntax.model.Figure;
import core.basesyntax.service.FigureSupplier;

public class Main {
public static void main(String[] args) {
final int AMOUNT_FIGURES = 6;
final int HALF_FIGURES_COUNT = 3;

Figure[] figures = new Figure[AMOUNT_FIGURES];
FigureSupplier figureSupplier = new FigureSupplier();

for (int i = 0; i < HALF_FIGURES_COUNT; i++) {
figures[i] = figureSupplier.getRandomFigure();
}

for (int i = 3; i < AMOUNT_FIGURES; i++) {
figures[i] = figureSupplier.getDefaultFigure();
}

for (Figure figure : figures) {
figure.draw();
}
}
}
29 changes: 29 additions & 0 deletions src/main/java/core/basesyntax/model/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package core.basesyntax.model;

import core.basesyntax.service.AreaCalculator;

public class Circle extends Figure implements AreaCalculator {
private final double radius;

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

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

public double getRadius() {
return radius;
}

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

public enum Color {
Red,
Green,
Blue,
Yellow,
Orange,
Purple,
Black,
White,
Pink
}
15 changes: 15 additions & 0 deletions src/main/java/core/basesyntax/model/Figure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package core.basesyntax.model;

public abstract class Figure {
String color;

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

public String getColor() {
return color;
}

public abstract void draw();
}
45 changes: 45 additions & 0 deletions src/main/java/core/basesyntax/model/IsoscelesTrapezoid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package core.basesyntax.model;

import core.basesyntax.service.AreaCalculator;

public class IsoscelesTrapezoid extends Figure implements AreaCalculator {

private final double longerBase;
private final double shorterBase;
private final double height;

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

@Override
public double getArea() {
return (longerBase + shorterBase) * height / 2;
}


@Override
public void draw() {
System.out.println("Figure: " + this.getClass().getSimpleName().toLowerCase()
+ ", area: " + getArea() + " sq. units, "
+ "longerbase " + getLongerBase() + " units, "
+ "shoterbase " + getShorterBase() + " units, "
Comment on lines +28 to +29

Choose a reason for hiding this comment

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

There's a typo in the draw method output: 'shoterbase' should be 'shorterbase'.

+ "height " + getHeight() + " units, "
+ "color: " + getColor().toLowerCase());
}

public double getLongerBase() {
return longerBase;
}

public double getShorterBase() {
return shorterBase;
}

public double getHeight() {
return height;
}
}
36 changes: 36 additions & 0 deletions src/main/java/core/basesyntax/model/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package core.basesyntax.model;

import core.basesyntax.service.AreaCalculator;

public class Rectangle extends Figure implements AreaCalculator {
private final double length;
private final double width;

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

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

@Override
public void draw() {
System.out.println("Figure: " + this.getClass().getSimpleName().toLowerCase()
+ ", area: " + getArea() + " sq. units, "
+ "length " + getLength() + " units, "
+ "width " + getWidth() + " units, "
+ "color: " + getColor().toLowerCase());
}

public double getLength() {
return length;
}

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

import core.basesyntax.service.AreaCalculator;

public class RightTriangle extends Figure implements AreaCalculator {

private final double firstLeg;
private final double secondLeg;

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

@Override
public double getArea() {
return getFirstLeg() * getSecondLeg() / 2;
}

@Override
public void draw() {
System.out.println("Figure: " + this.getClass().getSimpleName().toLowerCase()
+ ", area: " + getArea() + " sq. units, "
+ "firstLeg " + getFirstLeg() + " units, "
+ "secondleg " + getSecondLeg() + " units, "
Comment on lines +25 to +26

Choose a reason for hiding this comment

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

There's a typo in the draw method output: 'secondleg' should be 'secondLeg'.

+ "color: " + getColor().toLowerCase());
}

public double getFirstLeg() {
return firstLeg;
}

public double getSecondLeg() {
return secondLeg;
}
}
30 changes: 30 additions & 0 deletions src/main/java/core/basesyntax/model/Square.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package core.basesyntax.model;

import core.basesyntax.service.AreaCalculator;

public class Square extends Figure implements AreaCalculator {

private final double side;

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

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

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

public double getSide() {
return side;
}
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/service/AreaCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax.service;

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

import core.basesyntax.model.Color;

import java.util.Random;

public class ColorSupplier {

public String getRandomColor() {
Random random = new Random();
Color[] colors = Color.values();
Color color = colors[random.nextInt(colors.length)];
return color.name();
}
}
30 changes: 30 additions & 0 deletions src/main/java/core/basesyntax/service/FigureSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package core.basesyntax.service;

import core.basesyntax.model.*;

import java.util.Random;

public class FigureSupplier {

public core.basesyntax.model.Figure getRandomFigure() {
ColorSupplier colorSupplier = new ColorSupplier();
Random random = new Random();

Figure circle = new Circle(colorSupplier.getRandomColor(), random.nextDouble());
Figure rectangle = new Rectangle(colorSupplier.getRandomColor(), random.nextDouble(), random.nextDouble());
Figure isoscelesTrapezoid = new IsoscelesTrapezoid(colorSupplier.getRandomColor(), random.nextDouble(), random.nextDouble(), 4);
Figure rightTriangle = new RightTriangle(colorSupplier.getRandomColor(), random.nextDouble(), random.nextDouble());
Figure square = new Square(colorSupplier.getRandomColor(), random.nextDouble());
Comment on lines +13 to +17

Choose a reason for hiding this comment

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

Using random.nextDouble() for dimensions may result in very small or zero values, which might not be suitable for figures. Consider using random.nextDouble() * someMaxValue to ensure the dimensions are within a reasonable range.


Figure[] figures = {
circle, rectangle,
isoscelesTrapezoid,
rightTriangle, square};

return figures[random.nextInt(figures.length)];
}

public core.basesyntax.model.Figure getDefaultFigure() {
return new Circle(Color.White.name(), 2);
}
}
Loading