generated from mate-academy/jv-homework-template
-
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
added solution according to README.md #1296
Open
vadymhrnk
wants to merge
1
commit into
mate-academy:master
Choose a base branch
from
vadymhrnk:oop-advanced-hw
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface AreaCalculator { | ||
double getArea(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package core.basesyntax; | ||
|
||
public class Circle extends Figure { | ||
private double radius; | ||
|
||
public Circle(Color color, double radius) { | ||
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: " + getColor()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package core.basesyntax; | ||
|
||
public enum Color { | ||
WHITE, | ||
BLACK, | ||
PURPLE, | ||
ORANGE, | ||
GREEN, | ||
BROWN, | ||
BLUE, | ||
YELLOW | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
return Color.values()[index]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax; | ||
|
||
public interface Drawable { | ||
void draw(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package core.basesyntax; | ||
|
||
public abstract class Figure implements Drawable, AreaCalculator { | ||
private Color color; | ||
|
||
public Figure(Color color) { | ||
this.color = color; | ||
} | ||
|
||
public String getColor() { | ||
return color.name().toLowerCase(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package core.basesyntax; | ||
|
||
import java.util.Random; | ||
|
||
public class FigureSupplier { | ||
private static final int MAX_FIGURE_NUMBER = 5; | ||
private static final int MAX_VALUE = 100; | ||
private static final int MIN_VALUE = 1; | ||
private static final double DEFAULT_RADIUS = 10; | ||
|
||
private Random random = new Random(); | ||
private ColorSupplier supplier = new ColorSupplier(); | ||
|
||
public Figure getRandomFigure() { | ||
int randomFigureNumber = random.nextInt(MAX_FIGURE_NUMBER); | ||
Color randomColor = supplier.getRandomColor(); | ||
|
||
switch (randomFigureNumber) { | ||
case 0: | ||
return new Circle(randomColor, | ||
getRandomNumber()); | ||
case 1: | ||
return new IsoscelesTrapezoid(randomColor, | ||
getRandomNumber(), getRandomNumber(), getRandomNumber()); | ||
case 2: | ||
return new Rectangle(randomColor, | ||
getRandomNumber(), getRandomNumber()); | ||
case 3: | ||
return new RightTriangle(randomColor, | ||
getRandomNumber(), getRandomNumber()); | ||
case 4: | ||
return new Square(randomColor, | ||
getRandomNumber()); | ||
default: | ||
return getDefaultFigure(); | ||
} | ||
} | ||
|
||
public Figure getDefaultFigure() { | ||
return new Circle(Color.WHITE, DEFAULT_RADIUS); | ||
} | ||
|
||
private int getRandomNumber() { | ||
return random.nextInt(MAX_VALUE) + MIN_VALUE; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package core.basesyntax; | ||
|
||
public class IsoscelesTrapezoid extends Figure { | ||
private double topParallelSide; | ||
private double bottomParallelSide; | ||
private double side; | ||
|
||
public IsoscelesTrapezoid( | ||
Color color, double topParallelSide, | ||
double bottomParallelSide, double side) { | ||
super(color); | ||
if (topParallelSide < bottomParallelSide) { | ||
this.topParallelSide = topParallelSide; | ||
this.bottomParallelSide = bottomParallelSide; | ||
} else { | ||
this.topParallelSide = bottomParallelSide; | ||
this.bottomParallelSide = topParallelSide; | ||
} | ||
this.side = side; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return ((topParallelSide + bottomParallelSide) / 2) | ||
* (Math.sqrt(side * side | ||
- Math.pow((bottomParallelSide - topParallelSide) / 2, 2))); | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: isosceles trapezoid, area: " + getArea() + " sq.units, " | ||
+ "topParallelSide: " + topParallelSide + " units, " | ||
+ "bottomParallelSide: " + bottomParallelSide + " units, " | ||
+ "side: " + side + " units, " | ||
+ "color: " + getColor()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package core.basesyntax; | ||
|
||
public class Main { | ||
private static final int SIZE = 6; | ||
|
||
public static void main(String[] args) { | ||
Figure[] figures = new Figure[SIZE]; | ||
FigureSupplier supplier = new FigureSupplier(); | ||
|
||
for (int i = 0; i < SIZE; i++) { | ||
if (SIZE / 2 > i) { | ||
figures[i] = supplier.getRandomFigure(); | ||
} else { | ||
figures[i] = supplier.getDefaultFigure(); | ||
} | ||
figures[i].draw(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package core.basesyntax; | ||
|
||
public class Rectangle extends Figure { | ||
private double height; | ||
private double width; | ||
|
||
public Rectangle(Color color, double width, double height) { | ||
super(color); | ||
this.width = width; | ||
this.height = height; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return height * width; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: rectangle, area: " + getArea() + " sq.units, " | ||
+ "height: " + height + " units, width: " + width + " units, " | ||
+ "color: " + getColor()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package core.basesyntax; | ||
|
||
public class RightTriangle extends Figure { | ||
private double firstLeg; | ||
private double secondLeg; | ||
|
||
public RightTriangle(Color color, double firstLeg, double secondLeg) { | ||
super(color); | ||
this.firstLeg = firstLeg; | ||
this.secondLeg = secondLeg; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return (firstLeg * secondLeg) / 2; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: right triangle, area: " + getArea() + " sq.units, " | ||
+ "firstLeg: " + firstLeg + " units, secondLeg: " + secondLeg + " units, " | ||
+ "color: " + getColor()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package core.basesyntax; | ||
|
||
public class Square extends Figure { | ||
private double side; | ||
|
||
public Square(Color color, double side) { | ||
super(color); | ||
this.side = side; | ||
} | ||
|
||
@Override | ||
public double getArea() { | ||
return side * side; | ||
} | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Figure: square, area: " + getArea() + " sq.units, " | ||
+ "side: " + side + " units, " | ||
+ "color: " + getColor()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If you are confident that your users will always pass the correct values (i.e., topParallelSide is always less than or equal to bottomParallelSide), then you could disable this check. However, it is usually better to be more secure and add this check to avoid incorrect use of the class.
In summary, good catch 👍