-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (call/factory): new factory method example with components
- Loading branch information
Showing
23 changed files
with
462 additions
and
1 deletion.
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
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,49 @@ | ||
package pt.c04gui.s20chart; | ||
|
||
import java.awt.Color; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.knowm.xchart.SwingWrapper; | ||
import org.knowm.xchart.XYChart; | ||
import org.knowm.xchart.XYChartBuilder; | ||
import org.knowm.xchart.XYSeries; | ||
import org.knowm.xchart.XYSeries.XYSeriesRenderStyle; | ||
import org.knowm.xchart.style.markers.SeriesMarkers; | ||
|
||
public class XYChart02 { | ||
|
||
public static void main(String[] args) { | ||
XYChart02 exampleChart = new XYChart02(); | ||
XYChart chart = exampleChart.getChart(); | ||
new SwingWrapper<XYChart>(chart).displayChart(); | ||
} | ||
|
||
public XYChart getChart() { | ||
|
||
// Create Chart | ||
XYChart chart = new XYChartBuilder().width(800).height(600).title("Line Chart").xAxisTitle("X").yAxisTitle("Y").build(); | ||
|
||
// Customize Chart | ||
chart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Line); | ||
chart.getStyler().setChartTitleVisible(false); | ||
chart.getStyler().setLegendVisible(false); | ||
chart.getStyler().setAxisTitlesVisible(false); | ||
chart.getStyler().setXAxisDecimalPattern("0.0000000"); | ||
|
||
// Series | ||
int size = 10; | ||
List<Double> xData = new ArrayList<Double>(); | ||
List<Double> yData = new ArrayList<Double>(); | ||
for (int i = 0; i <= size; i++) { | ||
xData.add(((double) i) / 1000000); | ||
yData.add(10 * Math.exp(-i)); | ||
} | ||
XYSeries series = chart.addSeries("10^(-x)", xData, yData); | ||
series.setMarkerColor(Color.RED); | ||
series.setMarker(SeriesMarkers.SQUARE); | ||
|
||
return chart; | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
src/java/src/pt/c06patterns/factory/s10components/App05ChartSequence.java
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,36 @@ | ||
package pt.c06patterns.factory.s10components; | ||
|
||
import java.util.Scanner; | ||
|
||
import pt.c06patterns.factory.s10components.chart.BarChartFactory; | ||
import pt.c06patterns.factory.s10components.chart.IBarChart; | ||
import pt.c06patterns.factory.s10components.chart.exception.PlotException; | ||
import pt.c06patterns.factory.s10components.sequence.IMathSequenceRatio; | ||
import pt.c06patterns.factory.s10components.sequence.MathSequenceFactory; | ||
|
||
public class App05ChartSequence { | ||
public static void main(String args[]) { | ||
System.out.print("Progression type (arithmetic or geometric): "); | ||
Scanner keyboard = new Scanner(System.in); | ||
String sequenceType = keyboard.nextLine(); | ||
|
||
System.out.print("Chart type (console ou graphic): "); | ||
String chartType = keyboard.nextLine(); | ||
|
||
keyboard.close(); | ||
|
||
IMathSequenceRatio gp = MathSequenceFactory.createSequenceRatio(sequenceType); | ||
gp.setInitial(1); | ||
gp.setRatio(2); | ||
|
||
try { | ||
IBarChart bcg = BarChartFactory.create(chartType); | ||
bcg.setFilled(true); | ||
bcg.setN(7); | ||
bcg.connect(gp); | ||
bcg.plot(); | ||
} catch (PlotException error) { | ||
System.err.println("*** Error: " + error.getMessage()); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/java/src/pt/c06patterns/factory/s10components/chart/BarChart.java
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 pt.c06patterns.factory.s10components.chart; | ||
|
||
import pt.c06patterns.factory.s10components.sequence.ISequence; | ||
|
||
public abstract class BarChart implements IBarChart { | ||
protected boolean filled; | ||
protected int n; | ||
|
||
protected ISequence sequence; | ||
|
||
public BarChart() { | ||
filled = true; | ||
n = 3; | ||
} | ||
|
||
public boolean isFilled() { | ||
return filled; | ||
} | ||
|
||
public void setFilled(boolean filled) { | ||
this.filled = filled; | ||
} | ||
|
||
public int getN() { | ||
return n; | ||
} | ||
|
||
public void setN(int n) { | ||
this.n = n; | ||
} | ||
|
||
public void connect(ISequence sequence) { | ||
this.sequence = sequence; | ||
} | ||
|
||
public abstract void plot(); | ||
} |
16 changes: 16 additions & 0 deletions
16
src/java/src/pt/c06patterns/factory/s10components/chart/BarChartFactory.java
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,16 @@ | ||
package pt.c06patterns.factory.s10components.chart; | ||
|
||
public class BarChartFactory { | ||
|
||
public static IBarChart create(String type) { | ||
IBarChart chart = null; | ||
|
||
if (type.equalsIgnoreCase("console")) | ||
chart = new ConsoleBarChart(); | ||
else if (type.equalsIgnoreCase("graphic")) | ||
chart = new GraphicBarChart(); | ||
|
||
return chart; | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
src/java/src/pt/c06patterns/factory/s10components/chart/ConsoleBarChart.java
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,41 @@ | ||
package pt.c06patterns.factory.s10components.chart; | ||
|
||
import pt.c06patterns.factory.s10components.chart.exception.InvalidBlankChar; | ||
import pt.c06patterns.factory.s10components.chart.exception.UnsupportedNegativeNumber; | ||
|
||
public class ConsoleBarChart extends BarChart { | ||
private char character; | ||
|
||
public ConsoleBarChart() { | ||
super(); | ||
character = '*'; | ||
} | ||
|
||
public char getCharacter() { | ||
return character; | ||
} | ||
|
||
public void setCharacter(char character) throws InvalidBlankChar { | ||
if (character == ' ') | ||
throw new InvalidBlankChar("the chart does not support blank character"); | ||
this.character = character; | ||
} | ||
|
||
public void plot() { | ||
if (sequence != null) { | ||
int value = sequence.first(); | ||
for (int s = 1; s <= n; s++) { | ||
if (value > 0) { | ||
for (int v = 1; v < value; v++) | ||
System.out.print((filled) ? character : ' '); | ||
System.out.print(character); | ||
} else if (value < 0) { | ||
System.out.println("?"); | ||
throw new UnsupportedNegativeNumber("the chart does not support a negative number"); | ||
} | ||
System.out.println(); | ||
value = sequence.next(); | ||
} | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/java/src/pt/c06patterns/factory/s10components/chart/GraphicBarChart.java
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,40 @@ | ||
package pt.c06patterns.factory.s10components.chart; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.knowm.xchart.CategoryChart; | ||
import org.knowm.xchart.CategoryChartBuilder; | ||
import org.knowm.xchart.SwingWrapper; | ||
|
||
public class GraphicBarChart extends BarChart { | ||
|
||
public void plot() { | ||
// Create Chart | ||
CategoryChart chart = | ||
new CategoryChartBuilder().width(800).height(600). | ||
title("Chart").xAxisTitle("X").yAxisTitle("Y").build(); | ||
|
||
// Customize Chart | ||
chart.getStyler().setChartTitleVisible(false); | ||
chart.getStyler().setLegendVisible(false); | ||
chart.getStyler().setAxisTitlesVisible(false); | ||
chart.getStyler().setXAxisDecimalPattern("0"); | ||
|
||
// Series | ||
List<Integer> xData = new ArrayList<Integer>(); | ||
List<Integer> yData = new ArrayList<Integer>(); | ||
if (sequence != null) { | ||
int value = sequence.first(); | ||
for (int s = 1; s <= n; s++) { | ||
xData.add(s); | ||
yData.add(value); | ||
value = sequence.next(); | ||
} | ||
} | ||
chart.addSeries("series", xData, yData); | ||
|
||
new SwingWrapper<CategoryChart>(chart).displayChart(); | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
src/java/src/pt/c06patterns/factory/s10components/chart/IBarChart.java
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 pt.c06patterns.factory.s10components.chart; | ||
|
||
public interface IBarChart | ||
extends IChart, IRSequence, IBarChartProperties { | ||
} |
9 changes: 9 additions & 0 deletions
9
src/java/src/pt/c06patterns/factory/s10components/chart/IBarChartProperties.java
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,9 @@ | ||
package pt.c06patterns.factory.s10components.chart; | ||
|
||
public interface IBarChartProperties { | ||
public boolean isFilled(); | ||
public void setFilled(boolean filled); | ||
|
||
public int getN(); | ||
public void setN(int n); | ||
} |
5 changes: 5 additions & 0 deletions
5
src/java/src/pt/c06patterns/factory/s10components/chart/IChart.java
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 pt.c06patterns.factory.s10components.chart; | ||
|
||
public interface IChart { | ||
public void plot(); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/java/src/pt/c06patterns/factory/s10components/chart/IRSequence.java
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,7 @@ | ||
package pt.c06patterns.factory.s10components.chart; | ||
|
||
import pt.c06patterns.factory.s10components.sequence.ISequence; | ||
|
||
public interface IRSequence { | ||
public void connect(ISequence sequence); | ||
} |
15 changes: 15 additions & 0 deletions
15
src/java/src/pt/c06patterns/factory/s10components/chart/exception/InvalidBlankChar.java
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,15 @@ | ||
package pt.c06patterns.factory.s10components.chart.exception; | ||
|
||
public class InvalidBlankChar extends PlotException { | ||
|
||
private static final long serialVersionUID = -9219514119468607139L; | ||
|
||
public InvalidBlankChar() { | ||
super(); | ||
} | ||
|
||
public InvalidBlankChar(String message) { | ||
super(message); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/java/src/pt/c06patterns/factory/s10components/chart/exception/PlotException.java
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,15 @@ | ||
package pt.c06patterns.factory.s10components.chart.exception; | ||
|
||
public class PlotException extends RuntimeException { | ||
|
||
private static final long serialVersionUID = 2727260512165247433L; | ||
|
||
public PlotException() { | ||
super(); | ||
} | ||
|
||
public PlotException(String message) { | ||
super(message); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
...a/src/pt/c06patterns/factory/s10components/chart/exception/UnsupportedNegativeNumber.java
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,15 @@ | ||
package pt.c06patterns.factory.s10components.chart.exception; | ||
|
||
public class UnsupportedNegativeNumber extends PlotException { | ||
|
||
private static final long serialVersionUID = -1248911490628763059L; | ||
|
||
public UnsupportedNegativeNumber() { | ||
super(); | ||
} | ||
|
||
public UnsupportedNegativeNumber(String message) { | ||
super(message); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/java/src/pt/c06patterns/factory/s10components/sequence/ArithmeticProgression.java
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,39 @@ | ||
package pt.c06patterns.factory.s10components.sequence; | ||
|
||
public class ArithmeticProgression implements IMathSequenceRatio { | ||
private int initial, | ||
ratio; | ||
private int current; | ||
|
||
public ArithmeticProgression() { | ||
initial = 1; | ||
ratio = 1; | ||
current = initial; | ||
} | ||
|
||
public int getInitial() { | ||
return initial; | ||
} | ||
|
||
public void setInitial(int initial) { | ||
this.initial = initial; | ||
} | ||
|
||
public int getRatio() { | ||
return ratio; | ||
} | ||
|
||
public void setRatio(int ratio) { | ||
this.ratio = ratio; | ||
} | ||
|
||
public int first() { | ||
current = initial; | ||
return current; | ||
} | ||
|
||
public int next() { | ||
current += ratio; | ||
return current; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/java/src/pt/c06patterns/factory/s10components/sequence/Fibonacci.java
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,42 @@ | ||
package pt.c06patterns.factory.s10components.sequence; | ||
|
||
public class Fibonacci implements IMathSequence { | ||
private int initialCurrent, initialNext; | ||
private int current, next; | ||
|
||
public Fibonacci() { | ||
initialCurrent = 0; | ||
initialNext = 1; | ||
current = 0; | ||
next = 1; | ||
} | ||
|
||
public int getInitial() { | ||
return initialCurrent; | ||
} | ||
|
||
public void setInitial(int initial) { | ||
current = 0; | ||
next = 1; | ||
while (initial > current) { | ||
int sum = current + next; | ||
current = next; | ||
next = sum; | ||
} | ||
initialCurrent = current; | ||
initialNext = next; | ||
} | ||
|
||
public int first() { | ||
current = initialCurrent; | ||
next = initialNext; | ||
return current; | ||
} | ||
|
||
public int next() { | ||
int sum = current + next; | ||
current = next; | ||
next = sum; | ||
return current; | ||
} | ||
} |
Oops, something went wrong.