Skip to content

Commit

Permalink
feat (call/factory): new factory method example with components
Browse files Browse the repository at this point in the history
  • Loading branch information
santanche committed Jun 22, 2021
1 parent 5ac6744 commit 4e83861
Show file tree
Hide file tree
Showing 23 changed files with 462 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -8,7 +8,7 @@ Todos os exemplos no diretório `notebook` são preparados para o ambiente Jupyt
## Abrir branch específico em uma instância do [binderhub](https://github.com/jupyterhub/binderhub)

* Última versão testada e estável:
[![launch @ mybinder.org][badge-jupyterlab-mybinder-org]](https://mybinder.org/v2/gh/santanche/java2learn/v1.1.5)
[![launch @ mybinder.org][badge-jupyterlab-mybinder-org]](https://mybinder.org/v2/gh/santanche/java2learn/v1.1.6)

* Última versão disponível:
[![launch @ mybinder.org][badge-jupyterlab-mybinder-org]](https://mybinder.org/v2/gh/santanche/java2learn/master)
Expand Down
49 changes: 49 additions & 0 deletions src/java/src/pt/c04gui/s20chart/XYChart02.java
@@ -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;
}

}
@@ -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());
}
}
}
@@ -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();
}
@@ -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;
}

}
@@ -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();
}
}
}
}
@@ -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();
}

}
@@ -0,0 +1,5 @@
package pt.c06patterns.factory.s10components.chart;

public interface IBarChart
extends IChart, IRSequence, IBarChartProperties {
}
@@ -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);
}
@@ -0,0 +1,5 @@
package pt.c06patterns.factory.s10components.chart;

public interface IChart {
public void plot();
}
@@ -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);
}
@@ -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);
}

}
@@ -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);
}

}
@@ -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);
}

}
@@ -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;
}
}
@@ -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;
}
}

0 comments on commit 4e83861

Please sign in to comment.