-
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
Jv oop advanced #1737
Jv oop advanced #1737
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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(); | ||
} | ||
} | ||
} |
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()); | ||
} | ||
} |
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 | ||
} |
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(); | ||
} |
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, " | ||
+ "height " + getHeight() + " units, " | ||
+ "color: " + getColor().toLowerCase()); | ||
} | ||
|
||
public double getLongerBase() { | ||
return longerBase; | ||
} | ||
|
||
public double getShorterBase() { | ||
return shorterBase; | ||
} | ||
|
||
public double getHeight() { | ||
return height; | ||
} | ||
} |
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; | ||
} | ||
} |
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
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. There's a typo in the |
||
+ "color: " + getColor().toLowerCase()); | ||
} | ||
|
||
public double getFirstLeg() { | ||
return firstLeg; | ||
} | ||
|
||
public double getSecondLeg() { | ||
return secondLeg; | ||
} | ||
} |
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax.service; | ||
|
||
public interface AreaCalculator { | ||
double getArea(); | ||
} |
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(); | ||
} | ||
} |
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
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. Using |
||
|
||
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); | ||
} | ||
} |
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.
There's a typo in the
draw
method output: 'shoterbase' should be 'shorterbase'.