-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
implemented all the methods from the task #1284
base: master
Are you sure you want to change the base?
Changes from all commits
08cc25a
d42c7bf
fc6bec6
e2033b7
8e32637
33ff967
578c13c
4b2dcf1
498a4aa
d28763d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface AreaCalculator { | ||
double getArea(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package core.basesyntax; | ||
|
||
public class Circle extends Figure { | ||
private int radius; | ||
|
||
public Circle(int radius, Color color) { | ||
super(color); | ||
this.radius = radius; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return Math.PI * radius * radius; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: circle, " | ||
+ "area: " + getArea() + " sq. units, " | ||
+ "radius: " + radius + " units, " | ||
+ "color: " + this.getColor() | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package core.basesyntax; | ||
|
||
public enum Color { | ||
RED, | ||
ORANGE, | ||
YELLOW, | ||
GREEN, | ||
BLUE, | ||
INDIGO, | ||
VIOLET, | ||
WHITE, | ||
} |
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(); | ||
private Color[] colors = Color.values(); | ||
|
||
public Color getRandomColor() { | ||
return colors[random.nextInt(colors.length)]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface Drawable { | ||
void draw(); | ||
} |
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 Color color; | ||
|
||
public Figure(Color color) { | ||
this.color = color; | ||
} | ||
|
||
public Color getColor() { | ||
return color; | ||
} | ||
|
||
public void setColor(Color color) { | ||
this.color = color; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class FigureSupplier { | ||
private static final int MAX_FIGURES_TYPES = 5; | ||
private static final int SIZE_LIMIT = 5; | ||
private static final int DEFAULT_CIRCLE_RADIUS = 10; | ||
|
||
private ColorSupplier colorSupplier = new ColorSupplier(); | ||
private Random random = new Random(); | ||
|
||
private int getRandomSize() { | ||
return random.nextInt(SIZE_LIMIT) + 1; | ||
} | ||
|
||
public Figure getRandomFigure() { | ||
Color randomColor = colorSupplier.getRandomColor(); | ||
int sideA = getRandomSize(); | ||
switch (random.nextInt(MAX_FIGURES_TYPES) + 1) { | ||
case 1: | ||
return new Circle(sideA, randomColor); | ||
case 2: | ||
return new Square(sideA, randomColor); | ||
case 3: | ||
int rectangleSideB = getRandomSize(); | ||
return new Rectangle(sideA, rectangleSideB, randomColor); | ||
case 4: | ||
int triangleSideB = getRandomSize(); | ||
return new RightTriangle(sideA, triangleSideB, randomColor); | ||
case 5: | ||
int trapezoidSideB = getRandomSize(); | ||
int trapezoidHeight = getRandomSize(); | ||
return new IsoscelesTrapezoid(sideA, trapezoidSideB, trapezoidHeight, randomColor); | ||
default: | ||
return getDefaultFigure(); | ||
} | ||
} | ||
|
||
public Figure getDefaultFigure() { | ||
return new Circle(DEFAULT_CIRCLE_RADIUS, Color.WHITE); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: it's better to make Color.WHITE as a constant too with a proper name to give some context as you did with radius |
||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package core.basesyntax; | ||
|
||
public class IsoscelesTrapezoid extends Figure { | ||
private int sideA; | ||
private int sideB; | ||
private int height; | ||
|
||
public IsoscelesTrapezoid(int sideA, int sideB, int height, Color color) { | ||
super(color); | ||
this.sideA = sideA; | ||
this.sideB = sideB; | ||
this.height = height; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return (double) ((sideA + sideB) * height) / 2; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: isosceles trapezoid, " | ||
+ "area: " + getArea() + " sq. units, " | ||
+ "first side: " + sideA + " units, " | ||
+ "second side: " + sideB + " units, " | ||
+ "color: " + this.getColor() | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package core.basesyntax; | ||
|
||
public class Main { | ||
private static final int MAIN_LOOP_COUNT = 10; | ||
|
||
public static void main(String[] args) { | ||
Drawable[] figures = new Drawable[MAIN_LOOP_COUNT]; | ||
FigureSupplier figureSupplier = new FigureSupplier(); | ||
|
||
for (int i = 0; i < figures.length; i++) { | ||
if (i < figures.length / 2) { | ||
figures[i] = figureSupplier.getDefaultFigure(); | ||
} else { | ||
figures[i] = figureSupplier.getRandomFigure(); | ||
} | ||
} | ||
|
||
for (Drawable figure : figures) { | ||
figure.draw(); | ||
} | ||
Comment on lines
+18
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can draw a figure right after you've created it, eliminating the need to iterate over all the figures |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends Figure { | ||
private int sideA; | ||
private int sideB; | ||
|
||
public Rectangle(int sideA, int sideB, Color color) { | ||
super(color); | ||
this.sideA = sideA; | ||
this.sideB = sideB; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return sideA * sideB; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: rectangle, " | ||
+ "area: " + getArea() + " sq. units, " | ||
+ "first side: " + sideA + " units, " | ||
+ "second side: " + sideB + " units, " | ||
+ "color: " + this.getColor() | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package core.basesyntax; | ||
|
||
public class RightTriangle extends Figure { | ||
private int firstLeg; | ||
private int secondLeg; | ||
|
||
public RightTriangle(int firstLeg, int secondLeg, Color color) { | ||
super(color); | ||
this.firstLeg = firstLeg; | ||
this.secondLeg = secondLeg; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return (double) (firstLeg * secondLeg / 2); | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: right triangle, " | ||
+ "area: " + getArea() + " sq. units, " | ||
+ "first leg: " + firstLeg + " units, " | ||
+ "second leg: " + secondLeg + " units, " | ||
+ "color: " + this.getColor() | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package core.basesyntax; | ||
|
||
public class Square extends Figure { | ||
private int sideA; | ||
|
||
public Square(int sideA, Color color) { | ||
super(color); | ||
this.sideA = sideA; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return sideA * sideA; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: square, " | ||
+ "area: " + getArea() + " sq. units, " | ||
+ "side: " + sideA + " units, " | ||
+ "color: " + this.getColor() | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a constructor
public Figure(String color)